| 1 |
[case testSelfTypeInstance] |
| 2 |
from typing import TypeVar |
| 3 |
|
| 4 |
T = TypeVar('T', bound='A', covariant=True) |
| 5 |
|
| 6 |
class A: |
| 7 |
def copy(self: T) -> T: pass |
| 8 |
|
| 9 |
class B(A): |
| 10 |
pass |
| 11 |
|
| 12 |
reveal_type(A().copy) # N: Revealed type is "def () -> __main__.A" |
| 13 |
reveal_type(B().copy) # N: Revealed type is "def () -> __main__.B" |
| 14 |
reveal_type(A().copy()) # N: Revealed type is "__main__.A" |
| 15 |
reveal_type(B().copy()) # N: Revealed type is "__main__.B" |
| 16 |
|
| 17 |
[builtins fixtures/bool.pyi] |
| 18 |
|
| 19 |
[case testSelfTypeStaticAccess] |
| 20 |
from typing import TypeVar |
| 21 |
|
| 22 |
T = TypeVar('T', bound='A', covariant=True) |
| 23 |
class A: |
| 24 |
def copy(self: T) -> T: pass |
| 25 |
|
| 26 |
class B(A): |
| 27 |
pass |
| 28 |
|
| 29 |
# Erased instances appear on reveal_type; unrelated to self type |
| 30 |
def f(a: A) -> None: pass |
| 31 |
f(A.copy(A())) |
| 32 |
f(A.copy(B())) |
| 33 |
f(B.copy(B())) |
| 34 |
|
| 35 |
# TODO: make it an error |
| 36 |
# f(B.copy(A())) |
| 37 |
|
| 38 |
def g(a: B) -> None: pass |
| 39 |
g(A.copy(A())) # E: Argument 1 to "g" has incompatible type "A"; expected "B" |
| 40 |
g(A.copy(B())) |
| 41 |
g(B.copy(B())) |
| 42 |
|
| 43 |
[builtins fixtures/bool.pyi] |
| 44 |
|
| 45 |
[case testSelfTypeReturn] |
| 46 |
from typing import TypeVar, Type |
| 47 |
|
| 48 |
R = TypeVar('R') |
| 49 |
def _type(self: R) -> Type[R]: pass |
| 50 |
|
| 51 |
T = TypeVar('T', bound='A', covariant=True) |
| 52 |
class A: |
| 53 |
def copy(self: T) -> T: |
| 54 |
if B(): |
| 55 |
return A() # E: Incompatible return value type (got "A", expected "T") |
| 56 |
elif A(): |
| 57 |
return B() # E: Incompatible return value type (got "B", expected "T") |
| 58 |
reveal_type(_type(self)) # N: Revealed type is "type[T`-1]" |
| 59 |
return reveal_type(_type(self)()) # N: Revealed type is "T`-1" |
| 60 |
|
| 61 |
class B(A): |
| 62 |
pass |
| 63 |
|
| 64 |
Q = TypeVar('Q', bound='C', covariant=True) |
| 65 |
class C: |
| 66 |
def __init__(self, a: int) -> None: pass |
| 67 |
|
| 68 |
def copy(self: Q) -> Q: |
| 69 |
if self: |
| 70 |
return reveal_type(_type(self)(1)) # N: Revealed type is "Q`-1" |
| 71 |
else: |
| 72 |
return _type(self)() # E: Missing positional argument "a" in call to "C" |
| 73 |
|
| 74 |
|
| 75 |
[builtins fixtures/bool.pyi] |
| 76 |
|
| 77 |
[case testSelfTypeClass] |
| 78 |
from typing import TypeVar, Type |
| 79 |
|
| 80 |
T = TypeVar('T', bound='A') |
| 81 |
|
| 82 |
class A: |
| 83 |
@classmethod |
| 84 |
def new(cls: Type[T]) -> T: |
| 85 |
return reveal_type(cls()) # N: Revealed type is "T`-1" |
| 86 |
|
| 87 |
class B(A): |
| 88 |
pass |
| 89 |
|
| 90 |
Q = TypeVar('Q', bound='C', covariant=True) |
| 91 |
class C: |
| 92 |
def __init__(self, a: int) -> None: pass |
| 93 |
|
| 94 |
@classmethod |
| 95 |
def new(cls: Type[Q]) -> Q: |
| 96 |
if cls: |
| 97 |
return cls(1) |
| 98 |
else: |
| 99 |
return cls() # E: Missing positional argument "a" in call to "C" |
| 100 |
|
| 101 |
|
| 102 |
reveal_type(A.new) # N: Revealed type is "def () -> __main__.A" |
| 103 |
reveal_type(B.new) # N: Revealed type is "def () -> __main__.B" |
| 104 |
reveal_type(A.new()) # N: Revealed type is "__main__.A" |
| 105 |
reveal_type(B.new()) # N: Revealed type is "__main__.B" |
| 106 |
|
| 107 |
[builtins fixtures/classmethod.pyi] |
| 108 |
|
| 109 |
[case testSelfTypeOverride] |
| 110 |
from typing import TypeVar, cast |
| 111 |
|
| 112 |
T = TypeVar('T', bound='A', covariant=True) |
| 113 |
|
| 114 |
class A: |
| 115 |
def copy(self: T) -> T: pass |
| 116 |
|
| 117 |
class B(A): |
| 118 |
pass |
| 119 |
|
| 120 |
Q = TypeVar('Q', bound='C', covariant=True) |
| 121 |
class C(A): |
| 122 |
def copy(self: Q) -> Q: pass |
| 123 |
|
| 124 |
reveal_type(C().copy) # N: Revealed type is "def () -> __main__.C" |
| 125 |
reveal_type(C().copy()) # N: Revealed type is "__main__.C" |
| 126 |
reveal_type(cast(A, C()).copy) # N: Revealed type is "def () -> __main__.A" |
| 127 |
reveal_type(cast(A, C()).copy()) # N: Revealed type is "__main__.A" |
| 128 |
|
| 129 |
[builtins fixtures/bool.pyi] |
| 130 |
|
| 131 |
[case testSelfTypeOverrideCompatibility] |
| 132 |
from typing import overload, TypeVar, Generic |
| 133 |
|
| 134 |
T = TypeVar("T") |
| 135 |
|
| 136 |
class A(Generic[T]): |
| 137 |
@overload |
| 138 |
def f(self: A[int]) -> int: ... |
| 139 |
@overload |
| 140 |
def f(self: A[str]) -> str: ... |
| 141 |
def f(self): ... |
| 142 |
|
| 143 |
class B(A[T]): |
| 144 |
@overload |
| 145 |
def f(self: A[int]) -> int: ... |
| 146 |
@overload |
| 147 |
def f(self: A[str]) -> str: ... |
| 148 |
def f(self): ... |
| 149 |
|
| 150 |
class B2(A[T]): |
| 151 |
@overload |
| 152 |
def f(self: A[int]) -> int: ... |
| 153 |
@overload |
| 154 |
def f(self: A[str]) -> str: ... |
| 155 |
@overload |
| 156 |
def f(self: A[bytes]) -> bytes: ... |
| 157 |
def f(self): ... |
| 158 |
|
| 159 |
class C(A[int]): |
| 160 |
def f(self) -> int: ... |
| 161 |
|
| 162 |
class D(A[str]): |
| 163 |
def f(self) -> int: ... # E: Return type "int" of "f" incompatible with return type "str" in supertype "A" |
| 164 |
|
| 165 |
class E(A[T]): |
| 166 |
def f(self) -> int: ... # E: Signature of "f" incompatible with supertype "A" \ |
| 167 |
# N: Superclass: \ |
| 168 |
# N: @overload \ |
| 169 |
# N: def f(self) -> int \ |
| 170 |
# N: @overload \ |
| 171 |
# N: def f(self) -> str \ |
| 172 |
# N: Subclass: \ |
| 173 |
# N: def f(self) -> int |
| 174 |
|
| 175 |
|
| 176 |
class F(A[bytes]): |
| 177 |
# Note there's an argument to be made that this is actually compatible with the supertype |
| 178 |
def f(self) -> bytes: ... # E: Signature of "f" incompatible with supertype "A" \ |
| 179 |
# N: Superclass: \ |
| 180 |
# N: @overload \ |
| 181 |
# N: def f(self) -> int \ |
| 182 |
# N: @overload \ |
| 183 |
# N: def f(self) -> str \ |
| 184 |
# N: Subclass: \ |
| 185 |
# N: def f(self) -> bytes |
| 186 |
|
| 187 |
class G(A): |
| 188 |
def f(self): ... |
| 189 |
|
| 190 |
class H(A[int]): |
| 191 |
def f(self): ... |
| 192 |
|
| 193 |
class I(A[int]): |
| 194 |
def f(*args): ... |
| 195 |
|
| 196 |
class J(A[int]): |
| 197 |
def f(self, arg) -> int: ... # E: Signature of "f" incompatible with supertype "A" \ |
| 198 |
# N: Superclass: \ |
| 199 |
# N: def f(self) -> int \ |
| 200 |
# N: Subclass: \ |
| 201 |
# N: def f(self, arg: Any) -> int |
| 202 |
|
| 203 |
[builtins fixtures/tuple.pyi] |
| 204 |
|
| 205 |
[case testSelfTypeOverrideCompatibilityGeneric] |
| 206 |
from typing import TypeVar, Generic, overload |
| 207 |
|
| 208 |
T = TypeVar("T", str, int, None) |
| 209 |
|
| 210 |
class A(Generic[T]): |
| 211 |
@overload |
| 212 |
def f(self, s: T) -> T: ... |
| 213 |
@overload |
| 214 |
def f(self: A[str], s: bytes) -> str: ... |
| 215 |
def f(self, s: object): ... |
| 216 |
|
| 217 |
class B(A[int]): |
| 218 |
def f(self, s: int) -> int: ... |
| 219 |
|
| 220 |
class C(A[None]): |
| 221 |
def f(self, s: int) -> int: ... # E: Return type "int" of "f" incompatible with return type "None" in supertype "A" \ |
| 222 |
# E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "None" \ |
| 223 |
# N: This violates the Liskov substitution principle \ |
| 224 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 225 |
[builtins fixtures/tuple.pyi] |
| 226 |
|
| 227 |
[case testSelfTypeOverrideCompatibilityTypeVar] |
| 228 |
from typing import overload, TypeVar, Union |
| 229 |
|
| 230 |
AT = TypeVar("AT", bound="A") |
| 231 |
|
| 232 |
class A: |
| 233 |
@overload |
| 234 |
def f(self: AT, x: int) -> AT: ... |
| 235 |
@overload |
| 236 |
def f(self, x: str) -> None: ... |
| 237 |
@overload |
| 238 |
def f(self: AT) -> bytes: ... |
| 239 |
def f(*a, **kw): ... |
| 240 |
|
| 241 |
class B(A): |
| 242 |
@overload # E: Signature of "f" incompatible with supertype "A" \ |
| 243 |
# N: Superclass: \ |
| 244 |
# N: @overload \ |
| 245 |
# N: def f(self, x: int) -> B \ |
| 246 |
# N: @overload \ |
| 247 |
# N: def f(self, x: str) -> None \ |
| 248 |
# N: @overload \ |
| 249 |
# N: def f(self) -> bytes \ |
| 250 |
# N: Subclass: \ |
| 251 |
# N: @overload \ |
| 252 |
# N: def f(self, x: int) -> B \ |
| 253 |
# N: @overload \ |
| 254 |
# N: def f(self, x: str) -> None |
| 255 |
def f(self, x: int) -> B: ... |
| 256 |
@overload |
| 257 |
def f(self, x: str) -> None: ... |
| 258 |
def f(*a, **kw): ... |
| 259 |
[builtins fixtures/dict.pyi] |
| 260 |
|
| 261 |
[case testSelfTypeOverrideCompatibilitySelfTypeVar] |
| 262 |
from typing import Any, Generic, Self, TypeVar, overload |
| 263 |
|
| 264 |
T_co = TypeVar('T_co', covariant=True) |
| 265 |
|
| 266 |
class Config(Generic[T_co]): |
| 267 |
@overload |
| 268 |
def get(self, instance: None) -> Self: ... |
| 269 |
@overload |
| 270 |
def get(self, instance: Any) -> T_co: ... |
| 271 |
def get(self, *a, **kw): ... |
| 272 |
|
| 273 |
class MultiConfig(Config[T_co]): |
| 274 |
@overload |
| 275 |
def get(self, instance: None) -> Self: ... |
| 276 |
@overload |
| 277 |
def get(self, instance: Any) -> T_co: ... |
| 278 |
def get(self, *a, **kw): ... |
| 279 |
[builtins fixtures/dict.pyi] |
| 280 |
|
| 281 |
[case testSelfTypeSuper] |
| 282 |
from typing import TypeVar, cast |
| 283 |
|
| 284 |
T = TypeVar('T', bound='A', covariant=True) |
| 285 |
|
| 286 |
class A: |
| 287 |
def copy(self: T) -> T: pass |
| 288 |
|
| 289 |
Q = TypeVar('Q', bound='B', covariant=True) |
| 290 |
class B(A): |
| 291 |
def copy(self: Q) -> Q: |
| 292 |
reveal_type(self) # N: Revealed type is "Q`-1" |
| 293 |
reveal_type(super().copy) # N: Revealed type is "def () -> Q`-1" |
| 294 |
return super().copy() |
| 295 |
|
| 296 |
[builtins fixtures/bool.pyi] |
| 297 |
|
| 298 |
[case testSelfTypeRecursiveBinding] |
| 299 |
from typing import TypeVar, Callable, Type |
| 300 |
|
| 301 |
T = TypeVar('T', bound='A', covariant=True) |
| 302 |
class A: |
| 303 |
# TODO: This is potentially unsafe, as we use T in an argument type |
| 304 |
def copy(self: T, factory: Callable[[T], T]) -> T: |
| 305 |
return factory(self) |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def new(cls: Type[T], factory: Callable[[T], T]) -> T: |
| 309 |
reveal_type(cls) # N: Revealed type is "type[T`-1]" |
| 310 |
reveal_type(cls()) # N: Revealed type is "T`-1" |
| 311 |
cls(2) # E: Too many arguments for "A" |
| 312 |
return cls() |
| 313 |
|
| 314 |
class B(A): |
| 315 |
pass |
| 316 |
|
| 317 |
reveal_type(A().copy) # N: Revealed type is "def (factory: def (__main__.A) -> __main__.A) -> __main__.A" |
| 318 |
reveal_type(B().copy) # N: Revealed type is "def (factory: def (__main__.B) -> __main__.B) -> __main__.B" |
| 319 |
reveal_type(A.new) # N: Revealed type is "def (factory: def (__main__.A) -> __main__.A) -> __main__.A" |
| 320 |
reveal_type(B.new) # N: Revealed type is "def (factory: def (__main__.B) -> __main__.B) -> __main__.B" |
| 321 |
|
| 322 |
[builtins fixtures/classmethod.pyi] |
| 323 |
|
| 324 |
[case testSelfTypeBound] |
| 325 |
from typing import TypeVar, Callable, cast |
| 326 |
|
| 327 |
TA = TypeVar('TA', bound='A', covariant=True) |
| 328 |
|
| 329 |
class A: |
| 330 |
def copy(self: TA) -> TA: |
| 331 |
pass |
| 332 |
|
| 333 |
class C(A): |
| 334 |
def copy(self: C) -> C: |
| 335 |
pass |
| 336 |
|
| 337 |
class D(A): |
| 338 |
def copy(self: A) -> A: # E: Return type "A" of "copy" incompatible with return type "D" in supertype "A" |
| 339 |
pass |
| 340 |
|
| 341 |
TB = TypeVar('TB', bound='B', covariant=True) |
| 342 |
class B(A): |
| 343 |
x = 1 |
| 344 |
def copy(self: TB) -> TB: |
| 345 |
reveal_type(self.x) # N: Revealed type is "builtins.int" |
| 346 |
return cast(TB, None) |
| 347 |
|
| 348 |
[builtins fixtures/bool.pyi] |
| 349 |
|
| 350 |
-- # TODO: fail for this |
| 351 |
-- [case testSelfTypeBare] |
| 352 |
-- from typing import TypeVar, Type |
| 353 |
-- |
| 354 |
-- T = TypeVar('T', bound='E') |
| 355 |
-- |
| 356 |
-- class E: |
| 357 |
-- def copy(self: T, other: T) -> T: pass |
| 358 |
|
| 359 |
[case testSelfTypeClone] |
| 360 |
from typing import TypeVar, Type |
| 361 |
T = TypeVar('T', bound='C') |
| 362 |
|
| 363 |
class C: |
| 364 |
def copy(self: T) -> T: |
| 365 |
return self |
| 366 |
|
| 367 |
@classmethod |
| 368 |
def new(cls: Type[T]) -> T: |
| 369 |
return cls() |
| 370 |
|
| 371 |
class D(C): pass |
| 372 |
|
| 373 |
reveal_type(D.new) # N: Revealed type is "def () -> __main__.D" |
| 374 |
reveal_type(D().new) # N: Revealed type is "def () -> __main__.D" |
| 375 |
reveal_type(D.new()) # N: Revealed type is "__main__.D" |
| 376 |
reveal_type(D().new()) # N: Revealed type is "__main__.D" |
| 377 |
|
| 378 |
Q = TypeVar('Q', bound=C) |
| 379 |
|
| 380 |
def clone(arg: Q) -> Q: |
| 381 |
reveal_type(arg.copy) # N: Revealed type is "def () -> Q`-1" |
| 382 |
reveal_type(arg.copy()) # N: Revealed type is "Q`-1" |
| 383 |
reveal_type(arg.new) # N: Revealed type is "def () -> Q`-1" |
| 384 |
reveal_type(arg.new()) # N: Revealed type is "Q`-1" |
| 385 |
return arg.copy() |
| 386 |
|
| 387 |
def make(cls: Type[Q]) -> Q: |
| 388 |
reveal_type(cls.new) # N: Revealed type is "def () -> Q`-1" |
| 389 |
reveal_type(cls().new) # N: Revealed type is "def () -> Q`-1" |
| 390 |
reveal_type(cls().new()) # N: Revealed type is "Q`-1" |
| 391 |
return cls.new() |
| 392 |
|
| 393 |
[builtins fixtures/classmethod.pyi] |
| 394 |
|
| 395 |
[case testSelfTypeGeneric] |
| 396 |
from typing import TypeVar |
| 397 |
|
| 398 |
T = TypeVar('T', int, str) |
| 399 |
|
| 400 |
class A: |
| 401 |
pass |
| 402 |
|
| 403 |
class B(A): |
| 404 |
def __init__(self, arg: T) -> None: |
| 405 |
super(B, self).__init__() |
| 406 |
|
| 407 |
[case testSelfTypeNonsensical] |
| 408 |
from typing import TypeVar, Type |
| 409 |
|
| 410 |
T = TypeVar('T', bound=str) |
| 411 |
class A: |
| 412 |
def foo(self: T) -> T: # E: The erased type of self "builtins.str" is not a supertype of its class "__main__.A" |
| 413 |
return self |
| 414 |
|
| 415 |
@classmethod |
| 416 |
def cfoo(cls: Type[T]) -> T: # E: The erased type of self "type[builtins.str]" is not a supertype of its class "type[__main__.A]" |
| 417 |
return cls() |
| 418 |
|
| 419 |
Q = TypeVar('Q', bound='B') |
| 420 |
class B: |
| 421 |
def foo(self: Q) -> Q: |
| 422 |
return self |
| 423 |
|
| 424 |
@classmethod |
| 425 |
def cfoo(cls: Type[Q]) -> Q: |
| 426 |
return cls() |
| 427 |
|
| 428 |
class C: |
| 429 |
def foo(self: C) -> C: return self |
| 430 |
|
| 431 |
@classmethod |
| 432 |
def cfoo(cls: Type[C]) -> C: |
| 433 |
return cls() |
| 434 |
|
| 435 |
class D: |
| 436 |
def foo(self: Q) -> Q: # E: The erased type of self "__main__.B" is not a supertype of its class "__main__.D" |
| 437 |
return self |
| 438 |
|
| 439 |
@staticmethod |
| 440 |
def bar(self: str) -> str: |
| 441 |
return self |
| 442 |
|
| 443 |
@classmethod |
| 444 |
def cfoo(cls: Type[Q]) -> Q: # E: The erased type of self "type[__main__.B]" is not a supertype of its class "type[__main__.D]" |
| 445 |
return cls() |
| 446 |
|
| 447 |
[builtins fixtures/classmethod.pyi] |
| 448 |
|
| 449 |
[case testSelfTypeLambdaDefault] |
| 450 |
from typing import Callable |
| 451 |
class C: |
| 452 |
@classmethod |
| 453 |
def foo(cls, |
| 454 |
arg: Callable[[int], str] = lambda a: '' |
| 455 |
) -> None: |
| 456 |
pass |
| 457 |
|
| 458 |
def bar(self, |
| 459 |
arg: Callable[[int], str] = lambda a: '' |
| 460 |
) -> None: |
| 461 |
pass |
| 462 |
[builtins fixtures/classmethod.pyi] |
| 463 |
|
| 464 |
[case testSelfTypeNew] |
| 465 |
from typing import TypeVar, Type |
| 466 |
|
| 467 |
T = TypeVar('T', bound='A') |
| 468 |
class A: |
| 469 |
def __new__(cls: Type[T]) -> T: |
| 470 |
return cls() |
| 471 |
|
| 472 |
def __init_subclass__(cls: Type[T]) -> None: |
| 473 |
pass |
| 474 |
|
| 475 |
class B: |
| 476 |
def __new__(cls: Type[T]) -> T: # E: The erased type of self "type[__main__.A]" is not a supertype of its class "type[__main__.B]" |
| 477 |
return cls() |
| 478 |
|
| 479 |
def __init_subclass__(cls: Type[T]) -> None: # E: The erased type of self "type[__main__.A]" is not a supertype of its class "type[__main__.B]" |
| 480 |
pass |
| 481 |
|
| 482 |
class C: |
| 483 |
def __new__(cls: Type[C]) -> C: |
| 484 |
return cls() |
| 485 |
|
| 486 |
def __init_subclass__(cls: Type[C]) -> None: |
| 487 |
pass |
| 488 |
|
| 489 |
class D: |
| 490 |
def __new__(cls: D) -> D: # E: The erased type of self "__main__.D" is not a supertype of its class "type[__main__.D]" |
| 491 |
return cls |
| 492 |
|
| 493 |
def __init_subclass__(cls: D) -> None: # E: The erased type of self "__main__.D" is not a supertype of its class "type[__main__.D]" |
| 494 |
pass |
| 495 |
|
| 496 |
class E: |
| 497 |
def __new__(cls) -> E: |
| 498 |
reveal_type(cls) # N: Revealed type is "type[__main__.E]" |
| 499 |
return cls() |
| 500 |
|
| 501 |
def __init_subclass__(cls) -> None: |
| 502 |
reveal_type(cls) # N: Revealed type is "type[__main__.E]" |
| 503 |
|
| 504 |
[case testSelfTypeNew_explicit] |
| 505 |
from typing import TypeVar, Type |
| 506 |
|
| 507 |
T = TypeVar('T', bound='A') |
| 508 |
class A: |
| 509 |
@staticmethod |
| 510 |
def __new__(cls: Type[T]) -> T: |
| 511 |
return cls() |
| 512 |
|
| 513 |
@classmethod |
| 514 |
def __init_subclass__(cls: Type[T]) -> None: |
| 515 |
pass |
| 516 |
|
| 517 |
class B: |
| 518 |
@staticmethod |
| 519 |
def __new__(cls: Type[T]) -> T: # E: The erased type of self "type[__main__.A]" is not a supertype of its class "type[__main__.B]" |
| 520 |
return cls() |
| 521 |
|
| 522 |
@classmethod |
| 523 |
def __init_subclass__(cls: Type[T]) -> None: # E: The erased type of self "type[__main__.A]" is not a supertype of its class "type[__main__.B]" |
| 524 |
pass |
| 525 |
|
| 526 |
class C: |
| 527 |
@staticmethod |
| 528 |
def __new__(cls: Type[C]) -> C: |
| 529 |
return cls() |
| 530 |
|
| 531 |
@classmethod |
| 532 |
def __init_subclass__(cls: Type[C]) -> None: |
| 533 |
pass |
| 534 |
|
| 535 |
class D: |
| 536 |
@staticmethod |
| 537 |
def __new__(cls: D) -> D: # E: The erased type of self "__main__.D" is not a supertype of its class "type[__main__.D]" |
| 538 |
return cls |
| 539 |
|
| 540 |
@classmethod |
| 541 |
def __init_subclass__(cls: D) -> None: # E: The erased type of self "__main__.D" is not a supertype of its class "type[__main__.D]" |
| 542 |
pass |
| 543 |
|
| 544 |
class E: |
| 545 |
@staticmethod |
| 546 |
def __new__(cls) -> E: |
| 547 |
reveal_type(cls) # N: Revealed type is "type[__main__.E]" |
| 548 |
return cls() |
| 549 |
|
| 550 |
@classmethod |
| 551 |
def __init_subclass__(cls) -> None: |
| 552 |
reveal_type(cls) # N: Revealed type is "type[__main__.E]" |
| 553 |
|
| 554 |
[builtins fixtures/classmethod.pyi] |
| 555 |
|
| 556 |
[case testSelfTypePropertyUnion] |
| 557 |
from typing import Union |
| 558 |
class A: |
| 559 |
@property |
| 560 |
def f(self: A) -> int: pass |
| 561 |
|
| 562 |
class B: |
| 563 |
@property |
| 564 |
def f(self: B) -> int: pass |
| 565 |
x: Union[A, B] |
| 566 |
reveal_type(x.f) # N: Revealed type is "builtins.int" |
| 567 |
|
| 568 |
[builtins fixtures/property.pyi] |
| 569 |
|
| 570 |
[case testSelfTypeProperSupertypeAttribute] |
| 571 |
from typing import Callable, TypeVar, ClassVar |
| 572 |
class K: pass |
| 573 |
T = TypeVar('T', bound=K) |
| 574 |
class A(K): |
| 575 |
@property |
| 576 |
def g(self: K) -> int: return 0 |
| 577 |
@property |
| 578 |
def gt(self: T) -> T: return self |
| 579 |
f: ClassVar[Callable[[object], int]] |
| 580 |
ft: ClassVar[Callable[[T], T]] |
| 581 |
|
| 582 |
class B(A): |
| 583 |
pass |
| 584 |
|
| 585 |
reveal_type(A().g) # N: Revealed type is "builtins.int" |
| 586 |
reveal_type(A().gt) # N: Revealed type is "__main__.A" |
| 587 |
reveal_type(A().f()) # N: Revealed type is "builtins.int" |
| 588 |
reveal_type(A().ft()) # N: Revealed type is "__main__.A" |
| 589 |
reveal_type(B().g) # N: Revealed type is "builtins.int" |
| 590 |
reveal_type(B().gt) # N: Revealed type is "__main__.B" |
| 591 |
reveal_type(B().f()) # N: Revealed type is "builtins.int" |
| 592 |
reveal_type(B().ft()) # N: Revealed type is "__main__.B" |
| 593 |
|
| 594 |
[builtins fixtures/property.pyi] |
| 595 |
|
| 596 |
[case testSelfTypeProperSupertypeAttributeTuple] |
| 597 |
from typing import Callable, TypeVar, Tuple, ClassVar |
| 598 |
T = TypeVar('T') |
| 599 |
class A(Tuple[int, int]): |
| 600 |
@property |
| 601 |
def g(self: object) -> int: return 0 |
| 602 |
@property |
| 603 |
def gt(self: T) -> T: return self |
| 604 |
f: ClassVar[Callable[[object], int]] |
| 605 |
ft: ClassVar[Callable[[T], T]] |
| 606 |
|
| 607 |
class B(A): |
| 608 |
pass |
| 609 |
|
| 610 |
reveal_type(A().g) # N: Revealed type is "builtins.int" |
| 611 |
reveal_type(A().gt) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.A]" |
| 612 |
reveal_type(A().f()) # N: Revealed type is "builtins.int" |
| 613 |
reveal_type(A().ft()) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.A]" |
| 614 |
reveal_type(B().g) # N: Revealed type is "builtins.int" |
| 615 |
reveal_type(B().gt) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.B]" |
| 616 |
reveal_type(B().f()) # N: Revealed type is "builtins.int" |
| 617 |
reveal_type(B().ft()) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.B]" |
| 618 |
|
| 619 |
[builtins fixtures/property.pyi] |
| 620 |
|
| 621 |
[case testSelfTypeProperSupertypeAttributeMeta] |
| 622 |
from typing import Callable, TypeVar, Type, ClassVar |
| 623 |
T = TypeVar('T') |
| 624 |
class A(type): |
| 625 |
@property |
| 626 |
def g(cls: object) -> int: return 0 |
| 627 |
@property |
| 628 |
def gt(cls: T) -> T: return cls |
| 629 |
f: ClassVar[Callable[[object], int]] |
| 630 |
ft: ClassVar[Callable[[T], T]] |
| 631 |
|
| 632 |
class B(A): |
| 633 |
pass |
| 634 |
|
| 635 |
class X(metaclass=B): |
| 636 |
def __init__(self, x: int) -> None: pass |
| 637 |
class Y(X): pass |
| 638 |
X1: Type[X] |
| 639 |
reveal_type(X.g) # N: Revealed type is "builtins.int" |
| 640 |
reveal_type(X.gt) # N: Revealed type is "def (x: builtins.int) -> __main__.X" |
| 641 |
reveal_type(X.f()) # N: Revealed type is "builtins.int" |
| 642 |
reveal_type(X.ft()) # N: Revealed type is "def (x: builtins.int) -> __main__.X" |
| 643 |
reveal_type(Y.g) # N: Revealed type is "builtins.int" |
| 644 |
reveal_type(Y.gt) # N: Revealed type is "def (x: builtins.int) -> __main__.Y" |
| 645 |
reveal_type(Y.f()) # N: Revealed type is "builtins.int" |
| 646 |
reveal_type(Y.ft()) # N: Revealed type is "def (x: builtins.int) -> __main__.Y" |
| 647 |
reveal_type(X1.g) # N: Revealed type is "builtins.int" |
| 648 |
reveal_type(X1.gt) # N: Revealed type is "type[__main__.X]" |
| 649 |
reveal_type(X1.f()) # N: Revealed type is "builtins.int" |
| 650 |
reveal_type(X1.ft()) # N: Revealed type is "type[__main__.X]" |
| 651 |
|
| 652 |
[builtins fixtures/property.pyi] |
| 653 |
|
| 654 |
[case testSelfTypeProperSupertypeAttributeGeneric] |
| 655 |
from typing import Callable, TypeVar, Generic, ClassVar |
| 656 |
Q = TypeVar('Q', covariant=True) |
| 657 |
class K(Generic[Q]): |
| 658 |
q: Q |
| 659 |
T = TypeVar('T') |
| 660 |
class A(K[Q]): |
| 661 |
@property |
| 662 |
def g(self: K[object]) -> int: return 0 |
| 663 |
@property |
| 664 |
def gt(self: K[T]) -> T: return self.q |
| 665 |
f: ClassVar[Callable[[object], int]] |
| 666 |
ft: ClassVar[Callable[[T], T]] |
| 667 |
|
| 668 |
class B(A[Q]): |
| 669 |
pass |
| 670 |
a: A[int] |
| 671 |
b: B[str] |
| 672 |
reveal_type(a.g) # N: Revealed type is "builtins.int" |
| 673 |
reveal_type(a.gt) # N: Revealed type is "builtins.int" |
| 674 |
reveal_type(a.f()) # N: Revealed type is "builtins.int" |
| 675 |
reveal_type(a.ft()) # N: Revealed type is "__main__.A[builtins.int]" |
| 676 |
reveal_type(b.g) # N: Revealed type is "builtins.int" |
| 677 |
reveal_type(b.gt) # N: Revealed type is "builtins.str" |
| 678 |
reveal_type(b.f()) # N: Revealed type is "builtins.int" |
| 679 |
reveal_type(b.ft()) # N: Revealed type is "__main__.B[builtins.str]" |
| 680 |
[builtins fixtures/property.pyi] |
| 681 |
|
| 682 |
[case testSelfTypeRestrictedMethod] |
| 683 |
from typing import TypeVar, Generic |
| 684 |
|
| 685 |
T = TypeVar('T') |
| 686 |
class C(Generic[T]): |
| 687 |
def from_item(self: C[str]) -> None: ... |
| 688 |
|
| 689 |
i: C[int] |
| 690 |
s: C[str] |
| 691 |
|
| 692 |
i.from_item() # E: Invalid self argument "C[int]" to attribute function "from_item" with type "Callable[[C[str]], None]" |
| 693 |
s.from_item() |
| 694 |
|
| 695 |
[case testSelfTypeRestrictedClassMethod] |
| 696 |
from typing import TypeVar, Generic, Type |
| 697 |
|
| 698 |
T = TypeVar('T') |
| 699 |
class C(Generic[T]): |
| 700 |
@classmethod |
| 701 |
def from_item(cls: Type[C[str]]) -> None: ... |
| 702 |
|
| 703 |
class DI(C[int]): ... |
| 704 |
class DS(C[str]): ... |
| 705 |
|
| 706 |
DI().from_item() # E: Invalid self argument "type[DI]" to class attribute function "from_item" with type "Callable[[type[C[str]]], None]" |
| 707 |
DS().from_item() |
| 708 |
DI.from_item() # E: Invalid self argument "type[DI]" to attribute function "from_item" with type "Callable[[type[C[str]]], None]" |
| 709 |
DS.from_item() |
| 710 |
[builtins fixtures/classmethod.pyi] |
| 711 |
|
| 712 |
[case testSelfTypeRestrictedMethodOverload] |
| 713 |
from typing import TypeVar, Generic, overload, Tuple |
| 714 |
|
| 715 |
T = TypeVar('T') |
| 716 |
class C(Generic[T]): |
| 717 |
@overload |
| 718 |
def from_item(self: C[str], item: str) -> None: ... |
| 719 |
@overload |
| 720 |
def from_item(self: C[int], item: Tuple[int]) -> None: ... |
| 721 |
def from_item(self, item): |
| 722 |
... |
| 723 |
|
| 724 |
ci: C[int] |
| 725 |
cs: C[str] |
| 726 |
reveal_type(ci.from_item) # N: Revealed type is "def (item: tuple[builtins.int])" |
| 727 |
reveal_type(cs.from_item) # N: Revealed type is "def (item: builtins.str)" |
| 728 |
[builtins fixtures/tuple.pyi] |
| 729 |
|
| 730 |
[case testSelfTypeRestrictedMethodOverloadFallback] |
| 731 |
from typing import TypeVar, Generic, overload, Callable |
| 732 |
|
| 733 |
T = TypeVar('T') |
| 734 |
class C(Generic[T]): |
| 735 |
@overload |
| 736 |
def from_item(self: C[str]) -> str: ... |
| 737 |
@overload |
| 738 |
def from_item(self, converter: Callable[[T], str]) -> str: ... |
| 739 |
def from_item(self, converter): |
| 740 |
... |
| 741 |
|
| 742 |
ci: C[int] |
| 743 |
cs: C[str] |
| 744 |
reveal_type(cs.from_item()) # N: Revealed type is "builtins.str" |
| 745 |
ci.from_item() # E: Missing positional argument "converter" in call to "from_item" of "C" |
| 746 |
|
| 747 |
def conv(x: int) -> str: ... |
| 748 |
def bad(x: str) -> str: ... |
| 749 |
reveal_type(ci.from_item(conv)) # N: Revealed type is "builtins.str" |
| 750 |
ci.from_item(bad) # E: Argument 1 to "from_item" of "C" has incompatible type "Callable[[str], str]"; expected "Callable[[int], str]" |
| 751 |
|
| 752 |
[case testSelfTypeRestrictedMethodOverloadInit] |
| 753 |
from typing import TypeVar |
| 754 |
from lib import P, C |
| 755 |
|
| 756 |
reveal_type(P) # N: Revealed type is "Overload(def [T] (use_str: Literal[True]) -> lib.P[builtins.str], def [T] (use_str: Literal[False]) -> lib.P[builtins.int])" |
| 757 |
reveal_type(P(use_str=True)) # N: Revealed type is "lib.P[builtins.str]" |
| 758 |
reveal_type(P(use_str=False)) # N: Revealed type is "lib.P[builtins.int]" |
| 759 |
|
| 760 |
reveal_type(C) # N: Revealed type is "Overload(def [T] (item: T`1, use_tuple: Literal[False]) -> lib.C[T`1], def [T] (item: T`1, use_tuple: Literal[True]) -> lib.C[builtins.tuple[T`1, ...]])" |
| 761 |
reveal_type(C(0, use_tuple=False)) # N: Revealed type is "lib.C[builtins.int]" |
| 762 |
reveal_type(C(0, use_tuple=True)) # N: Revealed type is "lib.C[builtins.tuple[builtins.int, ...]]" |
| 763 |
|
| 764 |
T = TypeVar('T') |
| 765 |
class SubP(P[T]): |
| 766 |
pass |
| 767 |
|
| 768 |
SubP('no') # E: No overload variant of "SubP" matches argument type "str" \ |
| 769 |
# N: Possible overload variants: \ |
| 770 |
# N: def [T] SubP(use_str: Literal[True]) -> SubP[T] \ |
| 771 |
# N: def [T] SubP(use_str: Literal[False]) -> SubP[T] |
| 772 |
|
| 773 |
# This is a bit unfortunate: we don't have a way to map the overloaded __init__ to subtype. |
| 774 |
x = SubP(use_str=True) # E: Need type annotation for "x" |
| 775 |
reveal_type(x) # N: Revealed type is "__main__.SubP[Any]" |
| 776 |
y: SubP[str] = SubP(use_str=True) |
| 777 |
|
| 778 |
[file lib.pyi] |
| 779 |
from typing import Literal, TypeVar, Generic, overload, Tuple |
| 780 |
|
| 781 |
T = TypeVar('T') |
| 782 |
class P(Generic[T]): |
| 783 |
@overload |
| 784 |
def __init__(self: P[str], use_str: Literal[True]) -> None: ... |
| 785 |
@overload |
| 786 |
def __init__(self: P[int], use_str: Literal[False]) -> None: ... |
| 787 |
|
| 788 |
class C(Generic[T]): |
| 789 |
@overload |
| 790 |
def __init__(self: C[T], item: T, use_tuple: Literal[False]) -> None: ... |
| 791 |
@overload |
| 792 |
def __init__(self: C[Tuple[T, ...]], item: T, use_tuple: Literal[True]) -> None: ... |
| 793 |
[builtins fixtures/bool.pyi] |
| 794 |
|
| 795 |
[case testSelfTypeRestrictedMethodOverloadInitFallBacks] |
| 796 |
from lib import PFallBack, PFallBackAny |
| 797 |
|
| 798 |
t: bool |
| 799 |
xx = PFallBack(t) # E: Need type annotation for "xx" |
| 800 |
yy = PFallBackAny(t) # OK |
| 801 |
|
| 802 |
[file lib.pyi] |
| 803 |
from typing import Literal, TypeVar, Generic, overload, Tuple, Any |
| 804 |
|
| 805 |
class PFallBack(Generic[T]): |
| 806 |
@overload |
| 807 |
def __init__(self: PFallBack[str], use_str: Literal[True]) -> None: ... |
| 808 |
@overload |
| 809 |
def __init__(self: PFallBack[int], use_str: Literal[False]) -> None: ... |
| 810 |
@overload |
| 811 |
def __init__(self, use_str: bool) -> None: ... |
| 812 |
|
| 813 |
class PFallBackAny(Generic[T]): |
| 814 |
@overload |
| 815 |
def __init__(self: PFallBackAny[str], use_str: Literal[True]) -> None: ... |
| 816 |
@overload |
| 817 |
def __init__(self: PFallBackAny[int], use_str: Literal[False]) -> None: ... |
| 818 |
@overload |
| 819 |
def __init__(self: PFallBackAny[Any], use_str: bool) -> None: ... |
| 820 |
[builtins fixtures/bool.pyi] |
| 821 |
|
| 822 |
[case testSelfTypeRestrictedMethodOverloadInitBadTypeNoCrash] |
| 823 |
from lib import P |
| 824 |
P(0) |
| 825 |
[file lib.pyi] |
| 826 |
from typing import overload |
| 827 |
|
| 828 |
class P: |
| 829 |
@overload |
| 830 |
def __init__(self: Bad, x: int) -> None: ... # E: Name "Bad" is not defined |
| 831 |
@overload |
| 832 |
def __init__(self) -> None: ... |
| 833 |
|
| 834 |
[case testSelfTypeNarrowBinding] |
| 835 |
from typing import TypeVar, List, Generic |
| 836 |
|
| 837 |
T = TypeVar('T') |
| 838 |
S = TypeVar('S') |
| 839 |
|
| 840 |
class Base(Generic[T]): |
| 841 |
def get_item(self: Base[List[S]]) -> S: ... |
| 842 |
|
| 843 |
class Sub(Base[List[int]]): ... |
| 844 |
class BadSub(Base[int]): ... |
| 845 |
|
| 846 |
reveal_type(Sub().get_item()) # N: Revealed type is "builtins.int" |
| 847 |
BadSub().get_item() # E: Invalid self argument "BadSub" to attribute function "get_item" with type "Callable[[Base[list[S]]], S]" |
| 848 |
[builtins fixtures/list.pyi] |
| 849 |
|
| 850 |
[case testMixinAllowedWithProtocol] |
| 851 |
from typing import Protocol, TypeVar |
| 852 |
|
| 853 |
class Resource(Protocol): |
| 854 |
def close(self) -> int: ... |
| 855 |
|
| 856 |
class AtomicClose: |
| 857 |
def atomic_close(self: Resource) -> int: |
| 858 |
return self.close() |
| 859 |
|
| 860 |
T = TypeVar('T', bound=Resource) |
| 861 |
class Copyable: |
| 862 |
def copy(self: T) -> T: ... |
| 863 |
|
| 864 |
class File(AtomicClose, Copyable): |
| 865 |
def close(self) -> int: |
| 866 |
... |
| 867 |
|
| 868 |
class Bad(AtomicClose, Copyable): |
| 869 |
... |
| 870 |
|
| 871 |
f: File |
| 872 |
b: Bad |
| 873 |
f.atomic_close() # OK |
| 874 |
b.atomic_close() # E: Invalid self argument "Bad" to attribute function "atomic_close" with type "Callable[[Resource], int]" |
| 875 |
|
| 876 |
reveal_type(f.copy()) # N: Revealed type is "__main__.File" |
| 877 |
b.copy() # E: Invalid self argument "Bad" to attribute function "copy" with type "Callable[[T], T]" |
| 878 |
[builtins fixtures/tuple.pyi] |
| 879 |
|
| 880 |
[case testMixinProtocolSuper] |
| 881 |
from typing import Protocol |
| 882 |
|
| 883 |
class Base(Protocol): |
| 884 |
def func(self) -> int: |
| 885 |
... |
| 886 |
|
| 887 |
class TweakFunc: |
| 888 |
def func(self: Base) -> int: |
| 889 |
return reveal_type(super().func()) # E: Call to abstract method "func" of "Base" with trivial body via super() is unsafe \ |
| 890 |
# N: Revealed type is "builtins.int" |
| 891 |
|
| 892 |
class Good: |
| 893 |
def func(self) -> int: ... |
| 894 |
class C(TweakFunc, Good): pass |
| 895 |
C().func() # OK |
| 896 |
|
| 897 |
class Bad: |
| 898 |
def func(self) -> str: ... |
| 899 |
class CC(TweakFunc, Bad): pass # E: Definition of "func" in base class "TweakFunc" is incompatible with definition in base class "Bad" |
| 900 |
|
| 901 |
[case testBadClassLevelDecoratorHack] |
| 902 |
from typing import Protocol, TypeVar, Any |
| 903 |
|
| 904 |
class FuncLike(Protocol): |
| 905 |
__call__: Any |
| 906 |
F = TypeVar('F', bound=FuncLike) |
| 907 |
|
| 908 |
class Test: |
| 909 |
def _deco(func: F) -> F: ... |
| 910 |
|
| 911 |
@_deco |
| 912 |
def meth(self, x: str) -> int: ... |
| 913 |
|
| 914 |
reveal_type(Test().meth) # N: Revealed type is "def (x: builtins.str) -> builtins.int" |
| 915 |
Test()._deco # E: Invalid self argument "Test" to attribute function "_deco" with type "Callable[[F], F]" |
| 916 |
[builtins fixtures/tuple.pyi] |
| 917 |
|
| 918 |
[case testSelfTypeTrickyExample] |
| 919 |
from typing import * |
| 920 |
|
| 921 |
In = TypeVar('In') |
| 922 |
Out = TypeVar('Out') |
| 923 |
Mid = TypeVar('Mid') |
| 924 |
NewOut = TypeVar('NewOut') |
| 925 |
|
| 926 |
class Lnk(Generic[In, Out]): |
| 927 |
def test(self: Lnk[In, Mid], other: Lnk[Mid, NewOut]) -> Lnk[In, NewOut]: ... |
| 928 |
|
| 929 |
class X: pass |
| 930 |
class Y: pass |
| 931 |
class Z: pass |
| 932 |
|
| 933 |
a: Lnk[X, Y] = Lnk() |
| 934 |
b: Lnk[Y, Z] = Lnk() |
| 935 |
|
| 936 |
a.test(b) |
| 937 |
b.test(a) # E: Argument 1 to "test" of "Lnk" has incompatible type "Lnk[X, Y]"; expected "Lnk[Z, Y]" |
| 938 |
|
| 939 |
[case testSelfTypeReallyTrickyExample] |
| 940 |
from typing import * |
| 941 |
|
| 942 |
In = TypeVar('In') |
| 943 |
Out = TypeVar('Out') |
| 944 |
Other = TypeVar('Other') |
| 945 |
|
| 946 |
_1 = TypeVar('_1') |
| 947 |
_2 = TypeVar('_2') |
| 948 |
__1 = TypeVar('__1') |
| 949 |
__2 = TypeVar('__2') |
| 950 |
|
| 951 |
class Lnk(Generic[In, Out]): |
| 952 |
@overload |
| 953 |
def __rshift__(self, other: Lnk[Out, Other]) -> Lnk[In,Other]: ... |
| 954 |
@overload |
| 955 |
def __rshift__(self: Lnk[In, Tuple[_1, _2]], |
| 956 |
other: Tuple[Lnk[_1, __1], Lnk[_2, __2]]) -> Lnk[In, Tuple[__1, __2]]: ... |
| 957 |
def __rshift__(self: Any, other: Any) -> Any: |
| 958 |
... |
| 959 |
|
| 960 |
a: Lnk[str, Tuple[str, int]] = Lnk() |
| 961 |
b: Lnk[str, int] = Lnk() |
| 962 |
c: Lnk[int, float] = Lnk() |
| 963 |
|
| 964 |
d: Lnk[str, float] = b >> c # OK |
| 965 |
e: Lnk[str, Tuple[int, float]] = a >> (b, c) # OK |
| 966 |
f: Lnk[str, Tuple[float, int]] = a >> (c, b) # E: Unsupported operand types for >> ("Lnk[str, tuple[str, int]]" and "tuple[Lnk[int, float], Lnk[str, int]]") |
| 967 |
[builtins fixtures/tuple.pyi] |
| 968 |
|
| 969 |
[case testSelfTypeMutuallyExclusiveRestrictions] |
| 970 |
from typing import Generic, TypeVar |
| 971 |
|
| 972 |
T = TypeVar('T') |
| 973 |
|
| 974 |
class Foo(Generic[T]): |
| 975 |
def f1(self: Foo[str]) -> None: |
| 976 |
self.f2() # E: Invalid self argument "Foo[str]" to attribute function "f2" with type "Callable[[Foo[int]], None]" |
| 977 |
def f2(self: Foo[int]) -> None: |
| 978 |
self.f1() # E: Invalid self argument "Foo[int]" to attribute function "f1" with type "Callable[[Foo[str]], None]" |
| 979 |
|
| 980 |
[case testSelfTypeStructureMetaclassMatch] |
| 981 |
from typing import TypeVar, Type, Generic, cast |
| 982 |
|
| 983 |
Cls = TypeVar('Cls') |
| 984 |
T = TypeVar('T') |
| 985 |
|
| 986 |
class Manager(Generic[Cls]): |
| 987 |
def create(self: Manager[Type[T]]) -> T: ... |
| 988 |
|
| 989 |
class ModelMeta(type): |
| 990 |
@property |
| 991 |
def objects(cls: T) -> Manager[T]: ... |
| 992 |
|
| 993 |
class Model(metaclass=ModelMeta): |
| 994 |
pass |
| 995 |
|
| 996 |
class Dog(Model): ... |
| 997 |
class Cat(Model): ... |
| 998 |
|
| 999 |
c: Cat = Dog.objects.create() # E: Incompatible types in assignment (expression has type "Dog", variable has type "Cat") |
| 1000 |
d: Dog = Dog.objects.create() |
| 1001 |
[builtins fixtures/property.pyi] |
| 1002 |
|
| 1003 |
[case testSelfTypeProtocolMetaclassMatch] |
| 1004 |
from typing import Type, TypeVar, Protocol |
| 1005 |
|
| 1006 |
class HasX(Protocol): |
| 1007 |
x: int |
| 1008 |
|
| 1009 |
T = TypeVar('T', bound=HasX) |
| 1010 |
|
| 1011 |
class Meta(type): |
| 1012 |
def do_x(cls: Type[T]) -> T: |
| 1013 |
cls.x |
| 1014 |
return cls() |
| 1015 |
|
| 1016 |
class Good(metaclass=Meta): |
| 1017 |
x: int |
| 1018 |
class Bad(metaclass=Meta): |
| 1019 |
pass |
| 1020 |
|
| 1021 |
Good.do_x() |
| 1022 |
Bad.do_x() # E: Invalid self argument "type[Bad]" to attribute function "do_x" with type "Callable[[type[T]], T]" |
| 1023 |
|
| 1024 |
[case testSelfTypeProtocolClassmethodMatch] |
| 1025 |
from typing import Type, TypeVar, Protocol |
| 1026 |
|
| 1027 |
T = TypeVar('T') |
| 1028 |
|
| 1029 |
class HasDoX(Protocol): |
| 1030 |
@classmethod |
| 1031 |
def do_x(cls: Type[T]) -> T: |
| 1032 |
... |
| 1033 |
|
| 1034 |
class Good: |
| 1035 |
@classmethod |
| 1036 |
def do_x(cls) -> 'Good': |
| 1037 |
... |
| 1038 |
|
| 1039 |
class Bad: |
| 1040 |
@classmethod |
| 1041 |
def do_x(cls) -> Good: |
| 1042 |
... |
| 1043 |
|
| 1044 |
good: HasDoX = Good() |
| 1045 |
bad: HasDoX = Bad() |
| 1046 |
[builtins fixtures/classmethod.pyi] |
| 1047 |
[out] |
| 1048 |
main:21: error: Incompatible types in assignment (expression has type "Bad", variable has type "HasDoX") |
| 1049 |
main:21: note: Following member(s) of "Bad" have conflicts: |
| 1050 |
main:21: note: Expected: |
| 1051 |
main:21: note: def do_x(cls) -> Bad |
| 1052 |
main:21: note: Got: |
| 1053 |
main:21: note: def do_x(cls) -> Good |
| 1054 |
|
| 1055 |
[case testSelfTypeNotSelfType] |
| 1056 |
# Friendlier error messages for common mistakes. See #2950 |
| 1057 |
class A: |
| 1058 |
def f(x: int) -> None: ... |
| 1059 |
def g(self: None) -> None: ... |
| 1060 |
[out] |
| 1061 |
main:3: error: "self" parameter missing for a non-static method (or an invalid type for self) |
| 1062 |
main:4: error: The erased type of self "None" is not a supertype of its class "__main__.A" |
| 1063 |
|
| 1064 |
[case testUnionPropertyField] |
| 1065 |
from typing import Union |
| 1066 |
|
| 1067 |
class A: |
| 1068 |
x: int |
| 1069 |
|
| 1070 |
class B: |
| 1071 |
@property |
| 1072 |
def x(self) -> int: return 1 |
| 1073 |
|
| 1074 |
class C: |
| 1075 |
@property |
| 1076 |
def x(self) -> int: return 1 |
| 1077 |
|
| 1078 |
ab: Union[A, B, C] |
| 1079 |
reveal_type(ab.x) # N: Revealed type is "builtins.int" |
| 1080 |
[builtins fixtures/property.pyi] |
| 1081 |
|
| 1082 |
[case testSelfTypeNoTypeVars] |
| 1083 |
from typing import Generic, List, Optional, TypeVar, Any |
| 1084 |
|
| 1085 |
Q = TypeVar("Q") |
| 1086 |
T = TypeVar("T", bound='Super[Any]') |
| 1087 |
|
| 1088 |
class Super(Generic[Q]): |
| 1089 |
@classmethod |
| 1090 |
def meth(cls, arg: List[T]) -> List[T]: |
| 1091 |
pass |
| 1092 |
|
| 1093 |
class Sub(Super[int]): ... |
| 1094 |
|
| 1095 |
def test(x: List[Sub]) -> None: |
| 1096 |
reveal_type(Sub.meth(x)) # N: Revealed type is "builtins.list[__main__.Sub]" |
| 1097 |
[builtins fixtures/isinstancelist.pyi] |
| 1098 |
|
| 1099 |
[case testSelfTypeNoTypeVarsRestrict] |
| 1100 |
from typing import Generic, TypeVar |
| 1101 |
|
| 1102 |
T = TypeVar('T') |
| 1103 |
S = TypeVar('S') |
| 1104 |
|
| 1105 |
class C(Generic[T]): |
| 1106 |
def limited(self: C[str], arg: S) -> S: ... |
| 1107 |
|
| 1108 |
reveal_type(C[str]().limited(0)) # N: Revealed type is "builtins.int" |
| 1109 |
|
| 1110 |
[case testSelfTypeMultipleTypeVars] |
| 1111 |
from typing import Generic, TypeVar, Tuple |
| 1112 |
|
| 1113 |
T = TypeVar('T') |
| 1114 |
S = TypeVar('S') |
| 1115 |
U = TypeVar('U') |
| 1116 |
V = TypeVar('V') |
| 1117 |
|
| 1118 |
class C(Generic[T]): |
| 1119 |
def magic(self: C[Tuple[S, U]]) -> Tuple[T, S, U]: ... |
| 1120 |
|
| 1121 |
class D(Generic[V]): |
| 1122 |
def f(self) -> None: |
| 1123 |
reveal_type(C[Tuple[V, str]]().magic()) # N: Revealed type is "tuple[tuple[V`1, builtins.str], V`1, builtins.str]" |
| 1124 |
[builtins fixtures/tuple.pyi] |
| 1125 |
|
| 1126 |
[case testSelfTypeOnUnion] |
| 1127 |
from typing import TypeVar, Union |
| 1128 |
|
| 1129 |
T = TypeVar('T') |
| 1130 |
|
| 1131 |
class A: |
| 1132 |
same: int |
| 1133 |
|
| 1134 |
class C: |
| 1135 |
def same(self: T) -> T: ... |
| 1136 |
|
| 1137 |
x: Union[A, C] |
| 1138 |
reveal_type(x.same) # N: Revealed type is "builtins.int | (def () -> __main__.C)" |
| 1139 |
|
| 1140 |
[case testSelfTypeOnUnionClassMethod] |
| 1141 |
from typing import TypeVar, Union, Type |
| 1142 |
|
| 1143 |
T = TypeVar('T') |
| 1144 |
|
| 1145 |
class A: |
| 1146 |
same: int |
| 1147 |
|
| 1148 |
class C: |
| 1149 |
@classmethod |
| 1150 |
def same(cls: Type[T]) -> T: ... |
| 1151 |
|
| 1152 |
x: Union[A, C] |
| 1153 |
reveal_type(x.same) # N: Revealed type is "builtins.int | (def () -> __main__.C)" |
| 1154 |
[builtins fixtures/classmethod.pyi] |
| 1155 |
|
| 1156 |
[case SelfTypeOverloadedClassMethod] |
| 1157 |
from lib import Base |
| 1158 |
from typing import overload, Tuple |
| 1159 |
|
| 1160 |
class Sub(Base): |
| 1161 |
@overload |
| 1162 |
@classmethod |
| 1163 |
def make(cls) -> Sub: ... |
| 1164 |
@overload |
| 1165 |
@classmethod |
| 1166 |
def make(cls, num: int) -> Tuple[Sub, ...]: ... |
| 1167 |
@classmethod |
| 1168 |
def make(cls, num=1): |
| 1169 |
... |
| 1170 |
|
| 1171 |
class Other(Base): ... |
| 1172 |
class Double(Sub): ... |
| 1173 |
|
| 1174 |
reveal_type(Other.make()) # N: Revealed type is "__main__.Other" |
| 1175 |
reveal_type(Other.make(3)) # N: Revealed type is "builtins.tuple[__main__.Other, ...]" |
| 1176 |
reveal_type(Double.make()) # N: Revealed type is "__main__.Sub" |
| 1177 |
reveal_type(Double.make(3)) # N: Revealed type is "builtins.tuple[__main__.Sub, ...]" |
| 1178 |
[file lib.pyi] |
| 1179 |
from typing import overload, TypeVar, Type, Tuple |
| 1180 |
|
| 1181 |
T = TypeVar('T', bound=Base) |
| 1182 |
|
| 1183 |
class Base: |
| 1184 |
@overload |
| 1185 |
@classmethod |
| 1186 |
def make(cls: Type[T]) -> T: ... |
| 1187 |
@overload |
| 1188 |
@classmethod |
| 1189 |
def make(cls: Type[T], num: int) -> Tuple[T, ...]: ... |
| 1190 |
[builtins fixtures/classmethod.pyi] |
| 1191 |
|
| 1192 |
[case testSelfTypeClassMethodOnUnion] |
| 1193 |
from typing import Type, Union, TypeVar |
| 1194 |
|
| 1195 |
T = TypeVar('T') |
| 1196 |
|
| 1197 |
class A: |
| 1198 |
@classmethod |
| 1199 |
def meth(cls: Type[T]) -> T: ... |
| 1200 |
class B(A): ... |
| 1201 |
class C(A): ... |
| 1202 |
|
| 1203 |
t: Type[Union[B, C]] |
| 1204 |
reveal_type(t.meth) # N: Revealed type is "(def () -> __main__.B) | (def () -> __main__.C)" |
| 1205 |
x = t.meth() |
| 1206 |
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" |
| 1207 |
[builtins fixtures/classmethod.pyi] |
| 1208 |
|
| 1209 |
[case testSelfTypeClassMethodOnUnionGeneric] |
| 1210 |
from typing import Type, Union, TypeVar, Generic |
| 1211 |
|
| 1212 |
T = TypeVar('T') |
| 1213 |
S = TypeVar('S') |
| 1214 |
|
| 1215 |
class A(Generic[T]): |
| 1216 |
@classmethod |
| 1217 |
def meth(cls: Type[S]) -> S: ... |
| 1218 |
|
| 1219 |
t: Type[Union[A[int], A[str]]] |
| 1220 |
x = t.meth() |
| 1221 |
reveal_type(x) # N: Revealed type is "__main__.A[builtins.int] | __main__.A[builtins.str]" |
| 1222 |
[builtins fixtures/classmethod.pyi] |
| 1223 |
|
| 1224 |
[case testSelfTypeClassMethodOnUnionList] |
| 1225 |
from typing import Type, Union, TypeVar, List |
| 1226 |
|
| 1227 |
T = TypeVar('T') |
| 1228 |
|
| 1229 |
class A: |
| 1230 |
@classmethod |
| 1231 |
def meth(cls: Type[T]) -> List[T]: ... |
| 1232 |
class B(A): ... |
| 1233 |
class C(A): ... |
| 1234 |
|
| 1235 |
t: Type[Union[B, C]] |
| 1236 |
x = t.meth()[0] |
| 1237 |
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" |
| 1238 |
[builtins fixtures/isinstancelist.pyi] |
| 1239 |
|
| 1240 |
[case testSelfTypeClassMethodOverloadedOnInstance] |
| 1241 |
from typing import Optional, Type, TypeVar, overload, Union |
| 1242 |
|
| 1243 |
Id = int |
| 1244 |
|
| 1245 |
A = TypeVar("A", bound='AClass') |
| 1246 |
|
| 1247 |
class AClass: |
| 1248 |
@overload |
| 1249 |
@classmethod |
| 1250 |
def delete(cls: Type[A], id: Id, id2: Id) -> Optional[int]: ... |
| 1251 |
|
| 1252 |
@overload |
| 1253 |
@classmethod |
| 1254 |
def delete(cls: Type[A], id: A, id2: None = None) -> Optional[int]: ... |
| 1255 |
|
| 1256 |
@classmethod |
| 1257 |
def delete(cls: Type[A], id: Union[A, Id], id2: Optional[Id] = None) -> Optional[int]: |
| 1258 |
... |
| 1259 |
|
| 1260 |
def foo(x: Type[AClass]) -> None: |
| 1261 |
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" |
| 1262 |
y = x() |
| 1263 |
reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" |
| 1264 |
y.delete(10, 20) |
| 1265 |
y.delete(y) |
| 1266 |
|
| 1267 |
def bar(x: AClass) -> None: |
| 1268 |
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" |
| 1269 |
x.delete(10, 20) |
| 1270 |
[builtins fixtures/classmethod.pyi] |
| 1271 |
|
| 1272 |
[case testSelfTypeBadTypeIgnoredInConstructor] |
| 1273 |
class Base: ... |
| 1274 |
class Sub(Base): |
| 1275 |
def __init__(self: Base) -> None: ... |
| 1276 |
|
| 1277 |
reveal_type(Sub()) # N: Revealed type is "__main__.Sub" |
| 1278 |
|
| 1279 |
[case testSelfTypeBadTypeIgnoredInConstructorGeneric] |
| 1280 |
from typing import Generic, TypeVar |
| 1281 |
|
| 1282 |
T = TypeVar('T') |
| 1283 |
|
| 1284 |
class Base(Generic[T]): ... |
| 1285 |
class Sub(Base[T]): |
| 1286 |
def __init__(self: Base[T], item: T) -> None: ... |
| 1287 |
|
| 1288 |
reveal_type(Sub(42)) # N: Revealed type is "__main__.Sub[builtins.int]" |
| 1289 |
|
| 1290 |
[case testSelfTypeBadTypeIgnoredInConstructorOverload] |
| 1291 |
from typing import overload |
| 1292 |
|
| 1293 |
class Base: ... |
| 1294 |
class Sub(Base): |
| 1295 |
@overload |
| 1296 |
def __init__(self: Sub, item: int) -> None: ... |
| 1297 |
@overload |
| 1298 |
def __init__(self: Base) -> None: ... |
| 1299 |
def __init__(self, item=None): |
| 1300 |
... |
| 1301 |
|
| 1302 |
reveal_type(Sub) # N: Revealed type is "Overload(def (item: builtins.int) -> __main__.Sub, def () -> __main__.Sub)" |
| 1303 |
|
| 1304 |
[case testSelfTypeBadTypeIgnoredInConstructorAbstract] |
| 1305 |
from abc import abstractmethod |
| 1306 |
from typing import Protocol |
| 1307 |
|
| 1308 |
class Blah(Protocol): |
| 1309 |
@abstractmethod |
| 1310 |
def something(self) -> None: ... |
| 1311 |
|
| 1312 |
class Concrete(Blah): |
| 1313 |
def __init__(self: Blah) -> None: ... |
| 1314 |
def something(self) -> None: ... |
| 1315 |
|
| 1316 |
Concrete() # OK |
| 1317 |
|
| 1318 |
[case testSelfTypeGenericClassNoClashInstanceMethod] |
| 1319 |
from typing import TypeVar, Generic |
| 1320 |
|
| 1321 |
M = TypeVar("M") |
| 1322 |
T = TypeVar("T") |
| 1323 |
S = TypeVar("S") |
| 1324 |
|
| 1325 |
class Descriptor(Generic[M]): ... |
| 1326 |
|
| 1327 |
class BaseWrapper(Generic[M]): |
| 1328 |
def create_wrapper(self: T, metric_descriptor: Descriptor[M]) -> T: ... |
| 1329 |
class SubWrapper(BaseWrapper[M]): ... |
| 1330 |
|
| 1331 |
def build_wrapper(descriptor: Descriptor[M]) -> BaseWrapper[M]: |
| 1332 |
wrapper: BaseWrapper[M] |
| 1333 |
return wrapper.create_wrapper(descriptor) |
| 1334 |
|
| 1335 |
def build_sub_wrapper(descriptor: Descriptor[S]) -> SubWrapper[S]: |
| 1336 |
wrapper: SubWrapper[S] |
| 1337 |
x = wrapper.create_wrapper(descriptor) |
| 1338 |
reveal_type(x) # N: Revealed type is "__main__.SubWrapper[S`-1]" |
| 1339 |
return x |
| 1340 |
|
| 1341 |
[case testSelfTypeGenericClassNoClashClassMethod] |
| 1342 |
from typing import TypeVar, Generic, Type |
| 1343 |
|
| 1344 |
M = TypeVar("M") |
| 1345 |
T = TypeVar("T") |
| 1346 |
S = TypeVar("S") |
| 1347 |
|
| 1348 |
class Descriptor(Generic[M]): ... |
| 1349 |
|
| 1350 |
class BaseWrapper(Generic[M]): |
| 1351 |
@classmethod |
| 1352 |
def create_wrapper(cls: Type[T], metric_descriptor: Descriptor[M]) -> T: ... |
| 1353 |
class SubWrapper(BaseWrapper[M]): ... |
| 1354 |
|
| 1355 |
def build_wrapper(descriptor: Descriptor[M]) -> BaseWrapper[M]: |
| 1356 |
wrapper_cls: Type[BaseWrapper[M]] |
| 1357 |
return wrapper_cls.create_wrapper(descriptor) |
| 1358 |
|
| 1359 |
def build_sub_wrapper(descriptor: Descriptor[S]) -> SubWrapper[S]: |
| 1360 |
wrapper_cls: Type[SubWrapper[S]] |
| 1361 |
x = wrapper_cls.create_wrapper(descriptor) |
| 1362 |
reveal_type(x) # N: Revealed type is "__main__.SubWrapper[S`-1]" |
| 1363 |
return x |
| 1364 |
[builtins fixtures/classmethod.pyi] |
| 1365 |
|
| 1366 |
[case testSelfTypeGenericClassNoClashClassMethodClassObject] |
| 1367 |
from typing import TypeVar, Generic, Type |
| 1368 |
|
| 1369 |
M = TypeVar("M") |
| 1370 |
T = TypeVar("T") |
| 1371 |
|
| 1372 |
class Descriptor(Generic[M]): ... |
| 1373 |
|
| 1374 |
class BaseWrapper(Generic[M]): |
| 1375 |
@classmethod |
| 1376 |
def create_wrapper(cls: Type[T], metric_descriptor: Descriptor[M]) -> T: ... |
| 1377 |
class SubWrapper(BaseWrapper[M]): ... |
| 1378 |
|
| 1379 |
def build_wrapper(descriptor: Descriptor[M]) -> BaseWrapper[M]: |
| 1380 |
return BaseWrapper.create_wrapper(descriptor) |
| 1381 |
|
| 1382 |
def build_sub_wrapper(descriptor: Descriptor[M]) -> SubWrapper[M]: |
| 1383 |
x = SubWrapper.create_wrapper(descriptor) |
| 1384 |
reveal_type(x) # N: Revealed type is "__main__.SubWrapper[M`-1]" |
| 1385 |
return x |
| 1386 |
|
| 1387 |
def build_wrapper_non_gen(descriptor: Descriptor[int]) -> BaseWrapper[str]: |
| 1388 |
return BaseWrapper.create_wrapper(descriptor) # E: Argument 1 to "create_wrapper" of "BaseWrapper" has incompatible type "Descriptor[int]"; expected "Descriptor[str]" |
| 1389 |
|
| 1390 |
def build_sub_wrapper_non_gen(descriptor: Descriptor[int]) -> SubWrapper[str]: |
| 1391 |
return SubWrapper.create_wrapper(descriptor) # E: Argument 1 to "create_wrapper" of "BaseWrapper" has incompatible type "Descriptor[int]"; expected "Descriptor[str]" |
| 1392 |
[builtins fixtures/classmethod.pyi] |
| 1393 |
|
| 1394 |
[case testSelfTypeInGenericClassUsedFromAnotherGenericClass1] |
| 1395 |
from typing import TypeVar, Generic, Iterator, List, Tuple |
| 1396 |
|
| 1397 |
_T_co = TypeVar("_T_co", covariant=True) |
| 1398 |
_T1 = TypeVar("_T1") |
| 1399 |
_T2 = TypeVar("_T2") |
| 1400 |
S = TypeVar("S") |
| 1401 |
|
| 1402 |
class Z(Iterator[_T_co]): |
| 1403 |
def __new__(cls, |
| 1404 |
__iter1: List[_T1], |
| 1405 |
__iter2: List[_T2]) -> Z[Tuple[_T1, _T2]]: ... |
| 1406 |
def __iter__(self: S) -> S: ... |
| 1407 |
def __next__(self) -> _T_co: ... |
| 1408 |
|
| 1409 |
T = TypeVar('T') |
| 1410 |
|
| 1411 |
class C(Generic[T]): |
| 1412 |
a: List[T] |
| 1413 |
b: List[str] |
| 1414 |
|
| 1415 |
def f(self) -> None: |
| 1416 |
for x, y in Z(self.a, self.b): |
| 1417 |
reveal_type((x, y)) # N: Revealed type is "tuple[T`1, builtins.str]" |
| 1418 |
[builtins fixtures/tuple.pyi] |
| 1419 |
|
| 1420 |
[case testEnumerateReturningSelfFromIter] |
| 1421 |
from typing import Generic, Iterable, Iterator, TypeVar, Tuple |
| 1422 |
|
| 1423 |
T = TypeVar("T") |
| 1424 |
KT = TypeVar("KT") |
| 1425 |
VT = TypeVar("VT") |
| 1426 |
Self = TypeVar("Self") |
| 1427 |
|
| 1428 |
class enumerate(Iterator[Tuple[int, T]], Generic[T]): |
| 1429 |
def __init__(self, iterable: Iterable[T]) -> None: ... |
| 1430 |
def __iter__(self: Self) -> Self: ... |
| 1431 |
def __next__(self) -> Tuple[int, T]: ... |
| 1432 |
|
| 1433 |
class Dict(Generic[KT, VT]): |
| 1434 |
def update(self, __m: Iterable[Tuple[KT, VT]]) -> None: ... |
| 1435 |
|
| 1436 |
class ThingCollection(Generic[T]): |
| 1437 |
collection: Iterable[Tuple[float, T]] |
| 1438 |
index: Dict[int, T] |
| 1439 |
|
| 1440 |
def do_thing(self) -> None: |
| 1441 |
self.index.update((idx, c) for idx, (k, c) in enumerate(self.collection)) |
| 1442 |
[builtins fixtures/tuple.pyi] |
| 1443 |
|
| 1444 |
[case testDequeReturningSelfFromCopy] |
| 1445 |
# Tests a bug with generic self types identified in issue #12641 |
| 1446 |
from typing import Generic, Sequence, TypeVar |
| 1447 |
|
| 1448 |
T = TypeVar("T") |
| 1449 |
Self = TypeVar("Self") |
| 1450 |
|
| 1451 |
class deque(Sequence[T]): |
| 1452 |
def copy(self: Self) -> Self: ... |
| 1453 |
|
| 1454 |
class List(Sequence[T]): ... |
| 1455 |
|
| 1456 |
class Test(Generic[T]): |
| 1457 |
def test(self) -> None: |
| 1458 |
a: deque[List[T]] |
| 1459 |
# previously this failed with 'Incompatible types in assignment (expression has type "deque[List[List[T]]]", variable has type "deque[List[T]]")' |
| 1460 |
b: deque[List[T]] = a.copy() |
| 1461 |
|
| 1462 |
[case testTypingSelfBasic] |
| 1463 |
from typing import Self, List |
| 1464 |
|
| 1465 |
class C: |
| 1466 |
attr: List[Self] |
| 1467 |
def meth(self) -> List[Self]: ... |
| 1468 |
def test(self) -> Self: |
| 1469 |
if bool(): |
| 1470 |
return C() # E: Incompatible return value type (got "C", expected "Self") |
| 1471 |
else: |
| 1472 |
return self |
| 1473 |
class D(C): ... |
| 1474 |
|
| 1475 |
reveal_type(C.meth) # N: Revealed type is "def [Self <: __main__.C] (self: Self`1) -> builtins.list[Self`1]" |
| 1476 |
C.attr # E: Access to generic instance variables via class is ambiguous |
| 1477 |
reveal_type(D().meth()) # N: Revealed type is "builtins.list[__main__.D]" |
| 1478 |
reveal_type(D().attr) # N: Revealed type is "builtins.list[__main__.D]" |
| 1479 |
|
| 1480 |
[case testTypingSelfInvalidLocations] |
| 1481 |
from typing import Self, Callable |
| 1482 |
|
| 1483 |
var: Self # E: Self type is only allowed in annotations within class definition |
| 1484 |
reveal_type(var) # N: Revealed type is "Any" |
| 1485 |
|
| 1486 |
def foo() -> Self: ... # E: Self type is only allowed in annotations within class definition |
| 1487 |
reveal_type(foo) # N: Revealed type is "def () -> Any" |
| 1488 |
|
| 1489 |
bad: Callable[[Self], Self] # E: Self type is only allowed in annotations within class definition |
| 1490 |
reveal_type(bad) # N: Revealed type is "def (Any) -> Any" |
| 1491 |
|
| 1492 |
def func() -> None: |
| 1493 |
var: Self # E: Self type is only allowed in annotations within class definition |
| 1494 |
|
| 1495 |
class C(Self): ... # E: Self type is only allowed in annotations within class definition |
| 1496 |
|
| 1497 |
[case testTypingSelfInvalidArgs] |
| 1498 |
from typing import Self, List |
| 1499 |
|
| 1500 |
class C: |
| 1501 |
x: Self[int] # E: Self type cannot have type arguments |
| 1502 |
def meth(self) -> List[Self[int]]: # E: Self type cannot have type arguments |
| 1503 |
... |
| 1504 |
|
| 1505 |
[case testTypingSelfConflict] |
| 1506 |
from typing import Self, TypeVar, Tuple |
| 1507 |
|
| 1508 |
T = TypeVar("T") |
| 1509 |
class C: |
| 1510 |
def meth(self: T) -> Tuple[Self, T]: ... # E: Method cannot have explicit self annotation and Self type |
| 1511 |
reveal_type(C().meth()) # N: Revealed type is "tuple[Never, __main__.C]" |
| 1512 |
[builtins fixtures/property.pyi] |
| 1513 |
|
| 1514 |
[case testTypingSelfProperty] |
| 1515 |
from typing import Self, Tuple |
| 1516 |
class C: |
| 1517 |
@property |
| 1518 |
def attr(self) -> Tuple[Self, ...]: ... |
| 1519 |
class D(C): ... |
| 1520 |
|
| 1521 |
reveal_type(D().attr) # N: Revealed type is "builtins.tuple[__main__.D, ...]" |
| 1522 |
[builtins fixtures/property.pyi] |
| 1523 |
|
| 1524 |
[case testTypingSelfCallableVar] |
| 1525 |
from typing import Self, Callable |
| 1526 |
|
| 1527 |
class C: |
| 1528 |
x: Callable[[Self], Self] |
| 1529 |
def meth(self) -> Callable[[Self], Self]: ... |
| 1530 |
class D(C): ... |
| 1531 |
|
| 1532 |
reveal_type(C().x) # N: Revealed type is "def (__main__.C) -> __main__.C" |
| 1533 |
reveal_type(D().x) # N: Revealed type is "def (__main__.D) -> __main__.D" |
| 1534 |
reveal_type(D().meth()) # N: Revealed type is "def (__main__.D) -> __main__.D" |
| 1535 |
|
| 1536 |
[case testTypingSelfClassMethod] |
| 1537 |
from typing import Self |
| 1538 |
|
| 1539 |
class C: |
| 1540 |
@classmethod |
| 1541 |
def meth(cls) -> Self: ... |
| 1542 |
@staticmethod |
| 1543 |
def bad() -> Self: ... # E: Static methods cannot use Self type \ |
| 1544 |
# E: A function returning TypeVar should receive at least one argument containing the same TypeVar \ |
| 1545 |
# N: Consider using the upper bound "C" instead |
| 1546 |
|
| 1547 |
class D(C): ... |
| 1548 |
reveal_type(D.meth()) # N: Revealed type is "__main__.D" |
| 1549 |
reveal_type(D.bad()) # N: Revealed type is "Never" |
| 1550 |
[builtins fixtures/classmethod.pyi] |
| 1551 |
|
| 1552 |
[case testTypingSelfOverload] |
| 1553 |
from typing import Self, overload, Union |
| 1554 |
|
| 1555 |
class C: |
| 1556 |
@overload |
| 1557 |
def foo(self, other: Self) -> Self: ... |
| 1558 |
@overload |
| 1559 |
def foo(self, other: int) -> int: ... |
| 1560 |
def foo(self, other: Union[Self, int]) -> Union[Self, int]: |
| 1561 |
return other |
| 1562 |
class D(C): ... |
| 1563 |
reveal_type(D().foo) # N: Revealed type is "Overload(def (other: __main__.D) -> __main__.D, def (other: builtins.int) -> builtins.int)" |
| 1564 |
|
| 1565 |
[case testTypingSelfNestedInAlias] |
| 1566 |
from typing import Generic, Self, TypeVar, List, Tuple |
| 1567 |
|
| 1568 |
T = TypeVar("T") |
| 1569 |
Pairs = List[Tuple[T, T]] |
| 1570 |
|
| 1571 |
class C(Generic[T]): |
| 1572 |
def pairs(self) -> Pairs[Self]: ... |
| 1573 |
class D(C[T]): ... |
| 1574 |
reveal_type(D[int]().pairs()) # N: Revealed type is "builtins.list[tuple[__main__.D[builtins.int], __main__.D[builtins.int]]]" |
| 1575 |
[builtins fixtures/tuple.pyi] |
| 1576 |
|
| 1577 |
[case testTypingSelfOverrideVar] |
| 1578 |
from typing import Self, TypeVar, Generic |
| 1579 |
|
| 1580 |
T = TypeVar("T") |
| 1581 |
class C(Generic[T]): |
| 1582 |
x: Self |
| 1583 |
|
| 1584 |
class D(C[int]): |
| 1585 |
x: D |
| 1586 |
class Bad(C[int]): |
| 1587 |
x: C[int] # E: Incompatible types in assignment (expression has type "C[int]", base class "C" defined the type as "Bad") |
| 1588 |
|
| 1589 |
[case testTypingSelfOverrideVarMulti] |
| 1590 |
from typing import Self |
| 1591 |
|
| 1592 |
class C: |
| 1593 |
x: Self |
| 1594 |
class D: |
| 1595 |
x: C |
| 1596 |
class E: |
| 1597 |
x: Good |
| 1598 |
|
| 1599 |
class Bad(D, C): # E: Definition of "x" in base class "D" is incompatible with definition in base class "C" |
| 1600 |
... |
| 1601 |
class Good(E, C): |
| 1602 |
... |
| 1603 |
|
| 1604 |
[case testTypingSelfAlternativeGenericConstructor] |
| 1605 |
from typing import Self, Generic, TypeVar, Tuple |
| 1606 |
|
| 1607 |
T = TypeVar("T") |
| 1608 |
class C(Generic[T]): |
| 1609 |
def __init__(self, val: T) -> None: ... |
| 1610 |
@classmethod |
| 1611 |
def pair(cls, val: T) -> Tuple[Self, Self]: |
| 1612 |
return (cls(val), C(val)) # E: Incompatible return value type (got "tuple[Self, C[T]]", expected "tuple[Self, Self]") |
| 1613 |
|
| 1614 |
class D(C[int]): pass |
| 1615 |
reveal_type(C.pair(42)) # N: Revealed type is "tuple[__main__.C[builtins.int], __main__.C[builtins.int]]" |
| 1616 |
reveal_type(D.pair("no")) # N: Revealed type is "tuple[__main__.D, __main__.D]" \ |
| 1617 |
# E: Argument 1 to "pair" of "C" has incompatible type "str"; expected "int" |
| 1618 |
[builtins fixtures/classmethod.pyi] |
| 1619 |
|
| 1620 |
[case testTypingSelfMixedTypeVars] |
| 1621 |
from typing import Self, TypeVar, Generic, Tuple |
| 1622 |
|
| 1623 |
T = TypeVar("T") |
| 1624 |
S = TypeVar("S") |
| 1625 |
|
| 1626 |
class C(Generic[T]): |
| 1627 |
def meth(self, arg: S) -> Tuple[Self, S, T]: ... |
| 1628 |
|
| 1629 |
class D(C[int]): ... |
| 1630 |
|
| 1631 |
c: C[int] |
| 1632 |
d: D |
| 1633 |
reveal_type(c.meth("test")) # N: Revealed type is "tuple[__main__.C[builtins.int], builtins.str, builtins.int]" |
| 1634 |
reveal_type(d.meth("test")) # N: Revealed type is "tuple[__main__.D, builtins.str, builtins.int]" |
| 1635 |
[builtins fixtures/tuple.pyi] |
| 1636 |
|
| 1637 |
[case testTypingSelfRecursiveInit] |
| 1638 |
from typing import Self |
| 1639 |
|
| 1640 |
class C: |
| 1641 |
def __init__(self, other: Self) -> None: ... |
| 1642 |
class D(C): ... |
| 1643 |
|
| 1644 |
reveal_type(C) # N: Revealed type is "def (other: __main__.C) -> __main__.C" |
| 1645 |
reveal_type(D) # N: Revealed type is "def (other: __main__.D) -> __main__.D" |
| 1646 |
|
| 1647 |
[case testTypingSelfCorrectName] |
| 1648 |
from typing import Self, List |
| 1649 |
|
| 1650 |
class C: |
| 1651 |
Self = List[C] |
| 1652 |
def meth(self) -> Self: ... |
| 1653 |
reveal_type(C.meth) # N: Revealed type is "def (self: __main__.C) -> builtins.list[__main__.C]" |
| 1654 |
|
| 1655 |
[case testTypingSelfClassVar] |
| 1656 |
from typing import Self, ClassVar, Generic, TypeVar |
| 1657 |
|
| 1658 |
class C: |
| 1659 |
DEFAULT: ClassVar[Self] |
| 1660 |
reveal_type(C.DEFAULT) # N: Revealed type is "__main__.C" |
| 1661 |
|
| 1662 |
T = TypeVar("T") |
| 1663 |
class G(Generic[T]): |
| 1664 |
BAD: ClassVar[Self] # E: ClassVar cannot contain Self type in generic classes |
| 1665 |
reveal_type(G.BAD) # N: Revealed type is "__main__.G[Any]" |
| 1666 |
|
| 1667 |
[case testTypingSelfMetaClassDisabled] |
| 1668 |
from typing import Self |
| 1669 |
|
| 1670 |
class Meta(type): |
| 1671 |
def meth(cls) -> Self: ... # E: Self type cannot be used in a metaclass |
| 1672 |
|
| 1673 |
[case testTypingSelfNonAnnotationUses] |
| 1674 |
from typing import Self, List, cast |
| 1675 |
|
| 1676 |
class C: |
| 1677 |
A = List[Self] # E: Self type cannot be used in type alias target |
| 1678 |
B = cast(Self, ...) |
| 1679 |
def meth(self) -> A: ... |
| 1680 |
|
| 1681 |
class D(C): ... |
| 1682 |
reveal_type(D().meth()) # N: Revealed type is "builtins.list[Any]" |
| 1683 |
reveal_type(D().B) # N: Revealed type is "__main__.D" |
| 1684 |
|
| 1685 |
[case testTypingSelfInternalSafe] |
| 1686 |
from typing import Self |
| 1687 |
|
| 1688 |
class C: |
| 1689 |
x: Self |
| 1690 |
def __init__(self, x: C) -> None: |
| 1691 |
self.x = x # E: Incompatible types in assignment (expression has type "C", variable has type "Self") |
| 1692 |
|
| 1693 |
[case testTypingSelfRedundantAllowed] |
| 1694 |
from typing import Self, Type |
| 1695 |
|
| 1696 |
class C: |
| 1697 |
def f(self: Self) -> Self: |
| 1698 |
d: Defer |
| 1699 |
class Defer: ... |
| 1700 |
return self |
| 1701 |
|
| 1702 |
@classmethod |
| 1703 |
def g(cls: Type[Self]) -> Self: |
| 1704 |
d: DeferAgain |
| 1705 |
class DeferAgain: ... |
| 1706 |
return cls() |
| 1707 |
[builtins fixtures/classmethod.pyi] |
| 1708 |
|
| 1709 |
[case testTypingSelfRedundantAllowed_pep585] |
| 1710 |
from typing import Self |
| 1711 |
|
| 1712 |
class C: |
| 1713 |
def f(self: Self) -> Self: |
| 1714 |
d: Defer |
| 1715 |
class Defer: ... |
| 1716 |
return self |
| 1717 |
|
| 1718 |
@classmethod |
| 1719 |
def g(cls: type[Self]) -> Self: |
| 1720 |
d: DeferAgain |
| 1721 |
class DeferAgain: ... |
| 1722 |
return cls() |
| 1723 |
[builtins fixtures/classmethod.pyi] |
| 1724 |
|
| 1725 |
[case testTypingSelfRedundantWarning] |
| 1726 |
# mypy: enable-error-code="redundant-self" |
| 1727 |
|
| 1728 |
from typing import Self, Type |
| 1729 |
|
| 1730 |
class C: |
| 1731 |
def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method argument |
| 1732 |
d: Defer |
| 1733 |
class Defer: ... |
| 1734 |
return self |
| 1735 |
|
| 1736 |
@classmethod |
| 1737 |
def g(cls: Type[Self]) -> Self: # E: Redundant "Self" annotation for the first method argument |
| 1738 |
d: DeferAgain |
| 1739 |
class DeferAgain: ... |
| 1740 |
return cls() |
| 1741 |
[builtins fixtures/classmethod.pyi] |
| 1742 |
|
| 1743 |
[case testTypingSelfRedundantWarning_pep585] |
| 1744 |
# mypy: enable-error-code="redundant-self" |
| 1745 |
|
| 1746 |
from typing import Self |
| 1747 |
|
| 1748 |
class C: |
| 1749 |
def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method argument |
| 1750 |
d: Defer |
| 1751 |
class Defer: ... |
| 1752 |
return self |
| 1753 |
|
| 1754 |
@classmethod |
| 1755 |
def g(cls: type[Self]) -> Self: # E: Redundant "Self" annotation for the first method argument |
| 1756 |
d: DeferAgain |
| 1757 |
class DeferAgain: ... |
| 1758 |
return cls() |
| 1759 |
[builtins fixtures/classmethod.pyi] |
| 1760 |
|
| 1761 |
[case testTypingSelfAssertType] |
| 1762 |
from typing import Self, assert_type |
| 1763 |
|
| 1764 |
class C: |
| 1765 |
def foo(self) -> None: |
| 1766 |
assert_type(self, Self) # E: Expression is of type "C", not "Self" |
| 1767 |
assert_type(C(), Self) # E: Expression is of type "C", not "Self" |
| 1768 |
|
| 1769 |
def bar(self) -> Self: |
| 1770 |
assert_type(self, Self) # OK |
| 1771 |
assert_type(C(), Self) # E: Expression is of type "C", not "Self" |
| 1772 |
return self |
| 1773 |
|
| 1774 |
[case testTypingSelfTypeVarClash] |
| 1775 |
from typing import Self, TypeVar, Tuple |
| 1776 |
|
| 1777 |
S = TypeVar("S") |
| 1778 |
class C: |
| 1779 |
def bar(self) -> Self: ... |
| 1780 |
def foo(self, x: S) -> Tuple[Self, S]: ... |
| 1781 |
|
| 1782 |
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`1, x: S`2) -> tuple[Self`1, S`2]" |
| 1783 |
reveal_type(C().foo(42)) # N: Revealed type is "tuple[__main__.C, builtins.int]" |
| 1784 |
[builtins fixtures/tuple.pyi] |
| 1785 |
|
| 1786 |
[case testTypingSelfTypeVarClashAttr] |
| 1787 |
from typing import Self, TypeVar, Tuple, Callable |
| 1788 |
|
| 1789 |
class Defer(This): ... |
| 1790 |
|
| 1791 |
S = TypeVar("S") |
| 1792 |
class C: |
| 1793 |
def bar(self) -> Self: ... |
| 1794 |
foo: Callable[[S, Self], Tuple[Self, S]] |
| 1795 |
|
| 1796 |
reveal_type(C().foo) # N: Revealed type is "def [S] (S`2, __main__.C) -> tuple[__main__.C, S`2]" |
| 1797 |
reveal_type(C().foo(42, C())) # N: Revealed type is "tuple[__main__.C, builtins.int]" |
| 1798 |
class This: ... |
| 1799 |
[builtins fixtures/tuple.pyi] |
| 1800 |
|
| 1801 |
[case testTypingSelfAttrOldVsNewStyle] |
| 1802 |
from typing import Self, TypeVar |
| 1803 |
|
| 1804 |
T = TypeVar("T", bound='C') |
| 1805 |
class C: |
| 1806 |
x: Self |
| 1807 |
def foo(self: T) -> T: |
| 1808 |
return self.x |
| 1809 |
def bar(self: T) -> T: |
| 1810 |
self.x = self |
| 1811 |
return self |
| 1812 |
def baz(self: Self) -> None: |
| 1813 |
self.x = self |
| 1814 |
def bad(self) -> None: |
| 1815 |
# This is unfortunate, but required by PEP 484 |
| 1816 |
self.x = self # E: Incompatible types in assignment (expression has type "C", variable has type "Self") |
| 1817 |
|
| 1818 |
[case testTypingSelfClashInBodies] |
| 1819 |
from typing import Self, TypeVar |
| 1820 |
|
| 1821 |
T = TypeVar("T") |
| 1822 |
class C: |
| 1823 |
def very_bad(self, x: T) -> None: |
| 1824 |
self.x = x # E: Incompatible types in assignment (expression has type "T", variable has type "Self") |
| 1825 |
x: Self |
| 1826 |
def baz(self: Self, x: T) -> None: |
| 1827 |
y: T = x |
| 1828 |
|
| 1829 |
[case testTypingSelfClashUnrelated] |
| 1830 |
from typing import Self, Generic, TypeVar |
| 1831 |
|
| 1832 |
class B: ... |
| 1833 |
|
| 1834 |
T = TypeVar("T", bound=B) |
| 1835 |
class C(Generic[T]): |
| 1836 |
def __init__(self, val: T) -> None: |
| 1837 |
self.val = val |
| 1838 |
def foo(self) -> Self: ... |
| 1839 |
|
| 1840 |
def test(x: C[T]) -> T: |
| 1841 |
reveal_type(x.val) # N: Revealed type is "T`-1" |
| 1842 |
return x.val |
| 1843 |
|
| 1844 |
[case testTypingSelfGenericBound] |
| 1845 |
from typing import Self, Generic, TypeVar |
| 1846 |
|
| 1847 |
T = TypeVar("T") |
| 1848 |
class C(Generic[T]): |
| 1849 |
val: T |
| 1850 |
def foo(self) -> Self: |
| 1851 |
reveal_type(self.val) # N: Revealed type is "T`1" |
| 1852 |
return self |
| 1853 |
|
| 1854 |
[case testTypingSelfDifferentImport] |
| 1855 |
import typing as t |
| 1856 |
|
| 1857 |
class Foo: |
| 1858 |
def foo(self) -> t.Self: |
| 1859 |
return self |
| 1860 |
@classmethod |
| 1861 |
def bar(cls) -> t.Self: |
| 1862 |
return cls() |
| 1863 |
[builtins fixtures/classmethod.pyi] |
| 1864 |
|
| 1865 |
[case testTypingSelfAllowAliasUseInFinalClasses] |
| 1866 |
from typing import Self, final |
| 1867 |
|
| 1868 |
@final |
| 1869 |
class C: |
| 1870 |
def meth(self) -> Self: |
| 1871 |
return C() # OK for final classes |
| 1872 |
|
| 1873 |
[case testTypingSelfCallableClassVar] |
| 1874 |
from typing import Self, ClassVar, Callable, TypeVar |
| 1875 |
|
| 1876 |
class C: |
| 1877 |
f: ClassVar[Callable[[Self], Self]] |
| 1878 |
class D(C): ... |
| 1879 |
|
| 1880 |
reveal_type(D.f) # N: Revealed type is "def (__main__.D) -> __main__.D" |
| 1881 |
reveal_type(D().f) # N: Revealed type is "def () -> __main__.D" |
| 1882 |
|
| 1883 |
[case testSelfTypeCallableClassVarOldStyle] |
| 1884 |
from typing import ClassVar, Callable, TypeVar |
| 1885 |
|
| 1886 |
T = TypeVar("T") |
| 1887 |
class C: |
| 1888 |
f: ClassVar[Callable[[T], T]] |
| 1889 |
|
| 1890 |
class D(C): ... |
| 1891 |
|
| 1892 |
reveal_type(D.f) # N: Revealed type is "def [T] (T`3) -> T`3" |
| 1893 |
reveal_type(D().f) # N: Revealed type is "def () -> __main__.D" |
| 1894 |
|
| 1895 |
[case testTypingSelfOnSuperTypeVarValues] |
| 1896 |
from typing import Self, Generic, TypeVar |
| 1897 |
|
| 1898 |
T = TypeVar("T", int, str) |
| 1899 |
|
| 1900 |
class B: |
| 1901 |
def copy(self) -> Self: ... |
| 1902 |
class C(B, Generic[T]): |
| 1903 |
def copy(self) -> Self: |
| 1904 |
inst = super().copy() |
| 1905 |
reveal_type(inst) # N: Revealed type is "Self`0" |
| 1906 |
return inst |
| 1907 |
|
| 1908 |
[case testTypingSelfWithValuesExpansion] |
| 1909 |
from typing import Self, Generic, TypeVar |
| 1910 |
|
| 1911 |
class A: pass |
| 1912 |
class B: pass |
| 1913 |
T = TypeVar("T", A, B) |
| 1914 |
|
| 1915 |
class C(Generic[T]): |
| 1916 |
val: T |
| 1917 |
def foo(self, x: T) -> None: ... |
| 1918 |
def bar(self, x: T) -> Self: |
| 1919 |
reveal_type(self.foo) # N: Revealed type is "def (x: __main__.A)" \ |
| 1920 |
# N: Revealed type is "def (x: __main__.B)" |
| 1921 |
self.foo(x) |
| 1922 |
return self |
| 1923 |
def baz(self: Self, x: T) -> None: |
| 1924 |
reveal_type(self.val) # N: Revealed type is "__main__.A" \ |
| 1925 |
# N: Revealed type is "__main__.B" |
| 1926 |
self.val = x |
| 1927 |
|
| 1928 |
[case testNarrowSelfType] |
| 1929 |
from typing import Self, Union |
| 1930 |
|
| 1931 |
class A: ... |
| 1932 |
class B: |
| 1933 |
def f1(self, v: Union[Self, A]) -> A: |
| 1934 |
if isinstance(v, B): |
| 1935 |
return A() |
| 1936 |
else: |
| 1937 |
return v |
| 1938 |
def f2(self, v: Union[Self, A]) -> A: |
| 1939 |
if isinstance(v, B): |
| 1940 |
return A() |
| 1941 |
else: |
| 1942 |
return B() # E: Incompatible return value type (got "B", expected "A") |
| 1943 |
|
| 1944 |
[builtins fixtures/isinstancelist.pyi] |
| 1945 |
|
| 1946 |
[case testAttributeOnSelfAttributeInSubclass] |
| 1947 |
from typing import List, Self |
| 1948 |
|
| 1949 |
class A: |
| 1950 |
x: Self |
| 1951 |
xs: List[Self] |
| 1952 |
|
| 1953 |
class B(A): |
| 1954 |
extra: int |
| 1955 |
|
| 1956 |
def meth(self) -> None: |
| 1957 |
reveal_type(self.x) # N: Revealed type is "Self`0" |
| 1958 |
reveal_type(self.xs[0]) # N: Revealed type is "Self`0" |
| 1959 |
reveal_type(self.x.extra) # N: Revealed type is "builtins.int" |
| 1960 |
reveal_type(self.xs[0].extra) # N: Revealed type is "builtins.int" |
| 1961 |
[builtins fixtures/list.pyi] |
| 1962 |
|
| 1963 |
[case testSelfTypesWithParamSpecExtract] |
| 1964 |
from typing import Any, Callable, Generic, TypeVar |
| 1965 |
from typing_extensions import ParamSpec |
| 1966 |
|
| 1967 |
P = ParamSpec("P") |
| 1968 |
F = TypeVar("F", bound=Callable[..., Any]) |
| 1969 |
class Example(Generic[F]): |
| 1970 |
def __init__(self, fn: F) -> None: |
| 1971 |
... |
| 1972 |
def __call__(self: Example[Callable[P, Any]], *args: P.args, **kwargs: P.kwargs) -> None: |
| 1973 |
... |
| 1974 |
|
| 1975 |
def test_fn(a: int, b: str) -> None: |
| 1976 |
... |
| 1977 |
|
| 1978 |
example = Example(test_fn) |
| 1979 |
example() # E: Missing positional arguments "a", "b" in call to "__call__" of "Example" |
| 1980 |
example(1, "b") # OK |
| 1981 |
[builtins fixtures/list.pyi] |
| 1982 |
|
| 1983 |
[case testSelfTypesWithParamSpecInfer] |
| 1984 |
from typing import TypeVar, Protocol, Type, Callable |
| 1985 |
from typing_extensions import ParamSpec |
| 1986 |
|
| 1987 |
R = TypeVar("R", covariant=True) |
| 1988 |
P = ParamSpec("P") |
| 1989 |
class AsyncP(Protocol[P]): |
| 1990 |
def meth(self, *args: P.args, **kwargs: P.kwargs) -> None: |
| 1991 |
... |
| 1992 |
|
| 1993 |
class Async: |
| 1994 |
@classmethod |
| 1995 |
def async_func(cls: Type[AsyncP[P]]) -> Callable[P, int]: |
| 1996 |
... |
| 1997 |
|
| 1998 |
class Add(Async): |
| 1999 |
def meth(self, x: int, y: int) -> None: ... |
| 2000 |
|
| 2001 |
reveal_type(Add.async_func()) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int" |
| 2002 |
reveal_type(Add().async_func()) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int" |
| 2003 |
[builtins fixtures/classmethod.pyi] |
| 2004 |
|
| 2005 |
[case testSelfTypeMethodOnClassObject] |
| 2006 |
from typing import Self |
| 2007 |
|
| 2008 |
class Object: # Needed to mimic object in typeshed |
| 2009 |
ref: Self |
| 2010 |
|
| 2011 |
class Foo: |
| 2012 |
def foo(self) -> Self: |
| 2013 |
return self |
| 2014 |
|
| 2015 |
class Ben(Object): |
| 2016 |
MY_MAP = { |
| 2017 |
"foo": Foo.foo, |
| 2018 |
} |
| 2019 |
@classmethod |
| 2020 |
def doit(cls) -> Foo: |
| 2021 |
reveal_type(cls.MY_MAP) # N: Revealed type is "builtins.dict[builtins.str, def [Self <: __main__.Foo] (self: Self`1) -> Self`1]" |
| 2022 |
foo_method = cls.MY_MAP["foo"] |
| 2023 |
return foo_method(Foo()) |
| 2024 |
[builtins fixtures/isinstancelist.pyi] |
| 2025 |
|
| 2026 |
[case testSelfTypeOnGenericClassObjectNewStyleBound] |
| 2027 |
from typing import Generic, TypeVar, Self |
| 2028 |
|
| 2029 |
T = TypeVar("T") |
| 2030 |
S = TypeVar("S") |
| 2031 |
class B(Generic[T, S]): |
| 2032 |
def copy(self) -> Self: ... |
| 2033 |
|
| 2034 |
b: B[int, str] |
| 2035 |
reveal_type(B.copy(b)) # N: Revealed type is "__main__.B[builtins.int, builtins.str]" |
| 2036 |
|
| 2037 |
class C(B[T, S]): ... |
| 2038 |
|
| 2039 |
c: C[int, str] |
| 2040 |
reveal_type(C.copy(c)) # N: Revealed type is "__main__.C[builtins.int, builtins.str]" |
| 2041 |
|
| 2042 |
B.copy(42) # E: Value of type variable "Self" of "copy" of "B" cannot be "int" |
| 2043 |
C.copy(42) # E: Value of type variable "Self" of "copy" of "B" cannot be "int" |
| 2044 |
[builtins fixtures/tuple.pyi] |
| 2045 |
|
| 2046 |
[case testRecursiveSelfTypeCallMethodNoCrash] |
| 2047 |
from typing import Callable, TypeVar |
| 2048 |
|
| 2049 |
T = TypeVar("T") |
| 2050 |
class Partial: |
| 2051 |
def __call__(self: Callable[..., T]) -> T: ... |
| 2052 |
|
| 2053 |
class Partial2: |
| 2054 |
def __call__(self: Callable[..., T], x: T) -> T: ... |
| 2055 |
|
| 2056 |
p: Partial |
| 2057 |
reveal_type(p()) # N: Revealed type is "Never" |
| 2058 |
p2: Partial2 |
| 2059 |
reveal_type(p2(42)) # N: Revealed type is "builtins.int" |
| 2060 |
|
| 2061 |
[case testAccessingSelfClassVarInClassMethod] |
| 2062 |
from typing import Self, ClassVar, Type, TypeVar |
| 2063 |
|
| 2064 |
T = TypeVar("T", bound="Foo") |
| 2065 |
|
| 2066 |
class Foo: |
| 2067 |
instance: ClassVar[Self] |
| 2068 |
@classmethod |
| 2069 |
def get_instance(cls) -> Self: |
| 2070 |
return reveal_type(cls.instance) # N: Revealed type is "Self`0" |
| 2071 |
@classmethod |
| 2072 |
def get_instance_old(cls: Type[T]) -> T: |
| 2073 |
return reveal_type(cls.instance) # N: Revealed type is "T`-1" |
| 2074 |
|
| 2075 |
class Bar(Foo): |
| 2076 |
extra: int |
| 2077 |
|
| 2078 |
@classmethod |
| 2079 |
def get_instance(cls) -> Self: |
| 2080 |
reveal_type(cls.instance.extra) # N: Revealed type is "builtins.int" |
| 2081 |
return cls.instance |
| 2082 |
|
| 2083 |
@classmethod |
| 2084 |
def other(cls) -> None: |
| 2085 |
reveal_type(cls.instance) # N: Revealed type is "Self`0" |
| 2086 |
reveal_type(cls.instance.extra) # N: Revealed type is "builtins.int" |
| 2087 |
|
| 2088 |
reveal_type(Bar.instance) # N: Revealed type is "__main__.Bar" |
| 2089 |
[builtins fixtures/classmethod.pyi] |
| 2090 |
|
| 2091 |
[case testAccessingSelfClassVarInClassMethodTuple] |
| 2092 |
from typing import Self, ClassVar, Tuple |
| 2093 |
|
| 2094 |
class C(Tuple[int, str]): |
| 2095 |
x: Self |
| 2096 |
y: ClassVar[Self] |
| 2097 |
|
| 2098 |
@classmethod |
| 2099 |
def bar(cls) -> None: |
| 2100 |
reveal_type(cls.y) # N: Revealed type is "Self`0" |
| 2101 |
@classmethod |
| 2102 |
def bar_self(self) -> Self: |
| 2103 |
return reveal_type(self.y) # N: Revealed type is "Self`0" |
| 2104 |
|
| 2105 |
c: C |
| 2106 |
reveal_type(c.x) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.C]" |
| 2107 |
reveal_type(c.y) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.C]" |
| 2108 |
reveal_type(C.y) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.C]" |
| 2109 |
C.x # E: Access to generic instance variables via class is ambiguous |
| 2110 |
[builtins fixtures/classmethod.pyi] |
| 2111 |
|
| 2112 |
[case testAccessingTypingSelfUnion] |
| 2113 |
from typing import Self, Union |
| 2114 |
|
| 2115 |
class C: |
| 2116 |
x: Self |
| 2117 |
class D: |
| 2118 |
x: int |
| 2119 |
x: Union[C, D] |
| 2120 |
reveal_type(x.x) # N: Revealed type is "__main__.C | builtins.int" |
| 2121 |
|
| 2122 |
[case testCallableProtocolTypingSelf] |
| 2123 |
from typing import Protocol, Self |
| 2124 |
|
| 2125 |
class MyProtocol(Protocol): |
| 2126 |
__name__: str |
| 2127 |
|
| 2128 |
def __call__( |
| 2129 |
self: Self, |
| 2130 |
) -> None: ... |
| 2131 |
|
| 2132 |
def test() -> None: ... |
| 2133 |
value: MyProtocol = test |
| 2134 |
|
| 2135 |
[case testCallableProtocolOldSelf] |
| 2136 |
from typing import Protocol, TypeVar |
| 2137 |
|
| 2138 |
Self = TypeVar("Self", bound="MyProtocol") |
| 2139 |
|
| 2140 |
class MyProtocol(Protocol): |
| 2141 |
__name__: str |
| 2142 |
|
| 2143 |
def __call__( |
| 2144 |
self: Self, |
| 2145 |
) -> None: ... |
| 2146 |
|
| 2147 |
def test() -> None: ... |
| 2148 |
value: MyProtocol = test |
| 2149 |
|
| 2150 |
[case testSelfTypeUnionIter] |
| 2151 |
from typing import Self, Iterator, Generic, TypeVar, Union |
| 2152 |
|
| 2153 |
T = TypeVar("T") |
| 2154 |
|
| 2155 |
class range(Generic[T]): |
| 2156 |
def __iter__(self) -> Self: ... |
| 2157 |
def __next__(self) -> T: ... |
| 2158 |
|
| 2159 |
class count: |
| 2160 |
def __iter__(self) -> Iterator[int]: ... |
| 2161 |
|
| 2162 |
def foo(x: Union[range[int], count]) -> None: |
| 2163 |
for item in x: |
| 2164 |
reveal_type(item) # N: Revealed type is "builtins.int" |
| 2165 |
|
| 2166 |
[case testGenericDescriptorWithSelfTypeAnnotationsAndOverloads] |
| 2167 |
from __future__ import annotations |
| 2168 |
from typing import Any, overload, Callable, TypeVar, Generic, ParamSpec |
| 2169 |
from typing_extensions import Concatenate |
| 2170 |
|
| 2171 |
C = TypeVar("C", bound=Callable[..., Any]) |
| 2172 |
S = TypeVar("S") |
| 2173 |
P = ParamSpec("P") |
| 2174 |
R = TypeVar("R") |
| 2175 |
|
| 2176 |
class Descriptor(Generic[C]): |
| 2177 |
def __init__(self, impl: C) -> None: ... |
| 2178 |
|
| 2179 |
@overload |
| 2180 |
def __get__( |
| 2181 |
self: Descriptor[C], instance: None, owner: type | None |
| 2182 |
) -> Descriptor[C]: ... |
| 2183 |
|
| 2184 |
@overload |
| 2185 |
def __get__( |
| 2186 |
self: Descriptor[Callable[Concatenate[S, P], R]], instance: S, owner: type | None, |
| 2187 |
) -> Callable[P, R]: ... |
| 2188 |
|
| 2189 |
def __get__(self, *args, **kwargs): ... |
| 2190 |
|
| 2191 |
class Test: |
| 2192 |
@Descriptor |
| 2193 |
def method(self, foo: int, bar: str) -> bytes: ... |
| 2194 |
|
| 2195 |
reveal_type(Test().method) # N: Revealed type is "def (foo: builtins.int, bar: builtins.str) -> builtins.bytes" |
| 2196 |
|
| 2197 |
class Test2: |
| 2198 |
@Descriptor |
| 2199 |
def method(self, foo: int, *, bar: str) -> bytes: ... |
| 2200 |
|
| 2201 |
reveal_type(Test2().method) # N: Revealed type is "def (foo: builtins.int, *, bar: builtins.str) -> builtins.bytes" |
| 2202 |
[builtins fixtures/tuple.pyi] |
| 2203 |
|
| 2204 |
[case testSelfInMultipleInheritance] |
| 2205 |
from typing_extensions import Self |
| 2206 |
|
| 2207 |
class A: |
| 2208 |
foo: int |
| 2209 |
def method(self: Self, other: Self) -> None: |
| 2210 |
self.foo |
| 2211 |
other.foo |
| 2212 |
|
| 2213 |
class B: |
| 2214 |
bar: str |
| 2215 |
def method(self: Self, other: Self) -> None: |
| 2216 |
self.bar |
| 2217 |
other.bar |
| 2218 |
|
| 2219 |
class C(A, B): # OK: both methods take Self |
| 2220 |
pass |
| 2221 |
[builtins fixtures/tuple.pyi] |
| 2222 |
|
| 2223 |
[case testSelfTypeClassMethodNotSilentlyErased] |
| 2224 |
from typing import Self, Optional |
| 2225 |
|
| 2226 |
class X: |
| 2227 |
_inst: Optional[Self] = None |
| 2228 |
@classmethod |
| 2229 |
def default(cls) -> Self: |
| 2230 |
reveal_type(cls._inst) # N: Revealed type is "Self`0 | None" |
| 2231 |
if cls._inst is None: |
| 2232 |
cls._inst = cls() |
| 2233 |
return cls._inst |
| 2234 |
|
| 2235 |
reveal_type(X._inst) # E: Access to generic instance variables via class is ambiguous \ |
| 2236 |
# N: Revealed type is "__main__.X | None" |
| 2237 |
reveal_type(X()._inst) # N: Revealed type is "__main__.X | None" |
| 2238 |
|
| 2239 |
class Y(X): ... |
| 2240 |
reveal_type(Y._inst) # E: Access to generic instance variables via class is ambiguous \ |
| 2241 |
# N: Revealed type is "__main__.Y | None" |
| 2242 |
reveal_type(Y()._inst) # N: Revealed type is "__main__.Y | None" |
| 2243 |
[builtins fixtures/tuple.pyi] |
| 2244 |
|
| 2245 |
[case testSelfInFuncDecoratedClassmethod] |
| 2246 |
from collections.abc import Callable |
| 2247 |
from typing import Self, TypeVar |
| 2248 |
|
| 2249 |
T = TypeVar("T") |
| 2250 |
|
| 2251 |
def debug(make: Callable[[type[T]], T]) -> Callable[[type[T]], T]: |
| 2252 |
return make |
| 2253 |
|
| 2254 |
class Foo: |
| 2255 |
@classmethod |
| 2256 |
@debug |
| 2257 |
def make(cls) -> Self: |
| 2258 |
return cls() |
| 2259 |
|
| 2260 |
class Bar(Foo): ... |
| 2261 |
|
| 2262 |
reveal_type(Foo.make()) # N: Revealed type is "__main__.Foo" |
| 2263 |
reveal_type(Foo().make()) # N: Revealed type is "__main__.Foo" |
| 2264 |
reveal_type(Bar.make()) # N: Revealed type is "__main__.Bar" |
| 2265 |
reveal_type(Bar().make()) # N: Revealed type is "__main__.Bar" |
| 2266 |
[builtins fixtures/tuple.pyi] |
| 2267 |
|
| 2268 |
[case testSelfInClassDecoratedClassmethod] |
| 2269 |
from typing import Callable, Generic, TypeVar, Self |
| 2270 |
|
| 2271 |
T = TypeVar("T") |
| 2272 |
|
| 2273 |
class W(Generic[T]): |
| 2274 |
def __init__(self, fn: Callable[..., T]) -> None: ... |
| 2275 |
def __call__(self) -> T: ... |
| 2276 |
|
| 2277 |
class Check: |
| 2278 |
@W |
| 2279 |
def foo(self) -> Self: |
| 2280 |
... |
| 2281 |
|
| 2282 |
reveal_type(Check.foo()) # N: Revealed type is "def () -> __main__.Check" |
| 2283 |
reveal_type(Check().foo()) # N: Revealed type is "__main__.Check" |
| 2284 |
[builtins fixtures/tuple.pyi] |
| 2285 |
|
| 2286 |
[case testSelfInClassmethodWithOtherSelfMethod] |
| 2287 |
from typing import Any, Callable, Self, TypeVar |
| 2288 |
|
| 2289 |
_C = TypeVar("_C", bound=Callable[..., Any]) |
| 2290 |
|
| 2291 |
def identity(func: _C, /) -> _C: |
| 2292 |
return func |
| 2293 |
|
| 2294 |
class A: |
| 2295 |
def meth(self) -> Self: ... |
| 2296 |
|
| 2297 |
@classmethod |
| 2298 |
def other_meth(cls) -> Self: |
| 2299 |
reveal_type(cls.meth) # N: Revealed type is "def [Self <: __main__.A] (self: Self`1) -> Self`1" |
| 2300 |
reveal_type(A.meth) # N: Revealed type is "def [Self <: __main__.A] (self: Self`2) -> Self`2" |
| 2301 |
return cls().meth() |
| 2302 |
|
| 2303 |
class B: |
| 2304 |
@identity |
| 2305 |
def meth(self) -> Self: ... |
| 2306 |
|
| 2307 |
@classmethod |
| 2308 |
def other_meth(cls) -> Self: |
| 2309 |
reveal_type(cls.meth) # N: Revealed type is "def [Self <: __main__.B] (self: Self`5) -> Self`5" |
| 2310 |
reveal_type(B.meth) # N: Revealed type is "def [Self <: __main__.B] (self: Self`6) -> Self`6" |
| 2311 |
return cls().meth() |
| 2312 |
|
| 2313 |
class C: |
| 2314 |
@classmethod |
| 2315 |
def other_meth(cls) -> Self: ... |
| 2316 |
|
| 2317 |
def meth(self) -> Self: |
| 2318 |
reveal_type(self.other_meth) # N: Revealed type is "def () -> Self`0" |
| 2319 |
reveal_type(type(self).other_meth) # N: Revealed type is "def () -> Self`0" |
| 2320 |
return self.other_meth() |
| 2321 |
[builtins fixtures/tuple.pyi] |
| 2322 |
|
| 2323 |
[case testSelfTypeUpperBoundFiler] |
| 2324 |
from typing import Generic, TypeVar, overload, Sequence |
| 2325 |
|
| 2326 |
class B: ... |
| 2327 |
class C(B): ... |
| 2328 |
|
| 2329 |
TB = TypeVar("TB", bound=B) |
| 2330 |
TC = TypeVar("TC", bound=C) |
| 2331 |
|
| 2332 |
class G(Generic[TB]): |
| 2333 |
@overload |
| 2334 |
def test(self: G[TC]) -> list[TC]: ... |
| 2335 |
@overload |
| 2336 |
def test(self: G[TB]) -> Sequence[TB]: ... |
| 2337 |
def test(self): |
| 2338 |
... |
| 2339 |
|
| 2340 |
class D1(B): ... |
| 2341 |
class D2(C): ... |
| 2342 |
|
| 2343 |
gb: G[D1] |
| 2344 |
gc: G[D2] |
| 2345 |
|
| 2346 |
reveal_type(gb.test()) # N: Revealed type is "typing.Sequence[__main__.D1]" |
| 2347 |
reveal_type(gc.test()) # N: Revealed type is "builtins.list[__main__.D2]" |
| 2348 |
[builtins fixtures/list.pyi] |
| 2349 |
|
| 2350 |
[case testEnumImplicitlyFinalForSelfType] |
| 2351 |
from enum import Enum |
| 2352 |
from typing import Self |
| 2353 |
|
| 2354 |
# This enum has members and so is implicitly final. |
| 2355 |
# Foo and Self are interchangeable within the class. |
| 2356 |
class Foo(Enum): |
| 2357 |
A = 1 |
| 2358 |
|
| 2359 |
@classmethod |
| 2360 |
def foo(cls) -> Self: |
| 2361 |
return Foo.A |
| 2362 |
|
| 2363 |
@classmethod |
| 2364 |
def foo2(cls) -> Self: |
| 2365 |
return cls.bar() |
| 2366 |
|
| 2367 |
@classmethod |
| 2368 |
def bar(cls) -> Foo: |
| 2369 |
... |
| 2370 |
|
| 2371 |
# This enum is empty and should not be assignable to Self |
| 2372 |
class Bar(Enum): |
| 2373 |
@classmethod |
| 2374 |
def foo(cls) -> Self: |
| 2375 |
return cls.bar() # E: Incompatible return value type (got "Bar", expected "Self") |
| 2376 |
|
| 2377 |
@classmethod |
| 2378 |
def bar(cls) -> Bar: |
| 2379 |
... |
| 2380 |
[builtins fixtures/classmethod.pyi] |