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
I have a number of concrete classes which all derive from the same abstract base class. Creating a list of instances using __subclasses__() in a list comprehension, mypy complains that I'm trying to instantiate the abstract class.
$ python3 a.py
Traceback (most recent call last):
File "/Users/walshca/a.py", line 20, in <module>
animals: list[Animal] = [
^
File "/Users/walshca/a.py", line 21, in <listcomp>
subclass() for subclass in Animal.__subclasses__()
^^^^^^^^^^
TypeError: Can't instantiate abstract class Reptile with abstract method egg_type
NativeCommandExitException: Program "python3" ended with non-zero exit code: 1.
$ mypy a.py
a.py:21: error: Cannot instantiate abstract class "Animal" with abstract attribute "talk" [abstract]
Found 1 error in 1 file (checked 1 source file)
NativeCommandExitException: Program "mypy" ended with non-zero exit code: 1.
Instead, "enumerating concrete subclasses" would need to return something like typing.Concrete[Animal] (which doesn't exist), filtering __subclasses__() somehow (and probably recurse through subclasses of subclasses)
I have a number of concrete classes which all derive from the same abstract base class. Creating a list of instances using
__subclasses__()
in a list comprehension, mypy complains that I'm trying to instantiate the abstract class.Simple example:
https://mypy-play.net/?mypy=latest&python=3.11&gist=f03c51fac1173ae0bb751b00e614de68
Expected Behavior
As I'm not actually instantiating the abstract class, the mypy error looks like a false positive.
Your Environment
python 3.10.12
mypy v1.6.0
Possibly related issues
#15554
#14106
#3115
The text was updated successfully, but these errors were encountered: