Circular import under if TYPE_CHECKING
breaks subclass initialization signature
#12259
Labels
bug
mypy got something wrong
MVCE repository: https://github.com/jwodder/mypy-bug-20220227 (Run
tox -e typing
to see the failed type-check)Consider an
App
class with a method that createsWidget
instances based on a spec.Widget
is a base class, and the spec determines which child class gets instantiated.Now, the widgets keep a reference to the
App
, so in an attempt to avoid circular imports, the file containing theWidget
definition uses anif TYPE_CHECKING:
guard like so:The
RedWidget
child class is empty, but theBlueWidget
class adds an attribute:Now, if we put this all together and run mypy, we get an erroneous error:
Note that mypy only complains about the instantiation of
BlueWidget
, notRedWidget
. Also note that the same error occurs if attrs is replaced with dataclasses.I believe that this problem is caused by the circular import beneath the
if TYPE_CHECKING:
guard for some reason, as commenting it out and changing theWidget.app
annotation toAny
gets the type-checking to pass.Your Environment
Mypy version used: both 0.931 and commit feca706
Mypy command-line flags: none
Mypy configuration options from
mypy.ini
(and other config files):Python version used: 3.9.10
Operating system and version: macOS 11.6
The text was updated successfully, but these errors were encountered: