Closed bersbersbers closed 1 year ago
Technically the @override
on __init__
is correct. It's just object.__init__
which is overridden.
Technically the
@override
on__init__
is correct. It's justobject.__init__
which is overridden.
But object.__init__
does not accept an int
, does it? So maybe this is not a false-negative explicit-override
, but a false-negative override
, which is correctly emitted in this example:
from typing_extensions import override
class Base:
def bar(self) -> None: ...
class Class(Base):
@override # true-positive "Signature incompatible"
def bar(self, _foo: int) -> None: ...
But
object.__init__
does not accept anint
, does it? So maybe this is not a false-negativeexplicit-override
, but a false-negativeoverride
By that logic would all custom __init__
methods be an invalid override if they add new parameters?
My general advice would be to not add @override
on __init__
or __new__
.
So maybe this is not a false-negative
explicit-override
, but a false-negativeoverride
, which is correctly emitted in this example:
No, mypy is correct in not applying the Liskov Substitution Principle to constructor or initialiser methods. See https://softwareengineering.stackexchange.com/questions/431549/the-liskov-substitution-principle-and-python for detailed answers as to why.
As @cdce8p says, it doesn't look like there's a bug here :)
Thanks for the explanation. Maybe I have a wrong understanding of @override
that I need to wrap my head around.
I had expected that, for a given function, either I necessarily need to add @override
or I necessarily need to omit it. I had not expected that there may be cases where both having @override
and not having @override
are valid. Looks very similar to the case in https://github.com/microsoft/pyright/issues/4788#issuecomment-1472237480, I guess.
Bug Report
To Reproduce
https://gist.github.com/mypy-play/247d7a5dc46ce24298509f6f9181edc6
Expected Behavior
Actual Behavior
Your Environment