-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathgenerics_defaults.py
More file actions
173 lines (105 loc) · 5.47 KB
/
generics_defaults.py
File metadata and controls
173 lines (105 loc) · 5.47 KB
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
"""
Tests for basic usage of default values for TypeVar-like's.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#defaults-for-type-parameters.
from typing import Any, Callable, Generic, Self, Unpack, assert_type
from typing_extensions import TypeVar, ParamSpec, TypeVarTuple
DefaultStrT = TypeVar("DefaultStrT", default=str)
DefaultIntT = TypeVar("DefaultIntT", default=int)
DefaultBoolT = TypeVar("DefaultBoolT", default=bool)
T = TypeVar("T")
T1 = TypeVar("T1")
T2 = TypeVar("T2")
# > The order for defaults should follow the standard function parameter
# > rules, so a type parameter with no ``default`` cannot follow one with
# > a ``default`` value. Doing so may raise a ``TypeError`` at runtime,
# > and a type checker should flag this as an error.
class NonDefaultFollowsDefault(Generic[DefaultStrT, T]): ... # E: non-default TypeVars cannot follow ones with defaults
class NoNonDefaults(Generic[DefaultStrT, DefaultIntT]): ...
assert_type(NoNonDefaults, type[NoNonDefaults[str, int]])
assert_type(NoNonDefaults[str], type[NoNonDefaults[str, int]])
assert_type(NoNonDefaults[str, int], type[NoNonDefaults[str, int]])
class OneDefault(Generic[T, DefaultBoolT]): ...
assert_type(OneDefault[float], type[OneDefault[float, bool]])
assert_type(OneDefault[float](), OneDefault[float, bool])
class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): ...
assert_type(AllTheDefaults, type[AllTheDefaults[Any, Any, str, int, bool]])
assert_type(
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
)
AllTheDefaults[int] # E: expected 2 arguments to AllTheDefaults
assert_type(
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
)
assert_type(
AllTheDefaults[int, complex, str],
type[AllTheDefaults[int, complex, str, int, bool]],
)
assert_type(
AllTheDefaults[int, complex, str, int],
type[AllTheDefaults[int, complex, str, int, bool]],
)
assert_type(
AllTheDefaults[int, complex, str, int, bool],
type[AllTheDefaults[int, complex, str, int, bool]],
)
# > ``ParamSpec`` defaults are defined using the same syntax as
# > ``TypeVar`` \ s but use a ``list`` of types or an ellipsis
# > literal "``...``" or another in-scope ``ParamSpec``.
DefaultP = ParamSpec("DefaultP", default=[str, int])
class Class_ParamSpec(Generic[DefaultP]): ...
assert_type(Class_ParamSpec, type[Class_ParamSpec[str, int]])
assert_type(Class_ParamSpec(), Class_ParamSpec[str, int])
assert_type(Class_ParamSpec[[bool, bool]](), Class_ParamSpec[bool, bool])
# > ``TypeVarTuple`` defaults are defined using the same syntax as
# > ``TypeVar`` \ s, but instead of a single type, they use an unpacked tuple
# > of types or an unpacked, in-scope ``TypeVarTuple`` (see `Scoping Rules`_).
DefaultTs = TypeVarTuple("DefaultTs", default=Unpack[tuple[str, int]])
class Class_TypeVarTuple(Generic[*DefaultTs]): ...
assert_type(Class_TypeVarTuple, type[Class_TypeVarTuple[*tuple[str, int]]])
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])
AnotherDefaultTs = TypeVarTuple("AnotherDefaultTs", default=Unpack[DefaultTs])
# > If both ``bound`` and ``default`` are passed, ``default`` must be a
# > subtype of ``bound``. If not, the type checker should generate an
# > error.
Ok1 = TypeVar("Ok1", bound=float, default=int) # OK
Invalid1 = TypeVar("Invalid1", bound=str, default=int) # E: the bound and default are incompatible
# > For constrained ``TypeVar``\ s, the default needs to be one of the
# > constraints. A type checker should generate an error even if it is a
# > subtype of one of the constraints.
Ok2 = TypeVar("Ok2", float, str, default=float) # OK
Invalid2 = TypeVar("Invalid2", float, str, default=int) # E: expected one of float or str got int
# > In generic functions, type checkers may use a type parameter's default when the
# > type parameter cannot be solved to anything. We leave the semantics of this
# > usage unspecified, as ensuring the ``default`` is returned in every code path
# > where the type parameter can go unsolved may be too hard to implement. Type
# > checkers are free to either disallow this case or experiment with implementing
# > support.
T4 = TypeVar("T4", default=int)
def func1(x: int | set[T4]) -> T4:
raise NotImplementedError
assert_type(func1(0), int) # E[optional-default-use]
assert_type(func1(0), Any) # E[optional-default-use]
# > A ``TypeVar`` that immediately follows a ``TypeVarTuple`` is not allowed
# > to have a default, because it would be ambiguous whether a type argument
# > should be bound to the ``TypeVarTuple`` or the defaulted ``TypeVar``.
Ts = TypeVarTuple("Ts")
T5 = TypeVar("T5", default=bool)
class Foo5(Generic[*Ts, T5]): ... # E
# > It is allowed to have a ``ParamSpec`` with a default following a
# > ``TypeVarTuple`` with a default, as there can be no ambiguity between a
# > type argument for the ``ParamSpec`` and one for the ``TypeVarTuple``.
P = ParamSpec("P", default=[float, bool])
class Foo6(Generic[*Ts, P]): ... # OK
assert_type(Foo6[int, str], type[Foo6[int, str, [float, bool]]])
assert_type(Foo6[int, str, [bytes]], type[Foo6[int, str, [bytes]]])
# > Type parameter defaults should be bound by attribute access
# > (including call and subscript).
class Foo7(Generic[DefaultIntT]):
def meth(self, /) -> Self:
return self
attr: DefaultIntT
foo7 = Foo7()
assert_type(Foo7.meth(foo7), Foo7[int])
assert_type(Foo7().attr, int)