mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Support PEP 698 – Override Decorator

Open tmke8 opened this issue 3 years ago • 6 comments

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

tmke8 avatar Nov 12 '22 12:11 tmke8

The PEP has now been officially accepted for Python 3.12.

tmke8 avatar Feb 02 '23 15:02 tmke8

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").

tmke8 avatar May 12 '23 08:05 tmke8

@hauntsaninja , can you reopen this issue? It seems I can't do that.

tmke8 avatar May 12 '23 09:05 tmke8

FYI: this discussion may be relevant for whoever implements the flag. 😄

NeilGirdhar avatar May 12 '23 09:05 NeilGirdhar

Just opened #15512 to add a strict flag for method overrides as suggested in the PEP.

cdce8p avatar Jun 24 '23 21:06 cdce8p

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?

Avasam avatar Jul 10 '23 16:07 Avasam

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

mttbernardini avatar Jul 12 '23 13:07 mttbernardini

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?

bersbersbers avatar Jul 13 '23 14:07 bersbersbers

Are there plans for this error code to be included in strict mode 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.

cdce8p avatar Jul 13 '23 17:07 cdce8p

@bersbersbers I opened a tracking issue so that users can track when this is enabled by default in strict mode

  • #17511

johnthagen avatar Jul 09 '24 12:07 johnthagen