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
from __future__ importannotationsfromtypingimportSelf, finalclassA:
pass@finalclassB(A):
@finaldef__init__(self) ->None:
pass@classmethoddef__new__(cls: type[B]) ->B:
""" cls is type[B] and return is B, because class is final """new_instance: B=super().__new__(cls)
new_instance.__init__()
returnnew_instance@finalclassB2(A):
@finaldef__init__(self) ->None:
pass@classmethoddef__new__(cls) ->Self:
new_instance: Self=super().__new__(cls)
new_instance.__init__()
returnnew_instance
Expected Behavior
No errors reported.
Note that I think marking __init__ as @final is redundant, but I included it just to show that it doesn't make a difference. I would expect no errors if the class is marked @final OR if __init__ is marked @final. Or both, of course.
I also think the first case, where I explicitly annotate the B types, should not be necessary. Because the class is @final, I would expect the second case using Self to be fine. Again, I included that just to show that the error is reported either way.
Actual Behavior
main.py:17: error: Accessing "__init__" on an instance is unsound, since instance.__init__ could be from an incompatible subclass [misc]
main.py:29: error: Accessing "__init__" on an instance is unsound, since instance.__init__ could be from an incompatible subclass [misc]
Your Environment
Mypy version used: 1.15.0
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.13
The text was updated successfully, but these errors were encountered:
Bug Report
mypy
reports an error because of a concern about potentially incompatible subclasses, even though the class in question is marked@final
.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.13&gist=c475ec59941e76a62c4d458819ebc828
Expected Behavior
No errors reported.
Note that I think marking
__init__
as@final
is redundant, but I included it just to show that it doesn't make a difference. I would expect no errors if the class is marked@final
OR if__init__
is marked@final
. Or both, of course.I also think the first case, where I explicitly annotate the
B
types, should not be necessary. Because the class is@final
, I would expect the second case usingSelf
to be fine. Again, I included that just to show that the error is reported either way.Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: