| 1 |
[case testPEP695TypeParameterDefaultSupported] |
| 2 |
class C[T = None]: ... |
| 3 |
def f[T = list[int]]() -> None: ... |
| 4 |
def g[**P = [int, str]]() -> None: ... |
| 5 |
type A[T, S = int, U = str] = list[T] |
| 6 |
|
| 7 |
[case testPEP695TypeParameterDefaultBasic] |
| 8 |
from typing import Callable |
| 9 |
|
| 10 |
def f1[T1 = int](a: T1) -> list[T1]: ... |
| 11 |
reveal_type(f1) # N: Revealed type is "def [T1 = builtins.int] (a: T1`-1 = builtins.int) -> builtins.list[T1`-1 = builtins.int]" |
| 12 |
|
| 13 |
def f2[**P1 = [int, str]](a: Callable[P1, None]) -> Callable[P1, None]: ... |
| 14 |
reveal_type(f2) # N: Revealed type is "def [P1 = [builtins.int, builtins.str]] (a: def (*P1.args, **P1.kwargs)) -> def (*P1.args, **P1.kwargs)" |
| 15 |
|
| 16 |
def f3[*Ts1 = *tuple[int, str]](a: tuple[*Ts1]) -> tuple[*Ts1]: ... |
| 17 |
reveal_type(f3) # N: Revealed type is "def [Ts1 = Unpack[tuple[builtins.int, builtins.str]]] (a: tuple[Unpack[Ts1`-1 = Unpack[tuple[builtins.int, builtins.str]]]]) -> tuple[Unpack[Ts1`-1 = Unpack[tuple[builtins.int, builtins.str]]]]" |
| 18 |
|
| 19 |
|
| 20 |
class ClassA1[T1 = int]: ... |
| 21 |
class ClassA2[**P1 = [int, str]]: ... |
| 22 |
class ClassA3[*Ts1 = *tuple[int, str]]: ... |
| 23 |
|
| 24 |
reveal_type(ClassA1) # N: Revealed type is "def [T1 = builtins.int] () -> __main__.ClassA1[T1`1 = builtins.int]" |
| 25 |
reveal_type(ClassA2) # N: Revealed type is "def [P1 = [builtins.int, builtins.str]] () -> __main__.ClassA2[P1`1 = [builtins.int, builtins.str]]" |
| 26 |
reveal_type(ClassA3) # N: Revealed type is "def [Ts1 = Unpack[tuple[builtins.int, builtins.str]]] () -> __main__.ClassA3[Unpack[Ts1`1 = Unpack[tuple[builtins.int, builtins.str]]]]" |
| 27 |
[builtins fixtures/tuple.pyi] |
| 28 |
|
| 29 |
[case testPEP695TypeParameterDefaultValid] |
| 30 |
from typing import Any |
| 31 |
|
| 32 |
class ClassT1[T = int]: ... |
| 33 |
class ClassT2[T: float = int]: ... |
| 34 |
class ClassT3[T: list[Any] = list[int]]: ... |
| 35 |
class ClassT4[T: (int, str) = int]: ... |
| 36 |
|
| 37 |
class ClassP1[**P = []]: ... |
| 38 |
class ClassP2[**P = ...]: ... |
| 39 |
class ClassP3[**P = [int, str]]: ... |
| 40 |
|
| 41 |
class ClassTs1[*Ts = *tuple[int]]: ... |
| 42 |
class ClassTs2[*Ts = *tuple[int, ...]]: ... |
| 43 |
[builtins fixtures/tuple.pyi] |
| 44 |
|
| 45 |
[case testPEP695TypeParameterDefaultInvalid] |
| 46 |
class ClassT1[T = 2]: ... # E: TypeVar "default" must be a type |
| 47 |
class ClassT2[T = [int]]: ... # E: Bracketed expression "[...]" is not valid as a type \ |
| 48 |
# N: Did you mean "List[...]"? \ |
| 49 |
# E: TypeVar "default" must be a type |
| 50 |
class ClassT3[T: str = int]: ... # E: TypeVar default must be a subtype of the bound type |
| 51 |
class ClassT4[T: list[str] = list[int]]: ... # E: TypeVar default must be a subtype of the bound type |
| 52 |
class ClassT5[T: (int, str) = bytes]: ... # E: TypeVar default must be one of the constraint types |
| 53 |
class ClassT6[T: (int, str) = int | str]: ... # E: TypeVar default must be one of the constraint types |
| 54 |
class ClassT7[T: (float, str) = int]: ... # E: TypeVar default must be one of the constraint types |
| 55 |
|
| 56 |
class ClassP1[**P = int]: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 57 |
class ClassP2[**P = 2]: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 58 |
class ClassP3[**P = (2, int)]: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 59 |
class ClassP4[**P = [2, int]]: ... # E: Argument 0 of ParamSpec default must be a type |
| 60 |
|
| 61 |
class ClassTs1[*Ts = 2]: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 62 |
class ClassTs2[*Ts = int]: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 63 |
class ClassTs3[*Ts = tuple[int]]: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 64 |
[builtins fixtures/tuple.pyi] |
| 65 |
|
| 66 |
[case testPEP695TypeParameterDefaultInvalid2] |
| 67 |
from typing import overload |
| 68 |
def f1[T = 2]() -> None: ... # E: TypeVar "default" must be a type |
| 69 |
def f2[T = [int]]() -> None: ... # E: Bracketed expression "[...]" is not valid as a type \ |
| 70 |
# N: Did you mean "List[...]"? \ |
| 71 |
# E: TypeVar "default" must be a type |
| 72 |
def f3[T: str = int](x: T) -> T: ... # E: TypeVar default must be a subtype of the bound type |
| 73 |
def f4[T: list[str] = list[int]](x: T) -> T: ... # E: TypeVar default must be a subtype of the bound type |
| 74 |
def f5[T: (int, str) = bytes](x: T) -> T: ... # E: TypeVar default must be one of the constraint types |
| 75 |
def f6[T: (int, str) = int | str](x: T) -> T: ... # E: TypeVar default must be one of the constraint types |
| 76 |
def f7[T: (float, str) = int](x: T) -> T: ... # E: TypeVar default must be one of the constraint types |
| 77 |
def f8[T: str = int]() -> None: ... # TODO check unused TypeVars |
| 78 |
@overload |
| 79 |
def f9[T: str = int](x: T) -> T: ... # E: TypeVar default must be a subtype of the bound type |
| 80 |
@overload |
| 81 |
def f9[T: (int, str) = bytes](x: T) -> T: ... # E: TypeVar default must be one of the constraint types |
| 82 |
def f9() -> None: ... # type: ignore[misc] |
| 83 |
|
| 84 |
def g1[**P = int]() -> None: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 85 |
def g2[**P = 2]() -> None: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 86 |
def g3[**P = (2, int)]() -> None: ... # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 87 |
def g4[**P = [2, int]]() -> None: ... # E: Argument 0 of ParamSpec default must be a type |
| 88 |
|
| 89 |
def h1[*Ts = 2]() -> None: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 90 |
def h2[*Ts = int]() -> None: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 91 |
def h3[*Ts = tuple[int]]() -> None: ... # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 92 |
[builtins fixtures/tuple.pyi] |
| 93 |
|
| 94 |
[case testPEP695TypeParameterDefaultInvalid3] |
| 95 |
from typing import Callable |
| 96 |
|
| 97 |
type TA1[T: str = 1] = list[T] # E: TypeVar "default" must be a type |
| 98 |
type TA2[T: str = [int]] = list[T] # E: Bracketed expression "[...]" is not valid as a type \ |
| 99 |
# N: Did you mean "List[...]"? \ |
| 100 |
# E: TypeVar "default" must be a type |
| 101 |
type TA3[T: str = int] = list[T] # E: TypeVar default must be a subtype of the bound type |
| 102 |
type TA4[T: list[str] = list[int]] = list[T] # E: TypeVar default must be a subtype of the bound type |
| 103 |
type TA5[T: (int, str) = bytes] = list[T] # E: TypeVar default must be one of the constraint types |
| 104 |
type TA6[T: (int, str) = int | str] = list[T] # E: TypeVar default must be one of the constraint types |
| 105 |
type TA7[T: (float, str) = int] = list[T] # E: TypeVar default must be one of the constraint types |
| 106 |
|
| 107 |
type TB1[**P = int] = Callable[P, None] # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 108 |
type TB2[**P = 2] = Callable[P, None] # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 109 |
type TB3[**P = (2, int)] = Callable[P, None] # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec |
| 110 |
type TB4[**P = [2, int]] = Callable[P, None] # E: Argument 0 of ParamSpec default must be a type |
| 111 |
|
| 112 |
type TC1[*Ts = 2] = tuple[*Ts] # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 113 |
type TC2[*Ts = int] = tuple[*Ts] # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 114 |
type TC3[*Ts = tuple[int]] = tuple[*Ts] # E: The default argument to TypeVarTuple must be an Unpacked tuple |
| 115 |
[builtins fixtures/tuple.pyi] |
| 116 |
[typing fixtures/typing-full.pyi] |
| 117 |
|
| 118 |
[case testPEP695TypeParameterDefaultFunctions] |
| 119 |
from typing import Callable |
| 120 |
|
| 121 |
def callback1(x: str) -> None: ... |
| 122 |
|
| 123 |
def func_a1[T = str](x: int | T) -> T: ... |
| 124 |
reveal_type(func_a1(2)) # N: Revealed type is "builtins.str" |
| 125 |
reveal_type(func_a1(2.1)) # N: Revealed type is "builtins.float" |
| 126 |
|
| 127 |
def func_a2[T = str](x: int | T) -> list[T]: ... |
| 128 |
reveal_type(func_a2(2)) # N: Revealed type is "builtins.list[builtins.str]" |
| 129 |
reveal_type(func_a2(2.1)) # N: Revealed type is "builtins.list[builtins.float]" |
| 130 |
|
| 131 |
|
| 132 |
def func_a3[T: str = str](x: int | T) -> T: ... |
| 133 |
reveal_type(func_a3(2)) # N: Revealed type is "builtins.str" |
| 134 |
|
| 135 |
def func_a4[T: (bytes, str) = str](x: int | T) -> T: ... |
| 136 |
reveal_type(func_a4(2)) # N: Revealed type is "builtins.str" |
| 137 |
|
| 138 |
def func_b1[**P = [int, str]](x: int | Callable[P, None]) -> Callable[P, None]: ... |
| 139 |
reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str)" |
| 140 |
reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)" |
| 141 |
|
| 142 |
def func_c1[*Ts = *tuple[int, str]](x: int | Callable[[*Ts], None]) -> tuple[*Ts]: ... |
| 143 |
reveal_type(func_c1(callback1)) # N: Revealed type is "tuple[builtins.str]" |
| 144 |
reveal_type(func_c1(2)) # N: Revealed type is "tuple[builtins.int, builtins.str]" |
| 145 |
[builtins fixtures/tuple.pyi] |
| 146 |
|
| 147 |
[case testPEP695TypeParameterDefaultClass1] |
| 148 |
# flags: --disallow-any-generics |
| 149 |
|
| 150 |
class ClassA1[T2 = int, T3 = str]: ... |
| 151 |
|
| 152 |
def func_a1( |
| 153 |
a: ClassA1, |
| 154 |
b: ClassA1[float], |
| 155 |
c: ClassA1[float, float], |
| 156 |
d: ClassA1[float, float, float], # E: "ClassA1" expects between 0 and 2 type arguments, but 3 given |
| 157 |
) -> None: |
| 158 |
reveal_type(a) # N: Revealed type is "__main__.ClassA1[builtins.int, builtins.str]" |
| 159 |
reveal_type(b) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.str]" |
| 160 |
reveal_type(c) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.float]" |
| 161 |
reveal_type(d) # N: Revealed type is "__main__.ClassA1[builtins.int, builtins.str]" |
| 162 |
[builtins fixtures/tuple.pyi] |
| 163 |
|
| 164 |
[case testPEP695TypeParameterDefaultClass2] |
| 165 |
# flags: --disallow-any-generics |
| 166 |
|
| 167 |
class ClassB1[**P2 = [int, str], **P3 = ...]: ... |
| 168 |
|
| 169 |
def func_b1( |
| 170 |
a: ClassB1, |
| 171 |
b: ClassB1[[float]], |
| 172 |
c: ClassB1[[float], [float]], |
| 173 |
d: ClassB1[[float], [float], [float]], # E: "ClassB1" expects between 0 and 2 type arguments, but 3 given |
| 174 |
) -> None: |
| 175 |
reveal_type(a) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], ...]" |
| 176 |
reveal_type(b) # N: Revealed type is "__main__.ClassB1[[builtins.float], ...]" |
| 177 |
reveal_type(c) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]" |
| 178 |
reveal_type(d) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], ...]" |
| 179 |
|
| 180 |
k = ClassB1() |
| 181 |
reveal_type(k) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]" |
| 182 |
l = ClassB1[[float]]() |
| 183 |
reveal_type(l) # N: Revealed type is "__main__.ClassB1[[builtins.float], [*Any, **Any]]" |
| 184 |
m = ClassB1[[float], [float]]() |
| 185 |
reveal_type(m) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]" |
| 186 |
n = ClassB1[[float], [float], [float]]() # E: Type application has too many types (expected between 0 and 2) |
| 187 |
reveal_type(n) # N: Revealed type is "Any" |
| 188 |
|
| 189 |
[case testPEP695TypeParameterDefaultClass3] |
| 190 |
# flags: --disallow-any-generics |
| 191 |
|
| 192 |
class ClassC1[*Ts = *tuple[int, str]]: ... |
| 193 |
|
| 194 |
def func_c1( |
| 195 |
a: ClassC1, |
| 196 |
b: ClassC1[float], |
| 197 |
) -> None: |
| 198 |
reveal_type(a) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" |
| 199 |
reveal_type(b) # N: Revealed type is "__main__.ClassC1[builtins.float]" |
| 200 |
|
| 201 |
k = ClassC1() |
| 202 |
reveal_type(k) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" |
| 203 |
l = ClassC1[float]() |
| 204 |
reveal_type(l) # N: Revealed type is "__main__.ClassC1[builtins.float]" |
| 205 |
[builtins fixtures/tuple.pyi] |
| 206 |
|
| 207 |
[case testPEP695TypeParameterDefaultTypeAlias1] |
| 208 |
# flags: --disallow-any-generics |
| 209 |
|
| 210 |
type TA1[T2 = int, T3 = str] = dict[T2, T3] |
| 211 |
|
| 212 |
def func_a1( |
| 213 |
a: TA1, |
| 214 |
b: TA1[float], |
| 215 |
c: TA1[float, float], |
| 216 |
d: TA1[float, float, float], # E: Bad number of arguments for type alias, expected between 0 and 2, given 3 |
| 217 |
) -> None: |
| 218 |
reveal_type(a) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 219 |
reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.str]" |
| 220 |
reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float]" |
| 221 |
reveal_type(d) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 222 |
[builtins fixtures/dict.pyi] |
| 223 |
[typing fixtures/typing-full.pyi] |
| 224 |
|
| 225 |
[case testPEP695TypeParameterDefaultTypeAlias2] |
| 226 |
# flags: --disallow-any-generics |
| 227 |
|
| 228 |
class ClassB1[**P2, **P3]: ... |
| 229 |
type TB1[**P2 = [int, str], **P3 = ...] = ClassB1[P2, P3] |
| 230 |
|
| 231 |
def func_b1( |
| 232 |
a: TB1, |
| 233 |
b: TB1[[float]], |
| 234 |
c: TB1[[float], [float]], |
| 235 |
d: TB1[[float], [float], [float]], # E: Bad number of arguments for type alias, expected between 0 and 2, given 3 |
| 236 |
) -> None: |
| 237 |
reveal_type(a) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]" |
| 238 |
reveal_type(b) # N: Revealed type is "__main__.ClassB1[[builtins.float], [*Any, **Any]]" |
| 239 |
reveal_type(c) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]" |
| 240 |
reveal_type(d) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]" |
| 241 |
[builtins fixtures/tuple.pyi] |
| 242 |
[typing fixtures/typing-full.pyi] |
| 243 |
|
| 244 |
[case testPEP695TypeParameterDefaultTypeAlias3] |
| 245 |
# flags: --disallow-any-generics |
| 246 |
|
| 247 |
type TC1[*Ts = *tuple[int, str]] = tuple[*Ts] |
| 248 |
|
| 249 |
def func_c1( |
| 250 |
a: TC1, |
| 251 |
b: TC1[float], |
| 252 |
) -> None: |
| 253 |
reveal_type(a) # N: Revealed type is "tuple[builtins.int, builtins.str]" |
| 254 |
reveal_type(b) # N: Revealed type is "tuple[builtins.float]" |
| 255 |
|
| 256 |
[builtins fixtures/tuple.pyi] |
| 257 |
[typing fixtures/typing-full.pyi] |
| 258 |
|
| 259 |
[case testPEP695TypeParameterDefaultTypeAlias4] |
| 260 |
# flags: --disallow-any-generics |
| 261 |
class A[L = int, M = str]: ... |
| 262 |
TD1 = A[float] |
| 263 |
type TD2 = A[float] |
| 264 |
|
| 265 |
def func_d1( |
| 266 |
a: TD1, |
| 267 |
b: TD1[float], # E: Bad number of arguments for type alias, expected 0, given 1 |
| 268 |
c: TD2, |
| 269 |
d: TD2[float], # E: Bad number of arguments for type alias, expected 0, given 1 |
| 270 |
) -> None: |
| 271 |
reveal_type(a) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" |
| 272 |
reveal_type(b) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" |
| 273 |
reveal_type(c) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" |
| 274 |
reveal_type(d) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" |
| 275 |
[builtins fixtures/tuple.pyi] |
| 276 |
[typing fixtures/typing-full.pyi] |
| 277 |
|
| 278 |
[case testTypeVarConstraintsDefaultAliasesInline] |
| 279 |
type K = int |
| 280 |
type V = int |
| 281 |
|
| 282 |
class A1[T: (str, int) = K]: |
| 283 |
x: T |
| 284 |
class A2[T: (str, K) = K]: |
| 285 |
x: T |
| 286 |
class A3[T: (str, K) = V]: |
| 287 |
x: T |
| 288 |
|
| 289 |
reveal_type(A1().x) # N: Revealed type is "builtins.int" |
| 290 |
reveal_type(A2().x) # N: Revealed type is "builtins.int" |
| 291 |
reveal_type(A3().x) # N: Revealed type is "builtins.int" |
| 292 |
[builtins fixtures/tuple.pyi] |
| 293 |
|
| 294 |
[case testTypeVarDefaultToAnotherTypeVarClass] |
| 295 |
class A[X, Y = X, Z = Y]: |
| 296 |
x: X |
| 297 |
y: Y |
| 298 |
z: Z |
| 299 |
|
| 300 |
a1: A[int] |
| 301 |
reveal_type(a1.x) # N: Revealed type is "builtins.int" |
| 302 |
reveal_type(a1.y) # N: Revealed type is "builtins.int" |
| 303 |
reveal_type(a1.z) # N: Revealed type is "builtins.int" |
| 304 |
|
| 305 |
a2: A[int, str] |
| 306 |
reveal_type(a2.x) # N: Revealed type is "builtins.int" |
| 307 |
reveal_type(a2.y) # N: Revealed type is "builtins.str" |
| 308 |
reveal_type(a2.z) # N: Revealed type is "builtins.str" |
| 309 |
|
| 310 |
a3: A[int, str, bool] |
| 311 |
reveal_type(a3.x) # N: Revealed type is "builtins.int" |
| 312 |
reveal_type(a3.y) # N: Revealed type is "builtins.str" |
| 313 |
reveal_type(a3.z) # N: Revealed type is "builtins.bool" |
| 314 |
[builtins fixtures/tuple.pyi] |
| 315 |
|
| 316 |
[case testTypeVarDefaultToAnotherTypeVarAlias] |
| 317 |
type A[X, Y = X, Z = Y] = tuple[X, Y, Z] |
| 318 |
|
| 319 |
a1: A[int] |
| 320 |
reveal_type(a1[0]) # N: Revealed type is "builtins.int" |
| 321 |
reveal_type(a1[1]) # N: Revealed type is "builtins.int" |
| 322 |
reveal_type(a1[2]) # N: Revealed type is "builtins.int" |
| 323 |
|
| 324 |
a2: A[int, str] |
| 325 |
reveal_type(a2[0]) # N: Revealed type is "builtins.int" |
| 326 |
reveal_type(a2[1]) # N: Revealed type is "builtins.str" |
| 327 |
reveal_type(a2[2]) # N: Revealed type is "builtins.str" |
| 328 |
|
| 329 |
a3: A[int, str, bool] |
| 330 |
reveal_type(a3[0]) # N: Revealed type is "builtins.int" |
| 331 |
reveal_type(a3[1]) # N: Revealed type is "builtins.str" |
| 332 |
reveal_type(a3[2]) # N: Revealed type is "builtins.bool" |
| 333 |
[builtins fixtures/tuple.pyi] |
| 334 |
|
| 335 |
[case testTypeVarDefaultAssertTypeType] |
| 336 |
from typing import assert_type |
| 337 |
|
| 338 |
class NoNonDefaults[T = str, S = int]: ... |
| 339 |
|
| 340 |
assert_type(NoNonDefaults[()], type[NoNonDefaults[str, int]]) |
| 341 |
assert_type(NoNonDefaults[str], type[NoNonDefaults[str, int]]) |
| 342 |
assert_type(NoNonDefaults[str, int], type[NoNonDefaults[str, int]]) |
| 343 |
[builtins fixtures/tuple.pyi] |
| 344 |
|
| 345 |
[case testTypeVarDefaultToAnotherTypeVarWrong] |
| 346 |
class A[Y = X, X = int]: ... # E: Name "X" is not defined |
| 347 |
|
| 348 |
class B[Y = X]: ... # E: Name "X" is not defined |
| 349 |
[builtins fixtures/tuple.pyi] |
| 350 |
|
| 351 |
|
| 352 |
[case testTernaryOperatorWithTypeVarDefault] |
| 353 |
# https://github.com/python/mypy/issues/18817 |
| 354 |
|
| 355 |
class Ok[T, E = None]: |
| 356 |
def __init__(self, value: T) -> None: |
| 357 |
self._value = value |
| 358 |
|
| 359 |
class Err[E, T = None]: |
| 360 |
def __init__(self, value: E) -> None: |
| 361 |
self._value = value |
| 362 |
|
| 363 |
type Result[T, E] = Ok[T, E] | Err[E, T] |
| 364 |
|
| 365 |
class Bar[U]: |
| 366 |
def foo(data: U, cond: bool) -> Result[U, str]: |
| 367 |
return Ok(data) if cond else Err("Error") |
| 368 |
|
| 369 |
[case testRecursiveTypeVarDefaultBasicNewStyle] |
| 370 |
class C[T: C = C]: |
| 371 |
pass |
| 372 |
|
| 373 |
c: C |
| 374 |
reveal_type(c) # N: Revealed type is "__main__.C[__main__.C[Any]]" |
| 375 |
|
| 376 |
[case testRecursiveTypeVarDefaultMutualNewStyle] |
| 377 |
class C[T = D]: |
| 378 |
pass |
| 379 |
|
| 380 |
class D[S = C]: |
| 381 |
pass |
| 382 |
|
| 383 |
c: C |
| 384 |
d: D |
| 385 |
reveal_type(c) # N: Revealed type is "__main__.C[__main__.D[Any]]" |
| 386 |
reveal_type(d) # N: Revealed type is "__main__.D[__main__.C[Any]]" |
| 387 |
|
| 388 |
[case testNonRecursiveSimpleTypeVarDefaultNewStyle] |
| 389 |
class Child[S = Parent]: ... |
| 390 |
|
| 391 |
class Parent[T = int]: ... |
| 392 |
|
| 393 |
reveal_type(Child()) # N: Revealed type is "__main__.Child[__main__.Parent[builtins.int]]" |
| 394 |
|
| 395 |
[case testNonRecursiveTypeVarDefaultImportCycleClassNewStyle] |
| 396 |
import exp |
| 397 |
[file exp.pyi] |
| 398 |
import ind |
| 399 |
|
| 400 |
class F[T: D = D]: |
| 401 |
x: T |
| 402 |
|
| 403 |
class D(E): ... |
| 404 |
class E: ... |
| 405 |
|
| 406 |
[file ind.pyi] |
| 407 |
from exp import F |
| 408 |
|
| 409 |
class Ind(F): ... |
| 410 |
x: Ind |
| 411 |
reveal_type(x.x) # N: Revealed type is "exp.D" |
| 412 |
|
| 413 |
[case testNonRecursiveTypeVarDefaultImportCycleAliasNewStyle] |
| 414 |
import exp |
| 415 |
[file exp.pyi] |
| 416 |
import ind |
| 417 |
|
| 418 |
type F[T: D = D] = list[T] |
| 419 |
|
| 420 |
class D(E): ... |
| 421 |
class E: ... |
| 422 |
|
| 423 |
[file ind.pyi] |
| 424 |
from exp import F |
| 425 |
|
| 426 |
type Ind = list[F] |
| 427 |
x: Ind |
| 428 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[exp.D]]" |
| 429 |
|
| 430 |
[case testRecursiveTypeVarDefaultClassAndAliasNewStyle] |
| 431 |
class Trait[T = Pattern]: |
| 432 |
pass |
| 433 |
|
| 434 |
class Pattern1(Trait): |
| 435 |
pass |
| 436 |
class Pattern2(Trait): |
| 437 |
pass |
| 438 |
|
| 439 |
type Pattern = Pattern1 | Pattern2 |
| 440 |
|
| 441 |
reveal_type(Trait()) # N: Revealed type is "__main__.Trait[__main__.Pattern1 | __main__.Pattern2]" |
| 442 |
[builtins fixtures/tuple.pyi] |
| 443 |
|
| 444 |
[case testRecursiveTypeVarDefaultOnlyAliasNewStyle] |
| 445 |
type A[T = A] = list[T] |
| 446 |
a: A |
| 447 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.list[Any]]" |
| 448 |
[builtins fixtures/tuple.pyi] |
| 449 |
|
| 450 |
[case testRecursiveAliasTypeVarDefaultNewStyle] |
| 451 |
type A[T = A] = int |
| 452 |
a: A |
| 453 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 454 |
|
| 455 |
[case testPEP695VariadicGenericClassEmptyTupleUnpackIndex] |
| 456 |
class Blah[*Ts]: |
| 457 |
pass |
| 458 |
|
| 459 |
x: Blah[()] |
| 460 |
y: Blah[*tuple[()]] |
| 461 |
|
| 462 |
type Empty = tuple[()] |
| 463 |
z: Blah[*Empty] |
| 464 |
|
| 465 |
reveal_type(x) # N: Revealed type is "__main__.Blah[()]" |
| 466 |
reveal_type(y) # N: Revealed type is "__main__.Blah[()]" |
| 467 |
reveal_type(z) # N: Revealed type is "__main__.Blah[()]" |
| 468 |
[builtins fixtures/tuple.pyi] |
| 469 |
|
| 470 |
[case testPEP695VariadicGenericAliasEmptyTupleUnpackIndex] |
| 471 |
type Blah[*Ts] = list[tuple[*Ts]] |
| 472 |
|
| 473 |
x: Blah[()] |
| 474 |
y: Blah[*tuple[()]] |
| 475 |
|
| 476 |
type Empty = tuple[()] |
| 477 |
z: Blah[*Empty] |
| 478 |
|
| 479 |
reveal_type(x) # N: Revealed type is "builtins.list[tuple[()]]" |
| 480 |
reveal_type(y) # N: Revealed type is "builtins.list[tuple[()]]" |
| 481 |
reveal_type(z) # N: Revealed type is "builtins.list[tuple[()]]" |
| 482 |
[builtins fixtures/tuple.pyi] |