mypy
mypy copied to clipboard
Support PEP 698 – Override Decorator
PEP 698 adds an @override decorator.
Todo:
- [x] basic support for
@override(done in #14609) - [ ] optional strict mode (relevant discussion: https://github.com/python/typing/issues/1376 )
Related issue: #1888
The PEP has now been officially accepted for Python 3.12.
Ah, I shouldn't have linked this issue to the PR because there is still a missing piece: a new flag which enforces the use of @override on every overriding method ("strict mode").
@hauntsaninja , can you reopen this issue? It seems I can't do that.
FYI: this discussion may be relevant for whoever implements the flag. 😄
Just opened #15512 to add a strict flag for method overrides as suggested in the PEP.
I just noticed the Method "<method_name>" is marked as an override, but no base method was found with this name error is categorised under misc instead of override. Is that intended?
There seems to be some issue with the way this plays with --check-untyped-defs, see this playground example:
from typing_extensions import override
class A:
def f(self, x: int) -> int:
return 2
class B(A):
@override
def f(self, x: int): # why `-> int` is not inferred?
return None # why return type violation is not reported?
@override
def g(self, y): # why "no base method" is not reported?
return 4
Thanks, this is great! I have already added enable_error_code = ["explicit-override"] to enable it.
Are there plans for this error code to be included in strict mode any time soon?
Are there plans for this error code to be included in
strictmode any time soon?
I don't think that will happen. Just an example, enabling this for Home Assistant would create 11k+ new errors and that's just one project. It'll probably take quite a long time until all code is sufficiently updated, especially given that @override will only be added to the stdlib in Python 3.12.
@bersbersbers I opened a tracking issue so that users can track when this is enabled by default in strict mode
- #17511