| 1 |
import collections |
| 2 |
import sys |
| 3 |
import types |
| 4 |
import typing |
| 5 |
import warnings |
| 6 |
from collections.abc import Callable |
| 7 |
from typing import Any, ForwardRef, NewType, ParamSpecArgs, ParamSpecKwargs, TypeAlias, TypeVar |
| 8 |
|
| 9 |
import pytest |
| 10 |
import typing_extensions |
| 11 |
from typing_extensions import ( |
| 12 |
ParamSpec, |
| 13 |
TypeAliasType, |
| 14 |
TypeVarTuple, |
| 15 |
deprecated, |
| 16 |
) |
| 17 |
|
| 18 |
from typing_inspection import typing_objects |
| 19 |
|
| 20 |
CheckFunction: TypeAlias = Callable[[Any], bool] |
| 21 |
|
| 22 |
|
| 23 |
names_and_functions: list[tuple[str, CheckFunction]] = [ |
| 24 |
('Annotated', typing_objects.is_annotated), |
| 25 |
('Any', typing_objects.is_any), |
| 26 |
('ClassVar', typing_objects.is_classvar), |
| 27 |
('Concatenate', typing_objects.is_concatenate), |
| 28 |
('Final', typing_objects.is_final), |
| 29 |
('Generic', typing_objects.is_generic), |
| 30 |
('Literal', typing_objects.is_literal), |
| 31 |
('LiteralString', typing_objects.is_literalstring), |
| 32 |
('Never', typing_objects.is_never), |
| 33 |
('NoDefault', typing_objects.is_nodefault), |
| 34 |
('NoExtraItems', typing_objects.is_noextraitems), |
| 35 |
('NoReturn', typing_objects.is_noreturn), |
| 36 |
('NotRequired', typing_objects.is_notrequired), |
| 37 |
('ReadOnly', typing_objects.is_readonly), |
| 38 |
('Required', typing_objects.is_required), |
| 39 |
('Self', typing_objects.is_self), |
| 40 |
('TypeAlias', typing_objects.is_typealias), |
| 41 |
('TypeGuard', typing_objects.is_typeguard), |
| 42 |
('TypeIs', typing_objects.is_typeis), |
| 43 |
('Union', typing_objects.is_union), |
| 44 |
('Unpack', typing_objects.is_unpack), |
| 45 |
] |
| 46 |
|
| 47 |
|
| 48 |
identity_member_checks: list[tuple[CheckFunction, Any, bool]] = [] |
| 49 |
|
| 50 |
for typing_name, function in names_and_functions: |
| 51 |
if hasattr(typing, typing_name): |
| 52 |
identity_member_checks.append((function, getattr(typing, typing_name), True)) |
| 53 |
if hasattr(typing_extensions, typing_name): |
| 54 |
identity_member_checks.append((function, getattr(typing_extensions, typing_name), True)) |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize( |
| 58 |
['function', 'member', 'result'], |
| 59 |
identity_member_checks, |
| 60 |
) |
| 61 |
def test_identity_member_check(function: CheckFunction, member: Any, result: bool) -> None: |
| 62 |
assert function(member) == result |
| 63 |
|
| 64 |
|
| 65 |
class TypingNamedTuple(typing.NamedTuple): |
| 66 |
pass |
| 67 |
|
| 68 |
|
| 69 |
class TypingExtensionsNamedTuple(typing_extensions.NamedTuple): |
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
CollectionsNamedTuple = collections.namedtuple('CollectionsNamedTuple', []) # pyright: ignore[reportUntypedNamedTuple] # noqa: PYI024 |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize('nt_class', [TypingNamedTuple, TypingExtensionsNamedTuple, CollectionsNamedTuple]) |
| 77 |
def test_is_namedtuple(nt_class: type[Any]) -> None: |
| 78 |
assert typing_objects.is_namedtuple(nt_class) |
| 79 |
|
| 80 |
|
| 81 |
TypingNewType = typing.NewType('TypingNewType', int) |
| 82 |
TypingExtensionsNewType = typing.NewType('TypingExtensionsNewType', str) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
'new_type', |
| 87 |
[TypingNewType, TypingExtensionsNewType], |
| 88 |
) |
| 89 |
def test_is_newtype(new_type: NewType) -> None: |
| 90 |
assert typing_objects.is_newtype(new_type) |
| 91 |
|
| 92 |
|
| 93 |
TypingP = typing.ParamSpec('TypingP') |
| 94 |
TypingExtensionsP = typing_extensions.ParamSpec('TypingExtensionsP') |
| 95 |
|
| 96 |
param_specs: list[ParamSpec] = [TypingP, TypingExtensionsP] |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.parametrize( |
| 100 |
'param_spec', |
| 101 |
param_specs, |
| 102 |
) |
| 103 |
def test_is_paramspec(param_spec: ParamSpec) -> None: |
| 104 |
assert typing_objects.is_paramspec(param_spec) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize( |
| 108 |
'param_spec_args', |
| 109 |
[p.args for p in param_specs], |
| 110 |
) |
| 111 |
def test_is_paramspecargs(param_spec_args: ParamSpecArgs) -> None: |
| 112 |
assert typing_objects.is_paramspecargs(param_spec_args) |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize( |
| 116 |
'param_spec_kwargs', |
| 117 |
[p.kwargs for p in param_specs], |
| 118 |
) |
| 119 |
def test_is_paramspeckwargs(param_spec_kwargs: ParamSpecKwargs) -> None: |
| 120 |
assert typing_objects.is_paramspeckwargs(param_spec_kwargs) |
| 121 |
|
| 122 |
|
| 123 |
@pytest.mark.parametrize( |
| 124 |
'forwardref', |
| 125 |
[typing.ForwardRef('T'), typing_extensions.ForwardRef('T')], |
| 126 |
) |
| 127 |
def test_is_forwardref(forwardref: ForwardRef) -> None: |
| 128 |
assert typing_objects.is_forwardref(forwardref) |
| 129 |
|
| 130 |
|
| 131 |
T = TypeVar('T') |
| 132 |
|
| 133 |
TypingExtensionsTypeAliasType = typing_extensions.TypeAliasType('TypingExtensionsTypeAliasType', int, type_params=(T,)) |
| 134 |
|
| 135 |
type_alias_types: list[TypeAliasType] = [TypingExtensionsTypeAliasType] |
| 136 |
if sys.version_info >= (3, 12): |
| 137 |
TypingTypeAliasType = typing_extensions.TypeAliasType('TypingTypeAliasType', int, type_params=(T,)) |
| 138 |
type_alias_types.append(TypingTypeAliasType) |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
'type_alias_type', |
| 143 |
type_alias_types, |
| 144 |
) |
| 145 |
def test_is_typealiastype(type_alias_type: TypeAliasType) -> None: |
| 146 |
assert typing_objects.is_typealiastype(type_alias_type) |
| 147 |
|
| 148 |
# See Python 3.10 comment in the implementation: |
| 149 |
assert not typing_objects.is_typealiastype(type_alias_type[int]) |
| 150 |
|
| 151 |
|
| 152 |
TypingTypeVar = typing.TypeVar('TypingTypeVar') |
| 153 |
TypingExtensionsTypeVar = typing_extensions.TypeVar('TypingExtensionsTypeVar') |
| 154 |
|
| 155 |
|
| 156 |
@pytest.mark.parametrize( |
| 157 |
'type_var', |
| 158 |
[TypingTypeVar, TypingExtensionsTypeVar], |
| 159 |
) |
| 160 |
def test_is_typevar(type_var: TypeVar) -> None: |
| 161 |
assert typing_objects.is_typevar(type_var) |
| 162 |
|
| 163 |
|
| 164 |
TypingExtensionsTypeVarTuple = typing_extensions.TypeVarTuple('TypingExtensionsTypeVarTuple') |
| 165 |
|
| 166 |
type_var_tuples: list[TypeVarTuple] = [TypingExtensionsTypeVarTuple] |
| 167 |
if sys.version_info >= (3, 11): |
| 168 |
TypingTypeVarTuple = typing.TypeVarTuple('TypingTypeVarTuple') |
| 169 |
type_var_tuples.append(TypingTypeVarTuple) |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize( |
| 173 |
'type_var_tuple', |
| 174 |
type_var_tuples, |
| 175 |
) |
| 176 |
def test_is_typevartuple(type_var_tuple: TypeVarTuple) -> None: |
| 177 |
assert typing_objects.is_typevartuple(type_var_tuple) |
| 178 |
|
| 179 |
|
| 180 |
deprecated_types: list[deprecated] = [typing_extensions.deprecated('deprecated')] |
| 181 |
if sys.version_info >= (3, 13): |
| 182 |
deprecated_types.append(warnings.deprecated('deprecated')) |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.parametrize( |
| 186 |
'deprecated', |
| 187 |
deprecated_types, |
| 188 |
) |
| 189 |
def test_is_deprecated(deprecated: deprecated) -> None: |
| 190 |
assert typing_objects.is_deprecated(deprecated) |
| 191 |
|
| 192 |
|
| 193 |
# Misc. tests: |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.skipif( |
| 197 |
sys.version_info < (3, 10) or sys.version_info >= (3, 14), |
| 198 |
reason=( |
| 199 |
'`types.UnionType` is only available in Python 3.10. ' |
| 200 |
'In Python 3.14, `typing.Union` is an alias for `types.UnionType`.' |
| 201 |
), |
| 202 |
) |
| 203 |
def test_is_union_does_not_match_uniontype() -> None: |
| 204 |
assert not typing_objects.is_union(types.UnionType) |