1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
import sys
import typing
import pytest
from typing_inspect import (
is_generic_type, is_callable_type, is_new_type, is_tuple_type, is_union_type,
is_optional_type, is_final_type, is_literal_type, is_typevar, is_classvar,
is_forward_ref, get_origin, get_parameters, get_last_args, get_args, get_bound,
get_constraints, get_generic_type, get_generic_bases, get_last_origin,
typed_dict_keys, get_forward_arg, WITH_FINAL, WITH_LITERAL, LEGACY_TYPING, WITH_NEWTYPE,
)
from unittest import TestCase, main, skipIf, skipUnless
from typing import (
Union, Callable, Optional, TypeVar, Sequence, AnyStr, Mapping,
MutableMapping, Iterable, Generic, List, Any, Dict, Tuple, NamedTuple,
)
from typing import T as typing_T
from mypy_extensions import TypedDict as METypedDict
from typing_extensions import TypedDict as TETypedDict
from typing_extensions import Final
from typing_extensions import Literal
from typing_extensions import NewType as NewType_
# Does this raise an exception ?
# from typing import ClassVar
if sys.version_info < (3, 5, 3):
WITH_CLASSVAR = False
CLASSVAR_GENERIC = []
CLASSVAR_TYPEVAR = []
else:
from typing import ClassVar
WITH_CLASSVAR = True
CLASSVAR_GENERIC = [ClassVar[List[int]], ClassVar]
CLASSVAR_TYPEVAR = [ClassVar[int]]
# Does this raise an exception ?
# class Foo(Callable[[int], int]):
# pass
if sys.version_info < (3, 5, 3):
SUBCLASSABLE_CALLABLES = False
else:
SUBCLASSABLE_CALLABLES = True
# Does this raise an exception ?
# class MyClass(Tuple[str, int]):
# pass
if sys.version_info < (3, 5, 3):
SUBCLASSABLE_TUPLES = False
else:
SUBCLASSABLE_TUPLES = True
# Does this raise an exception ?
# T = TypeVar('T')
# Union[T, str][int]
if sys.version_info < (3, 5, 3):
EXISTING_UNIONS_SUBSCRIPTABLE = False
else:
EXISTING_UNIONS_SUBSCRIPTABLE = True
# Does this raise an exception ?
# Union[callable, Callable[..., int]]
if sys.version_info[:3] == (3, 5, 3) or sys.version_info[:3] < (3, 5, 2):
UNION_SUPPORTS_BUILTIN_CALLABLE = False
else:
UNION_SUPPORTS_BUILTIN_CALLABLE = True
# Does this raise an exception ?
# Tuple[T][int]
# List[Tuple[T]][int]
if sys.version_info[:3] == (3, 5, 3) or sys.version_info[:3] < (3, 5, 2):
GENERIC_TUPLE_PARAMETRIZABLE = False
else:
GENERIC_TUPLE_PARAMETRIZABLE = True
# Does this raise an exception ?
# Dict[T, T][int]
# Dict[int, Tuple[T, T]][int]
if sys.version_info[:3] == (3, 5, 3) or sys.version_info[:3] < (3, 5, 2):
GENERIC_WITH_MULTIPLE_IDENTICAL_PARAMETERS_CAN_BE_PARAMETRIZED_ONCE = False
else:
GENERIC_WITH_MULTIPLE_IDENTICAL_PARAMETERS_CAN_BE_PARAMETRIZED_ONCE = True
# Does this raise an exception ?
# Callable[[T], int][int]
# Callable[[], T][int]
if sys.version_info[:3] < (3, 5, 4):
CALLABLE_CAN_BE_PARAMETRIZED = False
else:
CALLABLE_CAN_BE_PARAMETRIZED = True
NEW_TYPING = sys.version_info[:3] >= (3, 7, 0) # PEP 560
PY36_TESTS = """
class TDM(METypedDict):
x: int
y: int
class TDE(TETypedDict):
x: int
y: int
class Other(dict):
x: int
y: int
"""
PY36 = sys.version_info[:3] >= (3, 6, 0)
PY39 = sys.version_info[:3] >= (3, 9, 0)
if PY36:
exec(PY36_TESTS)
# It is important for the test that this function is called 'NewType' to simulate the same __qualname__
# - which is "NewType.<locals>.new_type" - as typing.NewType has, i.e. it should be checked that is_new_type
# still do not accept a function which has the same __qualname__ and an attribute called __supertype__.
def NewType(name, tp):
def new_type(x):
return x
new_type.__name__ = name
new_type.__supertype__ = tp
return new_type
class IsUtilityTestCase(TestCase):
def sample_test(self, fun, samples, nonsamples):
msg = "Error asserting that %s(%s) is %s"
for s in samples:
self.assertTrue(fun(s), msg=msg % (fun.__name__, str(s), 'True'))
for s in nonsamples:
self.assertFalse(fun(s), msg=msg % (fun.__name__, str(s), 'False'))
def test_generic(self):
T = TypeVar('T')
samples = [Generic, Generic[T], Iterable[int], Mapping,
MutableMapping[T, List[int]], Sequence[Union[str, bytes]]]
if PY39:
samples.extend([list[int], dict[str, list[int]]])
nonsamples = [int, Union[int, str], Union[int, T], Callable[..., T],
Optional, bytes, list] + CLASSVAR_GENERIC
self.sample_test(is_generic_type, samples, nonsamples)
def test_callable(self):
samples = [Callable, Callable[..., int],
Callable[[int, int], Iterable[str]]]
nonsamples = [int, type, 42, [], List[int]]
if UNION_SUPPORTS_BUILTIN_CALLABLE:
nonsamples.append(Union[callable, Callable[..., int]])
self.sample_test(is_callable_type, samples, nonsamples)
if SUBCLASSABLE_CALLABLES:
class MyClass(Callable[[int], int]):
pass
self.assertTrue(is_callable_type(MyClass))
def test_tuple(self):
samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]]
if PY39:
samples.append(tuple[int, str])
nonsamples = [int, tuple, 42, List[int], NamedTuple('N', [('x', int)])]
self.sample_test(is_tuple_type, samples, nonsamples)
if SUBCLASSABLE_TUPLES:
class MyClass(Tuple[str, int]):
pass
self.assertTrue(is_tuple_type(MyClass))
def test_union(self):
T = TypeVar('T')
S = TypeVar('S')
samples = [Union, Union[T, int], Union[int, Union[T, S]]]
nonsamples = [int, Union[int, int], [], Iterable[Any]]
self.sample_test(is_union_type, samples, nonsamples)
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires 3.10 or higher")
def test_union_pep604(self):
T = TypeVar('T')
S = TypeVar('S')
samples = [T | int, int | (T | S), int | str]
nonsamples = [int, int | int, [], Iterable[Any]]
self.sample_test(is_union_type, samples, nonsamples)
def test_optional_type(self):
T = TypeVar('T')
samples = [type(None), # none type
Optional[int], # direct union to none type 1
Optional[T], # direct union to none type 2
Union[int, type(None)], # direct union to none type 4
]
if EXISTING_UNIONS_SUBSCRIPTABLE:
samples += [Optional[T][int], # direct union to none type 3
Union[str, T][type(None)] # direct union to none type 5
]
# nested unions are supported
samples += [Union[str, Optional[int]]] # nested Union 1
if EXISTING_UNIONS_SUBSCRIPTABLE:
samples += [Union[T, str][Optional[int]]] # nested Union 2
nonsamples = [int, Union[int, int], [], Iterable[Any], T]
if EXISTING_UNIONS_SUBSCRIPTABLE:
nonsamples += [Union[T, str][int]]
# unfortunately current definition sets these ones as non samples too
S1 = TypeVar('S1', bound=Optional[int])
S2 = TypeVar('S2', type(None), str)
S3 = TypeVar('S3', Optional[int], str)
S4 = TypeVar('S4', bound=Union[str, Optional[int]])
nonsamples += [S1, S2, S3, # typevar bound or constrained to optional
Union[S1, int], S4 # combinations of the above
]
self.sample_test(is_optional_type, samples, nonsamples)
@skipIf(not WITH_FINAL, "Final is not available")
def test_final_type(self):
samples = [
Final,
Final[int],
]
nonsamples = [
"v",
1,
(1, 2, 3),
int,
str,
Union["u", "v"],
]
self.sample_test(is_final_type, samples, nonsamples)
@skipIf(not WITH_LITERAL, "Literal is not available")
def test_literal_type(self):
samples = [
Literal,
Literal["v"],
Literal[1, 2, 3],
]
if hasattr(typing, "Literal"):
samples += [
typing.Literal,
typing.Literal["v"],
typing.Literal[1, 2, 3],
]
nonsamples = [
"v",
(1, 2, 3),
int,
str,
Union["u", "v"],
]
self.sample_test(is_literal_type, samples, nonsamples)
def test_typevar(self):
T = TypeVar('T')
S_co = TypeVar('S_co', covariant=True)
samples = [T, S_co]
nonsamples = [int, Union[T, int], Union[T, S_co], type] + CLASSVAR_TYPEVAR
self.sample_test(is_typevar, samples, nonsamples)
@skipIf(not WITH_CLASSVAR, "ClassVar is not present")
def test_classvar(self):
T = TypeVar('T')
samples = [ClassVar, ClassVar[int], ClassVar[List[T]]]
nonsamples = [int, 42, Iterable, List[int], type, T]
self.sample_test(is_classvar, samples, nonsamples)
@skipIf(not WITH_NEWTYPE, "NewType is not present")
def test_new_type(self):
T = TypeVar('T')
class WithAttrSuperTypeCls:
__supertype__ = str
class WithAttrSuperTypeObj:
def __init__(self):
self.__supertype__ = str
samples = [
NewType_,
NewType_('A', int),
NewType_('B', complex),
NewType_('C', List[int]),
NewType_('D', Union['p', 'y', 't', 'h', 'o', 'n']),
NewType_('E', List[Dict[str, float]]),
NewType_('F', NewType('F_', int)),
]
nonsamples = [
int,
42,
Iterable,
List[int],
Union["u", "v"],
type,
T,
NewType,
NewType('N', int),
WithAttrSuperTypeCls,
WithAttrSuperTypeCls(),
WithAttrSuperTypeObj,
WithAttrSuperTypeObj(),
]
self.sample_test(is_new_type, samples, nonsamples)
def test_is_forward_ref(self):
samples = []
nonsamples = []
for tp in (
Union["FowardReference", Dict[str, List[int]]],
Union["FR", List["FR"]],
Optional["Fref"],
Union["fRef", int],
Union["fR", AnyStr],
):
fr, not_fr = get_args(tp)
samples.append(fr)
nonsamples.append(not_fr)
self.sample_test(is_forward_ref, samples, nonsamples)
class GetUtilityTestCase(TestCase):
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
def test_last_origin(self):
T = TypeVar('T')
self.assertEqual(get_last_origin(int), None)
if WITH_CLASSVAR:
self.assertEqual(get_last_origin(ClassVar[int]), None)
self.assertEqual(get_last_origin(Generic[T]), Generic)
if EXISTING_UNIONS_SUBSCRIPTABLE:
self.assertEqual(get_last_origin(Union[T, int][str]), Union[T, int])
if GENERIC_TUPLE_PARAMETRIZABLE:
tp = List[Tuple[T, T]][int]
self.assertEqual(get_last_origin(tp), List[Tuple[T, T]])
self.assertEqual(get_last_origin(List), List)
def test_origin(self):
T = TypeVar('T')
self.assertEqual(get_origin(int), None)
if WITH_CLASSVAR:
self.assertEqual(get_origin(ClassVar[int]), None)
self.assertEqual(get_origin(Generic), Generic)
self.assertEqual(get_origin(Generic[T]), Generic)
# Cannot use assertEqual on Py3.5.2.
self.assertIs(get_origin(Literal[42]), Literal)
if hasattr(typing, "Literal"):
self.assertIs(get_origin(typing.Literal[42]), typing.Literal)
if PY39:
self.assertEqual(get_origin(list[int]), list)
if GENERIC_TUPLE_PARAMETRIZABLE:
tp = List[Tuple[T, T]][int]
self.assertEqual(get_origin(tp), list if NEW_TYPING else List)
def test_parameters(self):
T = TypeVar('T')
S_co = TypeVar('S_co', covariant=True)
U = TypeVar('U')
self.assertEqual(get_parameters(int), ())
self.assertEqual(get_parameters(Generic), ())
self.assertEqual(get_parameters(Union), ())
if not LEGACY_TYPING:
self.assertEqual(get_parameters(List[int]), ())
if PY39:
self.assertEqual(get_parameters(List), ())
else:
self.assertEqual(get_parameters(List), (typing_T,))
else:
# in 3.5.3 a list has no __args__ and instead they are used in __parameters__
# in 3.5.1 the behaviour is normal again.
pass
self.assertEqual(get_parameters(Generic[T]), (T,))
self.assertEqual(get_parameters(Tuple[List[T], List[S_co]]), (T, S_co))
if EXISTING_UNIONS_SUBSCRIPTABLE:
self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,))
self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co))
if PY39:
self.assertEqual(get_parameters(dict[int, T]), (T,))
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
def test_last_args(self):
T = TypeVar('T')
S = TypeVar('S')
self.assertEqual(get_last_args(int), ())
self.assertEqual(get_last_args(Union), ())
if WITH_CLASSVAR:
self.assertEqual(get_last_args(ClassVar[int]), (int,))
self.assertEqual(get_last_args(Union[T, int]), (T, int))
self.assertEqual(get_last_args(Union[str, int]), (str, int))
self.assertEqual(get_last_args(Tuple[T, int]), (T, int))
self.assertEqual(get_last_args(Tuple[str, int]), (str, int))
self.assertEqual(get_last_args(Generic[T]), (T, ))
if GENERIC_TUPLE_PARAMETRIZABLE:
tp = Iterable[Tuple[T, S]][int, T]
self.assertEqual(get_last_args(tp), (int, T))
if LEGACY_TYPING:
self.assertEqual(get_last_args(Callable[[T, S], int]), (T, S))
self.assertEqual(get_last_args(Callable[[], int]), ())
else:
self.assertEqual(get_last_args(Callable[[T, S], int]), (T, S, int))
self.assertEqual(get_last_args(Callable[[], int]), (int,))
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
def test_args(self):
if EXISTING_UNIONS_SUBSCRIPTABLE:
T = TypeVar('T')
self.assertEqual(get_args(Union[int, Tuple[T, int]][str]),
(int, (Tuple, str, int)))
self.assertEqual(get_args(Union[int, Union[T, int], str][int]),
(int, str))
self.assertEqual(get_args(int), ())
def test_args_evaluated(self):
T = TypeVar('T')
if EXISTING_UNIONS_SUBSCRIPTABLE:
self.assertEqual(get_args(Union[int, Tuple[T, int]][str], evaluate=True),
(int, Tuple[str, int]))
if GENERIC_WITH_MULTIPLE_IDENTICAL_PARAMETERS_CAN_BE_PARAMETRIZED_ONCE:
tp = Dict[int, Tuple[T, T]][Optional[int]]
self.assertEqual(get_args(tp, evaluate=True),
(int, Tuple[Optional[int], Optional[int]]))
if CALLABLE_CAN_BE_PARAMETRIZED:
tp = Callable[[], T][int]
self.assertEqual(get_args(tp, evaluate=True), ([], int,))
self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]], evaluate=True),
(int, Callable[[Tuple[T, ...]], str]))
# ClassVar special-casing
if WITH_CLASSVAR:
self.assertEqual(get_args(ClassVar, evaluate=True), ())
self.assertEqual(get_args(ClassVar[int], evaluate=True), (int,))
# Final special-casing
if WITH_FINAL:
self.assertEqual(get_args(Final, evaluate=True), ())
self.assertEqual(get_args(Final[int], evaluate=True), (int,))
# Literal special-casing
if WITH_LITERAL:
self.assertEqual(get_args(Literal, evaluate=True), ())
self.assertEqual(get_args(Literal["value"], evaluate=True), ("value",))
self.assertEqual(get_args(Literal[1, 2, 3], evaluate=True), (1, 2, 3))
if hasattr(typing, "Literal"):
self.assertEqual(get_args(typing.Literal, evaluate=True), ())
self.assertEqual(get_args(typing.Literal["value"], evaluate=True), ("value",))
self.assertEqual(get_args(typing.Literal[1, 2, 3], evaluate=True), (1, 2, 3))
if PY39:
self.assertEqual(get_args(list[int]), (int,))
self.assertEqual(get_args(tuple[int, str]), (int, str))
self.assertEqual(get_args(list[list[int]]), (list[int],))
# This would return (~T,) before Python 3.9.
self.assertEqual(get_args(List), ())
if sys.version_info >= (3, 10):
self.assertEqual(get_args(int | str), (int, str))
self.assertEqual(get_args((int | tuple[T, int])[str]), (int, tuple[str, int]))
def test_bound(self):
T = TypeVar('T')
TB = TypeVar('TB', bound=int)
self.assertEqual(get_bound(T), None)
self.assertEqual(get_bound(TB), int)
def test_constraints(self):
T = TypeVar('T')
TC = TypeVar('TC', int, str)
self.assertEqual(get_constraints(T), ())
self.assertEqual(get_constraints(TC), (int, str))
def test_generic_type(self):
T = TypeVar('T')
class Node(Generic[T]): pass
self.assertIs(get_generic_type(Node()), Node)
if not LEGACY_TYPING:
self.assertIs(get_generic_type(Node[int]()), Node[int])
self.assertIs(get_generic_type(Node[T]()), Node[T],)
else:
# Node[int]() was creating an object of NEW type Node[~T]
# and Node[T]() was creating an object of NEW type Node[~T]
pass
self.assertIs(get_generic_type(1), int)
def test_generic_bases(self):
class MyClass(List[int], Mapping[str, List[int]]): pass
self.assertEqual(get_generic_bases(MyClass),
(List[int], Mapping[str, List[int]]))
self.assertEqual(get_generic_bases(int), ())
@skipUnless(PY36, "Python 3.6 required")
def test_typed_dict_mypy_extension(self):
TDOld = METypedDict("TDOld", {'x': int, 'y': int})
self.assertEqual(typed_dict_keys(TDM), {'x': int, 'y': int})
self.assertEqual(typed_dict_keys(TDOld), {'x': int, 'y': int})
self.assertIs(typed_dict_keys(dict), None)
self.assertIs(typed_dict_keys(Other), None)
self.assertIsNot(typed_dict_keys(TDM), TDM.__annotations__)
@skipUnless(PY36, "Python 3.6 required")
def test_typed_dict_typing_extension(self):
TDOld = TETypedDict("TDOld", {'x': int, 'y': int})
self.assertEqual(typed_dict_keys(TDE), {'x': int, 'y': int})
self.assertEqual(typed_dict_keys(TDOld), {'x': int, 'y': int})
self.assertIs(typed_dict_keys(dict), None)
self.assertIs(typed_dict_keys(Other), None)
self.assertIsNot(typed_dict_keys(TDE), TDE.__annotations__)
@skipIf(
(3, 5, 2) > sys.version_info[:3] >= (3, 5, 0),
"get_args doesn't work in Python 3.5.0 and 3.5.1 for type"
" List and ForwardRef arg"
)
def test_get_forward_arg(self):
tp = List["FRef"]
fr = get_args(tp)[0]
self.assertEqual(get_forward_arg(fr), "FRef")
self.assertEqual(get_forward_arg(tp), None)
if __name__ == '__main__':
main()
|