Skip to content

Inferencing type reference of abstract class inferred from concrete type values #8096

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

Merged
merged 4 commits into from
Dec 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def visit_callable_type(self, t: CallableType) -> ProperType:
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = join_similar_callables(t, self.s)
# We set the from_type_type flag to suppress error when a collection of
# concrete class objects gets inferred as their common abstract superclass.
if not ((t.is_type_obj() and t.type_object().is_abstract) or
(self.s.is_type_obj() and self.s.type_object().is_abstract)):
result.from_type_type = True
if any(isinstance(tp, (NoneType, UninhabitedType))
for tp in get_proper_types(result.arg_types)):
# We don't want to return unusable Callable, attempt fallback instead.
Expand Down
5 changes: 5 additions & 0 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,11 @@ def visit_callable_type(self, t: CallableType) -> ProperType:
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = meet_similar_callables(t, self.s)
# We set the from_type_type flag to suppress error when a collection of
# concrete class objects gets inferred as their common abstract superclass.
if not ((t.is_type_obj() and t.type_object().is_abstract) or
(self.s.is_type_obj() and self.s.type_object().is_abstract)):
result.from_type_type = True
if isinstance(get_proper_type(result.ret_type), UninhabitedType):
# Return a plain None or <uninhabited> instead of a weird function.
return self.default(self.s)
Expand Down
48 changes: 48 additions & 0 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,51 @@ default = Config({'cannot': 'modify'}) # OK
default[1] = 2 # E: Unsupported target for indexed assignment
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testSubclassOfABCFromDictionary]
from abc import abstractmethod, ABCMeta

class MyAbstractType(metaclass=ABCMeta):
@abstractmethod
def do(self): pass

class MyConcreteA(MyAbstractType):
def do(self):
print('A')

class MyConcreteB(MyAbstractType):
def do(self):
print('B')

class MyAbstractA(MyAbstractType):
@abstractmethod
def do(self): pass

class MyAbstractB(MyAbstractType):
@abstractmethod
def do(self): pass

my_concrete_types = {
'A': MyConcreteA,
'B': MyConcreteB,
}

my_abstract_types = {
'A': MyAbstractA,
'B': MyAbstractB,
}

reveal_type(my_concrete_types) # N: Revealed type is 'builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]'
reveal_type(my_abstract_types) # N: Revealed type is 'builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]'

a = my_concrete_types['A']()
a.do()
b = my_concrete_types['B']()
b.do()

c = my_abstract_types['A']() # E: Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'
c.do()
d = my_abstract_types['B']() # E: Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'
d.do()

[builtins fixtures/dict.pyi]