@@ -351,21 +351,130 @@ class E:
351351 def __init_subclass__(cls) -> None:
352352 reveal_type(cls) # E: Revealed type is 'def () -> __main__.E'
353353
354- [case testSelfTypeProperty]
355- from typing import TypeVar
354+ [case testSelfTypePropertyUnion]
355+ from typing import Union
356+ class A:
357+ @property
358+ def f(self: A) -> int: pass
356359
357- T = TypeVar('T', bound='A')
360+ class B:
361+ @property
362+ def f(self: B) -> int: pass
363+ x: Union[A, B]
364+ reveal_type(x.f) # E: Revealed type is 'builtins.int'
358365
359- class A:
366+ [builtins fixtures/property.pyi]
367+
368+ [case testSelfTypeProperSupertypeAttribute]
369+ from typing import Callable, TypeVar
370+ class K: pass
371+ T = TypeVar('T', bound=K)
372+ class A(K):
360373 @property
361- def member(self: T) -> T:
362- pass
374+ def g(self: K) -> int: return 0
375+ @property
376+ def gt(self: T) -> T: return self
377+ f: Callable[[object], int]
378+ ft: Callable[[T], T]
379+
380+ class B(A):
381+ pass
382+
383+ reveal_type(A().g) # E: Revealed type is 'builtins.int'
384+ reveal_type(A().gt) # E: Revealed type is '__main__.A*'
385+ reveal_type(A().f()) # E: Revealed type is 'builtins.int'
386+ reveal_type(A().ft()) # E: Revealed type is '__main__.A*'
387+ reveal_type(B().g) # E: Revealed type is 'builtins.int'
388+ reveal_type(B().gt) # E: Revealed type is '__main__.B*'
389+ reveal_type(B().f()) # E: Revealed type is 'builtins.int'
390+ reveal_type(B().ft()) # E: Revealed type is '__main__.B*'
391+
392+ [builtins fixtures/property.pyi]
393+
394+ [case testSelfTypeProperSupertypeAttributeTuple]
395+ from typing import Callable, TypeVar, Tuple
396+ T = TypeVar('T')
397+ class A(Tuple[int, int]):
398+ @property
399+ def g(self: object) -> int: return 0
400+ @property
401+ def gt(self: T) -> T: return self
402+ f: Callable[[object], int]
403+ ft: Callable[[T], T]
363404
364405class B(A):
365406 pass
366407
367- reveal_type(A().member) # E: Revealed type is '__main__.A*'
368- reveal_type(B().member) # E: Revealed type is '__main__.B*'
408+ reveal_type(A().g) # E: Revealed type is 'builtins.int'
409+ reveal_type(A().gt) # E: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.A]'
410+ reveal_type(A().f()) # E: Revealed type is 'builtins.int'
411+ reveal_type(A().ft()) # E: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.A]'
412+ reveal_type(B().g) # E: Revealed type is 'builtins.int'
413+ reveal_type(B().gt) # E: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.B]'
414+ reveal_type(B().f()) # E: Revealed type is 'builtins.int'
415+ reveal_type(B().ft()) # E: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.B]'
416+
417+ [builtins fixtures/property.pyi]
418+
419+ [case testSelfTypeProperSupertypeAttributeMeta]
420+ from typing import Callable, TypeVar, Type
421+ T = TypeVar('T')
422+ class A(type):
423+ @property
424+ def g(cls: object) -> int: return 0
425+ @property
426+ def gt(cls: T) -> T: return cls
427+ f: Callable[[object], int]
428+ ft: Callable[[T], T]
429+
430+ class B(A):
431+ pass
432+
433+ class X(metaclass=B):
434+ def __init__(self, x: int) -> None: pass
435+ class Y(X): pass
436+ X1: Type[X]
437+ reveal_type(X.g) # E: Revealed type is 'builtins.int'
438+ reveal_type(X.gt) # E: Revealed type is 'def (x: builtins.int) -> __main__.X'
439+ reveal_type(X.f()) # E: Revealed type is 'builtins.int'
440+ reveal_type(X.ft()) # E: Revealed type is 'def (x: builtins.int) -> __main__.X'
441+ reveal_type(Y.g) # E: Revealed type is 'builtins.int'
442+ reveal_type(Y.gt) # E: Revealed type is 'def (x: builtins.int) -> __main__.Y'
443+ reveal_type(Y.f()) # E: Revealed type is 'builtins.int'
444+ reveal_type(Y.ft()) # E: Revealed type is 'def (x: builtins.int) -> __main__.Y'
445+ reveal_type(X1.g) # E: Revealed type is 'builtins.int'
446+ reveal_type(X1.gt) # E: Revealed type is 'Type[__main__.X]'
447+ reveal_type(X1.f()) # E: Revealed type is 'builtins.int'
448+ reveal_type(X1.ft()) # E: Revealed type is 'Type[__main__.X]'
449+
450+ [builtins fixtures/property.pyi]
451+
452+ [case testSelfTypeProperSupertypeAttributeGeneric]
453+ from typing import Callable, TypeVar, Generic
454+ Q = TypeVar('Q', covariant=True)
455+ class K(Generic[Q]):
456+ q: Q
457+ T = TypeVar('T')
458+ class A(K[Q]):
459+ @property
460+ def g(self: K[object]) -> int: return 0
461+ @property
462+ def gt(self: K[T]) -> T: return self.q
463+ f: Callable[[object], int]
464+ ft: Callable[[T], T]
465+
466+ class B(A[Q]):
467+ pass
468+ a: A[int]
469+ b: B[str]
470+ reveal_type(a.g) # E: Revealed type is 'builtins.int'
471+ --reveal_type(a.gt) # E: Revealed type is 'builtins.int'
472+ reveal_type(a.f()) # E: Revealed type is 'builtins.int'
473+ reveal_type(a.ft()) # E: Revealed type is '__main__.A*[builtins.int]'
474+ reveal_type(b.g) # E: Revealed type is 'builtins.int'
475+ --reveal_type(b.gt) # E: Revealed type is '__main__.B*[builtins.str]'
476+ reveal_type(b.f()) # E: Revealed type is 'builtins.int'
477+ reveal_type(b.ft()) # E: Revealed type is '__main__.B*[builtins.str]'
369478
370479[builtins fixtures/property.pyi]
371480
@@ -376,3 +485,21 @@ class A:
376485 # def g(self: None) -> None: ... see in check-python2.test
377486[out]
378487main:3: error: Self argument missing for a non-static method (or an invalid type for self)
488+
489+ [case testUnionPropertyField]
490+ from typing import Union
491+
492+ class A:
493+ x: int
494+
495+ class B:
496+ @property
497+ def x(self) -> int: return 1
498+
499+ class C:
500+ @property
501+ def x(self) -> int: return 1
502+
503+ ab: Union[A, B, C]
504+ reveal_type(ab.x) # E: Revealed type is 'builtins.int'
505+ [builtins fixtures/property.pyi]
0 commit comments