Skip to content

Commit 5672a06

Browse files
committed
Add tests of generic descriptors
1 parent 548d17c commit 5672a06

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test-data/unit/check-classes.test

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,49 @@ class A:
939939
a = A() # type: A
940940
reveal_type(a.f) # E: Revealed type is 'Union[__main__.D, builtins.str]'
941941

942+
[case testAccessingGenericNonDataDescriptor]
943+
from typing import TypeVar, Type, Generic
944+
945+
T = TypeVar('T')
946+
V = TypeVar('V')
947+
class D(Generic[T, V]):
948+
def __init__(self, value: V) -> None:
949+
self.value = value
950+
def __get__(self, instance: T, owner: Type[T]) -> V:
951+
return self.value
952+
953+
class A:
954+
f = D(10) # type: D[A, int]
955+
g = D('10') # type: D[A, str]
956+
957+
a = A() # type: A
958+
reveal_type(a.f) # E: Revealed type is 'builtins.int*'
959+
reveal_type(a.g) # E: Revealed type is 'builtins.str*'
960+
961+
[case testSettingGenericDataDescriptor]
962+
from typing import TypeVar, Type, Generic
963+
964+
T = TypeVar('T')
965+
V = TypeVar('V')
966+
class D(Generic[T, V]):
967+
def __init__(self, value: V) -> None:
968+
self.value = value
969+
def __get__(self, instance: T, owner: Type[T]) -> V:
970+
return self.value
971+
def __set__(self, instance: T, value: V) -> None:
972+
pass
973+
974+
class A:
975+
f = D(10) # type: D[A, int]
976+
g = D('10') # type: D[A, str]
977+
978+
a = A() # type: A
979+
a.f = 1
980+
a.f = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
981+
a.g = ''
982+
a.g = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
983+
984+
942985

943986
-- Multiple inheritance, non-object built-in class as base
944987
-- -------------------------------------------------------

0 commit comments

Comments
 (0)