Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix descriptor/Union interactions #5039

Merged
merged 1 commit into from May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/checkexpr.py
Expand Up @@ -1278,7 +1278,13 @@ def analyze_descriptor_access(self, instance_type: Type, descriptor_type: Type,
Return:
The return type of the appropriate ``__get__`` overload for the descriptor.
"""
if not isinstance(descriptor_type, Instance):
if isinstance(descriptor_type, UnionType):
# Map the access over union types
return UnionType.make_simplified_union([
self.analyze_descriptor_access(instance_type, typ, context)
for typ in descriptor_type.items
])
elif not isinstance(descriptor_type, Instance):
return descriptor_type

if not descriptor_type.type.has_readable_member('__get__'):
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-classes.test
Expand Up @@ -1495,6 +1495,20 @@ a = A()
a.f = 1
reveal_type(a.f) # E: Revealed type is 'builtins.str'

[case testDescriptorGetUnion]
from typing import Any, Union
class String:
def __get__(self, inst: Any, owner: Any) -> str:
return ''

class A:
attr: str

class B:
attr = String()

def foo(x: Union[A, B]) -> None:
reveal_type(x.attr) # E: Revealed type is 'builtins.str'

-- _promote decorators
-- -------------------
Expand Down