Skip to content

Commit 92962ea

Browse files
authored
Add TypeGuard to is_formatted_phone_number (#297)
I added an `assert_type` row since it seems that you are checking types with `pytest-mypy-plugins` but I figured using `assert_type` is probably preferable
1 parent 0b2fcaa commit 92962ea

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/phantom/ext/phonenumbers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import cast
1414

1515
import phonenumbers
16+
from typing_extensions import TypeGuard
1617

1718
from phantom import Phantom
1819
from phantom.bounds import parse_str
@@ -67,7 +68,7 @@ def normalize_phone_number(
6768
is_phone_number = excepts(InvalidPhoneNumber)(_deconstruct_phone_number)
6869

6970

70-
def is_formatted_phone_number(number: str) -> bool:
71+
def is_formatted_phone_number(number: str) -> TypeGuard[FormattedPhoneNumber]:
7172
try:
7273
return number == normalize_phone_number(number)
7374
except InvalidPhoneNumber:

tests/ext/test_phonenumbers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from typing_extensions import assert_type
23

34
from phantom.errors import BoundError
45
from phantom.ext.phonenumbers import FormattedPhoneNumber
@@ -103,8 +104,10 @@ def test_returns_false_for_invalid_number(self):
103104

104105

105106
class TestIsFormattedPhoneNumber:
106-
def test_returns_true_for_formatted_number(self):
107-
assert is_formatted_phone_number("+46123456789") is True
107+
def test_returns_true_for_formatted_number(self) -> None:
108+
value = "+46123456789"
109+
assert is_formatted_phone_number(value)
110+
assert_type(value, FormattedPhoneNumber)
108111

109112
def test_returns_false_for_unformatted_number(self):
110113
assert is_formatted_phone_number("+46 (123) 456 789") is False

0 commit comments

Comments
 (0)