← Back to index

aliases_newtype.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 14
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests the `typing.NewType` type constructor.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/aliases.html#newtype
6
7from typing import Any, Hashable, Literal, NewType, TypeVar, TypedDict, assert_type
8
9UserId = NewType("UserId", int)
11UserId("user") # E: incorrect type
[invalid-argument-type] Argument is incorrect: Expected `int`, found `Literal["user"]`
12u1: UserId = 42 # E: incorrect type
[invalid-assignment] Object of type `Literal[42]` is not assignable to `UserId`
13u2: UserId = UserId(42) # OK
15assert_type(UserId(5) + 1, int)
17# > NewType('Derived', Base) returns a dummy object
18_: type = UserId # E: `NewType()` does not return an instance of `type`
[invalid-assignment] Object of type `<NewType pseudo-class 'UserId'>` is not assignable to `type`
20# > Both ``isinstance`` and ``issubclass``, as well as subclassing will fail
21# > for ``NewType('Derived', Base)``, since the object returned by a call to
22# > ``NewType`` is not a class.
23isinstance(u2, UserId) # E: not allowed in isinstance call
[invalid-argument-type] Argument to function `isinstance` is incorrect: Expected `type | UnionType | tuple[Divergent, ...]`, found `<NewType pseudo-class 'UserId'>`
26class UserIdDerived(UserId): # E: subclassing not allowed
[invalid-base] Cannot subclass an instance of NewType
27 pass
30# > NewType accepts exactly two arguments: a name for the new unique type,
31# > and a base class. The latter should be a proper class (i.e., not a type
32# > construct like Union, etc.), or another unique type created by
33# > calling NewType.
35GoodName = NewType("BadName", int) # E: assigned name does not match
[invalid-newtype] The name of a `NewType` (`BadName`) must match the name of the variable it is assigned to (`GoodName`)
37GoodNewType1 = NewType("GoodNewType1", list) # OK
39GoodNewType2 = NewType("GoodNewType2", GoodNewType1) # OK
41nt1: GoodNewType1[int] # E: NewType cannot be generic
[invalid-type-form] `GoodNewType1` is a `NewType` and cannot be specialized
43TypeAlias1 = dict[str, str]
44GoodNewType3 = NewType("GoodNewType3", TypeAlias1)
47BadNewType1 = NewType("BadNewType1", int | str) # E: cannot be generic
[invalid-newtype] invalid base for `typing.NewType`: type `int | str`
49T = TypeVar("T")
50BadNewType2 = NewType("BadNewType2", list[T]) # E: cannot be generic
[invalid-newtype] invalid base for `typing.NewType`: A `NewType` base cannot be generic
52BadNewType3 = NewType("BadNewType3", Hashable) # E: cannot be protocol
[invalid-newtype] invalid base for `typing.NewType`: type `Hashable`
54BadNewType4 = NewType("BadNewType4", Literal[7]) # E: literal not allowed
[invalid-newtype] invalid base for `typing.NewType`: type `Literal[7]`
57class TD1(TypedDict):
58 a: int
61BadNewType5 = NewType("BadNewType5", TD1) # E: cannot be TypedDict
[invalid-newtype] invalid base for `typing.NewType`: type `TD1`
63BadNewType6 = NewType("BadNewType6", int, int) # E: too many arguments
[invalid-newtype] Wrong number of arguments in `NewType` creation: expected 2, found 3
65BadNewType7 = NewType("BadNewType7", Any) # E: cannot be Any
[invalid-newtype] invalid base for `typing.NewType`: type `Any`