Test if a given annotation is a class that can be used in isinstance()/issubclass().
(annotation: typing.Any)
| 35 | |
| 36 | |
| 37 | def annotation_is_class(annotation: typing.Any) -> bool: |
| 38 | """Test if a given annotation is a class that can be used in isinstance()/issubclass().""" |
| 39 | # isclass() returns True for generic type hints (e.g. `list[str]`) until Python 3.10. |
| 40 | # NOTE: The guard for Python 3.9 is because types.GenericAlias is only added in Python 3.9. This is not a problem |
| 41 | # as the syntax is added in the same version in the first place. |
| 42 | if (3, 9) <= sys.version_info < (3, 11) and isinstance(annotation, types.GenericAlias): |
| 43 | return False |
| 44 | return isclass(annotation) |
| 45 | |
| 46 | |
| 47 | def annotation_issubclass(annotation: typing.Any, cls: type) -> bool: |
no outgoing calls
no test coverage detected