@@ -147,29 +147,56 @@ def test_isna() -> None:
147
147
assert not check (assert_type (pd .isna (2.5 ), bool ), bool )
148
148
assert check (assert_type (pd .notna (2.5 ), bool ), bool )
149
149
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
151
155
nullable1 = random .choice (["value" , None , pd .NA , pd .NaT ])
152
156
if pd .notna (nullable1 ):
153
157
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 )
154
162
if pd .isna (nullable1 ):
155
163
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 ])
156
168
157
169
nullable2 = random .choice ([2 , None ])
158
170
if pd .notna (nullable2 ):
159
171
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 )
160
176
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 ])
167
186
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 )
169
192
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)
171
194
# assert_type(nullable3, Union[NAType, None])
172
195
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 ])
173
200
174
201
175
202
# GH 55
0 commit comments