diff --git a/setup.cfg b/setup.cfg index 8c93574..f5ddb8c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,7 +32,10 @@ package_dir = packages = find: python_requires = >=3.9 install_requires = - typeguard>=4 + # typeguard 4.3.0 breaks "intersection" protocols, see linked issue. I didn't figure + # out a way to work around this at the moment, so it needs to be pinned. + # https://github.com/antonagestam/phantom-types/issues/299 + typeguard>=4,<4.3.0 typing_extensions>=4.3.0 numerary>=0.4.3 diff --git a/src/phantom/ext/phonenumbers.py b/src/phantom/ext/phonenumbers.py index de792fd..23444aa 100644 --- a/src/phantom/ext/phonenumbers.py +++ b/src/phantom/ext/phonenumbers.py @@ -13,6 +13,7 @@ from typing import cast import phonenumbers +from typing_extensions import TypeGuard from phantom import Phantom from phantom.bounds import parse_str @@ -67,7 +68,7 @@ def normalize_phone_number( is_phone_number = excepts(InvalidPhoneNumber)(_deconstruct_phone_number) -def is_formatted_phone_number(number: str) -> bool: +def is_formatted_phone_number(number: str) -> TypeGuard[FormattedPhoneNumber]: try: return number == normalize_phone_number(number) except InvalidPhoneNumber: diff --git a/tests/ext/test_phonenumbers.py b/tests/ext/test_phonenumbers.py index 9602559..e265866 100644 --- a/tests/ext/test_phonenumbers.py +++ b/tests/ext/test_phonenumbers.py @@ -1,4 +1,5 @@ import pytest +from typing_extensions import assert_type from phantom.errors import BoundError from phantom.ext.phonenumbers import FormattedPhoneNumber @@ -103,8 +104,10 @@ def test_returns_false_for_invalid_number(self): class TestIsFormattedPhoneNumber: - def test_returns_true_for_formatted_number(self): - assert is_formatted_phone_number("+46123456789") is True + def test_returns_true_for_formatted_number(self) -> None: + value = "+46123456789" + assert is_formatted_phone_number(value) + assert_type(value, FormattedPhoneNumber) def test_returns_false_for_unformatted_number(self): assert is_formatted_phone_number("+46 (123) 456 789") is False