2929 from types import MethodType , ModuleType
3030 from typing import Final , Protocol , Union
3131
32- from typing_extensions import TypeAlias , TypeGuard
32+ from typing_extensions import TypeAlias , TypeIs
3333
3434 class _SupportsGet (Protocol ):
3535 def __get__ (self , __instance : Any , __owner : type | None = ...) -> Any : ... # NoQA: E704
@@ -213,12 +213,12 @@ def isNewType(obj: Any) -> bool:
213213 return __module__ == 'typing' and __qualname__ == 'NewType.<locals>.new_type'
214214
215215
216- def isenumclass (x : Any ) -> TypeGuard [type [enum .Enum ]]:
216+ def isenumclass (x : Any ) -> TypeIs [type [enum .Enum ]]:
217217 """Check if the object is an :class:`enumeration class <enum.Enum>`."""
218218 return isclass (x ) and issubclass (x , enum .Enum )
219219
220220
221- def isenumattribute (x : Any ) -> TypeGuard [enum .Enum ]:
221+ def isenumattribute (x : Any ) -> TypeIs [enum .Enum ]:
222222 """Check if the object is an enumeration attribute."""
223223 return isinstance (x , enum .Enum )
224224
@@ -235,7 +235,7 @@ def unpartial(obj: Any) -> Any:
235235 return obj
236236
237237
238- def ispartial (obj : Any ) -> TypeGuard [partial | partialmethod ]:
238+ def ispartial (obj : Any ) -> TypeIs [partial | partialmethod ]:
239239 """Check if the object is a partial function or method."""
240240 return isinstance (obj , (partial , partialmethod ))
241241
@@ -244,7 +244,7 @@ def isclassmethod(
244244 obj : Any ,
245245 cls : Any = None ,
246246 name : str | None = None ,
247- ) -> TypeGuard [classmethod ]:
247+ ) -> TypeIs [classmethod ]:
248248 """Check if the object is a :class:`classmethod`."""
249249 if isinstance (obj , classmethod ):
250250 return True
@@ -264,7 +264,7 @@ def isstaticmethod(
264264 obj : Any ,
265265 cls : Any = None ,
266266 name : str | None = None ,
267- ) -> TypeGuard [staticmethod ]:
267+ ) -> TypeIs [staticmethod ]:
268268 """Check if the object is a :class:`staticmethod`."""
269269 if isinstance (obj , staticmethod ):
270270 return True
@@ -278,7 +278,7 @@ def isstaticmethod(
278278 return False
279279
280280
281- def isdescriptor (x : Any ) -> TypeGuard [_SupportsGet | _SupportsSet | _SupportsDelete ]:
281+ def isdescriptor (x : Any ) -> TypeIs [_SupportsGet | _SupportsSet | _SupportsDelete ]:
282282 """Check if the object is a :external+python:term:`descriptor`."""
283283 return any (
284284 callable (safe_getattr (x , item , None )) for item in ('__get__' , '__set__' , '__delete__' )
@@ -345,12 +345,12 @@ def is_singledispatch_function(obj: Any) -> bool:
345345 )
346346
347347
348- def is_singledispatch_method (obj : Any ) -> TypeGuard [singledispatchmethod ]:
348+ def is_singledispatch_method (obj : Any ) -> TypeIs [singledispatchmethod ]:
349349 """Check if the object is a :class:`~functools.singledispatchmethod`."""
350350 return isinstance (obj , singledispatchmethod )
351351
352352
353- def isfunction (obj : Any ) -> TypeGuard [types .FunctionType ]:
353+ def isfunction (obj : Any ) -> TypeIs [types .FunctionType ]:
354354 """Check if the object is a user-defined function.
355355
356356 Partial objects are unwrapped before checking them.
@@ -360,7 +360,7 @@ def isfunction(obj: Any) -> TypeGuard[types.FunctionType]:
360360 return inspect .isfunction (unpartial (obj ))
361361
362362
363- def isbuiltin (obj : Any ) -> TypeGuard [types .BuiltinFunctionType ]:
363+ def isbuiltin (obj : Any ) -> TypeIs [types .BuiltinFunctionType ]:
364364 """Check if the object is a built-in function or method.
365365
366366 Partial objects are unwrapped before checking them.
@@ -370,7 +370,7 @@ def isbuiltin(obj: Any) -> TypeGuard[types.BuiltinFunctionType]:
370370 return inspect .isbuiltin (unpartial (obj ))
371371
372372
373- def isroutine (obj : Any ) -> TypeGuard [_RoutineType ]:
373+ def isroutine (obj : Any ) -> TypeIs [_RoutineType ]:
374374 """Check if the object is a kind of function or method.
375375
376376 Partial objects are unwrapped before checking them.
@@ -380,7 +380,7 @@ def isroutine(obj: Any) -> TypeGuard[_RoutineType]:
380380 return inspect .isroutine (unpartial (obj ))
381381
382382
383- def iscoroutinefunction (obj : Any ) -> TypeGuard [Callable [..., types .CoroutineType ]]:
383+ def iscoroutinefunction (obj : Any ) -> TypeIs [Callable [..., types .CoroutineType ]]:
384384 """Check if the object is a :external+python:term:`coroutine` function."""
385385 obj = unwrap_all (obj , stop = _is_wrapped_coroutine )
386386 return inspect .iscoroutinefunction (obj )
@@ -395,12 +395,12 @@ def _is_wrapped_coroutine(obj: Any) -> bool:
395395 return hasattr (obj , '__wrapped__' )
396396
397397
398- def isproperty (obj : Any ) -> TypeGuard [property | cached_property ]:
398+ def isproperty (obj : Any ) -> TypeIs [property | cached_property ]:
399399 """Check if the object is property (possibly cached)."""
400400 return isinstance (obj , (property , cached_property ))
401401
402402
403- def isgenericalias (obj : Any ) -> TypeGuard [types .GenericAlias ]:
403+ def isgenericalias (obj : Any ) -> TypeIs [types .GenericAlias ]:
404404 """Check if the object is a generic alias."""
405405 return isinstance (obj , (types .GenericAlias , typing ._BaseGenericAlias )) # type: ignore[attr-defined]
406406
0 commit comments