From 5672a061cb19e9d0d0de402513aae64bc921f72d Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 19 Oct 2016 11:15:14 -0400 Subject: [PATCH] Add tests of generic descriptors --- test-data/unit/check-classes.test | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index ec66a4a5e74ba..63ce776fbe0ba 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -939,6 +939,49 @@ class A: a = A() # type: A reveal_type(a.f) # E: Revealed type is 'Union[__main__.D, builtins.str]' +[case testAccessingGenericNonDataDescriptor] +from typing import TypeVar, Type, Generic + +T = TypeVar('T') +V = TypeVar('V') +class D(Generic[T, V]): + def __init__(self, value: V) -> None: + self.value = value + def __get__(self, instance: T, owner: Type[T]) -> V: + return self.value + +class A: + f = D(10) # type: D[A, int] + g = D('10') # type: D[A, str] + +a = A() # type: A +reveal_type(a.f) # E: Revealed type is 'builtins.int*' +reveal_type(a.g) # E: Revealed type is 'builtins.str*' + +[case testSettingGenericDataDescriptor] +from typing import TypeVar, Type, Generic + +T = TypeVar('T') +V = TypeVar('V') +class D(Generic[T, V]): + def __init__(self, value: V) -> None: + self.value = value + def __get__(self, instance: T, owner: Type[T]) -> V: + return self.value + def __set__(self, instance: T, value: V) -> None: + pass + +class A: + f = D(10) # type: D[A, int] + g = D('10') # type: D[A, str] + +a = A() # type: A +a.f = 1 +a.f = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") +a.g = '' +a.g = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") + + -- Multiple inheritance, non-object built-in class as base -- -------------------------------------------------------