You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I create an iterator of classes, all of which inherit from the same super class, itself inheriting from abc.ABC, I expect the inferred type to be an Iterator[Type[common super class]]. However, instead I get Iterator[abc.ABCMeta*], which seems to be a bit too far up the inheritance chain for me. Note that when I infer the types of instances of these same classes, I do get Iterator[Edit*], as expected.
Below is a simplified example of when I got this behavior:
# example.pyfromabcimportABC, abstractmethodfromtypingimportTYPE_CHECKINGclassEdit(ABC):
@classmethod@abstractmethoddefdo_something(cls) ->None: ...
classInsertion(Edit):
@classmethoddefdo_something(cls):
print("foo")
classDeletion(Edit):
@classmethoddefdo_something(cls):
print("foo")
classSubstitution(Edit):
@classmethoddefdo_something(cls):
print("baz")
ifnotTYPE_CHECKING:
# define reveal_type as a noop outside of mypyreveal_type=lambda*x: ...
# mypy infers common subclass for instances.instances=Insertion(), Deletion(), Substitution()
# Revealed type is 'typing.Iterator[example.Edit*]reveal_type(iter(instances))
# mypy does NOT infer common subclass for classes themselves.classes= [Insertion, Deletion, Substitution]
# This assert passes; however...assertall(issubclass(cls, Edit) forclsinclasses)
# Revealed type is 'typing.Iterator[abc.ABCMeta*]reveal_type(iter(classes))
# I expect: Revealed type is 'typing.Iterator[example.Edit*]forclsinInsertion, Deletion, Substitution:
# Revealed type is 'abc.ABCMeta'reveal_type(cls)
# I expect: Revealed Type is 'Type[example.Edit]'
This is a simplified diagram of the inheritance hierarchy:
I've seen this too (there may even be a duplicate issue). I don't think 2922 is related. However I do think the join() implementation inside mypy could be taught to do this better. It's not very common, and the workaround (add an explicit type) is easy enough.
When I create an iterator of classes, all of which inherit from the same super class, itself inheriting from
abc.ABC
, I expect the inferred type to be anIterator[Type[common super class]]
. However, instead I getIterator[abc.ABCMeta*]
, which seems to be a bit too far up the inheritance chain for me. Note that when I infer the types of instances of these same classes, I do getIterator[Edit*]
, as expected.Below is a simplified example of when I got this behavior:
This is a simplified diagram of the inheritance hierarchy:
Explcitly typing works, however:
Possibly related to: #2922.
The text was updated successfully, but these errors were encountered: