1# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#instantiating-generic-classes-and-type-erasure
3from typing import Any, TypeVar, Generic, assert_type
7# > If the constructor (__init__ or __new__) uses T in its signature, and a
8# > corresponding argument value is passed, the type of the corresponding
9# > argument(s) is substituted. Otherwise, Any is assumed.
11class Node(Generic[T]):
13 def __init__(self, label: T | None = None) -> None:
17assert_type(Node(''), Node[str])
18assert_type(Node(0), Node[int])
19assert_type(Node(), Node[Any])
21assert_type(Node(0).label, int)
22assert_type(Node().label, Any)
24# > In case the inferred type uses [Any] but the intended type is more specific,
25# > you can use an annotation to force the type of the variable, e.g.:
28assert_type(n1, Node[int])
30assert_type(n2, Node[str])
33assert_type(n3, Node[int])
35assert_type(n4, Node[str])
[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `int | None`, found `Literal[""]`
39n7 = Node[str]("") # OK
[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `str | None`, found `Literal[0]`
42Node[int].label = 1 # E
Expected a ty diagnostic for this line
Expected a ty diagnostic for this line
Expected a ty diagnostic for this line
Expected a ty diagnostic for this line
Expected a ty diagnostic for this line
47assert_type(n1.label, int)
48assert_type(Node[int]().label, int)
51# > [...] generic versions of concrete collections can be instantiated:
53from typing import DefaultDict
55data = DefaultDict[int, bytes]()
56assert_type(data[0], bytes)