|
1 | 1 | from types import GenericAlias |
2 | | -from typing import Any |
3 | 2 |
|
4 | | -from opyoid.type_checker.pep560_type_checker import Pep560TypeChecker |
| 3 | +# noinspection PyProtectedMember |
| 4 | +from typing import _GenericAlias, Any, Union # type: ignore[attr-defined] |
5 | 5 |
|
| 6 | +from opyoid.named import Named |
| 7 | +from opyoid.provider import Provider |
6 | 8 |
|
7 | | -class Pep585TypeChecker(Pep560TypeChecker): |
| 9 | + |
| 10 | +class Pep585TypeChecker: |
8 | 11 | """Various helpers to check type hints.""" |
9 | 12 |
|
10 | 13 | @staticmethod |
11 | 14 | def is_list(target_type: Any) -> bool: |
12 | | - """Returns True if target_type is List[<Any>] or list[<Any>]""" |
13 | | - return Pep560TypeChecker.is_list(target_type) or ( |
14 | | - isinstance(target_type, GenericAlias) and target_type.__origin__ == list |
15 | | - ) |
| 15 | + """Returns True if target_type is List[<Any>] or list[Any]""" |
| 16 | + return isinstance(target_type, (_GenericAlias, GenericAlias)) and bool(target_type.__origin__ == list) |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def is_pep585_list(target_type: Any) -> bool: |
| 20 | + """Returns True if target_type is list[<Any>]""" |
| 21 | + return isinstance(target_type, GenericAlias) and bool(target_type.__origin__ == list) |
16 | 22 |
|
17 | 23 | @staticmethod |
18 | 24 | def is_set(target_type: Any) -> bool: |
19 | | - """Returns True if target_type is Set[<Any>] or set[<Any>]""" |
20 | | - return Pep560TypeChecker.is_set(target_type) or ( |
21 | | - isinstance(target_type, GenericAlias) and target_type.__origin__ == set |
22 | | - ) |
| 25 | + """Returns True if target_type is Set[<Any>]""" |
| 26 | + return isinstance(target_type, (_GenericAlias, GenericAlias)) and bool(target_type.__origin__ == set) |
23 | 27 |
|
24 | 28 | @staticmethod |
25 | 29 | def is_tuple(target_type: Any) -> bool: |
26 | | - """Returns True if target_type is Tuple[<Any>] or tuple[<Any>]""" |
27 | | - return Pep560TypeChecker.is_tuple(target_type) or ( |
28 | | - isinstance(target_type, GenericAlias) and target_type.__origin__ == tuple |
29 | | - ) |
| 30 | + """Returns True if target_type is Tuple[<Any>]""" |
| 31 | + return isinstance(target_type, (_GenericAlias, GenericAlias)) and bool(target_type.__origin__ == tuple) |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def is_provider(target_type: Any) -> bool: |
| 35 | + """Returns True if target_type is Provider[<Any>]""" |
| 36 | + return isinstance(target_type, _GenericAlias) and target_type.__origin__ == Provider |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def is_named(target_type: Any) -> bool: |
| 40 | + """Returns True if target_type is Named[<Any>]""" |
| 41 | + return isinstance(target_type, type) and issubclass(target_type, Named) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def is_union(target_type: Any) -> bool: |
| 45 | + """Returns True if target_type is Union[<Any>, <Any>...] or Optional[<Any>]""" |
| 46 | + return isinstance(target_type, _GenericAlias) and target_type.__origin__ == Union |
30 | 47 |
|
31 | 48 | @staticmethod |
32 | 49 | def is_type(target_type: Any) -> bool: |
33 | 50 | """Returns True if target_type is Type[<Any>]""" |
34 | | - return Pep560TypeChecker.is_type(target_type) or ( |
35 | | - isinstance(target_type, GenericAlias) and target_type.__origin__ == type |
36 | | - ) |
| 51 | + return isinstance(target_type, (_GenericAlias, GenericAlias)) and bool(target_type.__origin__ == type) |
0 commit comments