Skip to content

Commit

Permalink
Add tests of generic descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
cpennington committed Oct 19, 2016
1 parent 548d17c commit 5672a06
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- -------------------------------------------------------
Expand Down

0 comments on commit 5672a06

Please sign in to comment.