-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
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
from __future__ import annotations
from typing import Self, final
class A:
pass
@final
class B(A):
@final
def __init__(self) -> None:
pass
@classmethod
def __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__()
return new_instance
@final
class B2(A):
@final
def __init__(self) -> None:
pass
@classmethod
def __new__(cls) -> Self:
new_instance: Self = super().__new__(cls)
new_instance.__init__()
return new_instanceExpected 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