Skip to content

Commit f649e13

Browse files
committed
Adjust TypeGuard tests to document limitations of notna/isna type narrowing
1 parent 39a4606 commit f649e13

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

tests/test_pandas.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,56 @@ def test_isna() -> None:
147147
assert not check(assert_type(pd.isna(2.5), bool), bool)
148148
assert check(assert_type(pd.notna(2.5), bool), bool)
149149

150-
# Check type guard functionality
150+
# Check TypeGuard type narrowing functionality
151+
# TODO: Due to limitations in TypeGuard spec, the true annotations are not always viable
152+
# and as a result the type narrowing does not always work as it intuitively should
153+
# There is a proposal being floated for a StrictTypeGuard that will have more rigid narrowing semantics
154+
# In the test cases below, a commented out assertion will be included to document the optimal test result
151155
nullable1 = random.choice(["value", None, pd.NA, pd.NaT])
152156
if pd.notna(nullable1):
153157
check(assert_type(nullable1, str), str)
158+
if not pd.isna(nullable1):
159+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
160+
# check(assert_type(nullable1, str), str)
161+
check(assert_type(nullable1, Union[str, NaTType, NAType, None]), str)
154162
if pd.isna(nullable1):
155163
assert_type(nullable1, Union[NaTType, NAType, None])
164+
if not pd.notna(nullable1):
165+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
166+
# assert_type(nullable1, Union[NaTType, NAType, None])
167+
assert_type(nullable1, Union[str, NaTType, NAType, None])
156168

157169
nullable2 = random.choice([2, None])
158170
if pd.notna(nullable2):
159171
check(assert_type(nullable2, int), int)
172+
if not pd.isna(nullable2):
173+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
174+
# check(assert_type(nullable2, int), int)
175+
check(assert_type(nullable2, Union[int, None]), int)
160176
if pd.isna(nullable2):
161-
# TODO: Due to limitations in TypeGuard spec, the true annotation is not viable at this time
162-
# There is a proposal being floated for a StrictTypeGuard that will have more rigid narrowing semantics
163-
# assert_type(nullable2, None)
164-
assert_type(nullable2, Union[NaTType, NAType, None])
165-
166-
nullable3 = random.choice([2, None, pd.NA])
177+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
178+
# check(assert_type(nullable2, None), type(None))
179+
check(assert_type(nullable2, Union[NaTType, NAType, None]), type(None))
180+
if not pd.notna(nullable2):
181+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
182+
# check(assert_type(nullable2, None), type(None))
183+
assert_type(nullable2, Union[int, NaTType, NAType, None])
184+
185+
nullable3 = random.choice([True, None, pd.NA])
167186
if pd.notna(nullable3):
168-
check(assert_type(nullable3, int), int)
187+
check(assert_type(nullable3, bool), bool)
188+
if not pd.isna(nullable3):
189+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
190+
# check(assert_type(nullable3, bool), bool)
191+
check(assert_type(nullable3, Union[bool, NAType, None]), bool)
169192
if pd.isna(nullable3):
170-
# TODO: See comment above about the limitations of TypeGuard
193+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
171194
# assert_type(nullable3, Union[NAType, None])
172195
assert_type(nullable3, Union[NaTType, NAType, None])
196+
if not pd.notna(nullable3):
197+
# TODO: This is the true type (see comments above on the limitations of TypeGuard)
198+
# assert_type(nullable3, Union[NAType, None])
199+
assert_type(nullable3, Union[bool, NaTType, NAType, None])
173200

174201

175202
# GH 55

0 commit comments

Comments
 (0)