Skip to content

@final decorator is not honored on override with --check-untyped-defs #9618

Closed

Description

@tfranzel

Bug Report

Overriding class methods marked with @final (pep-0591) are not reported as errors. The example script will not produce an error with mypy even though it probably should.

#9612 is potentially related, but they have a final problem in the other direction.

To Reproduce

from typing import final

class Base:
    @final
    def foo(self):
        pass

    def bar(self):
        pass

class Derived(Base):
    def foo(self):
        pass

Your Environment

✗ python --version && mypy --version && mypy foo.py 
Python 3.8.5
mypy 0.790
Success: no issues found in 1 source file

Activity

  1. a-reich commented on Oct 27, 2020

    @a-reich

    Hi, just wanted to note that when I tried this example, mypy gives the expected error about final as soon as any annotation is added to Derived's foo method. It's possible that the design was intentionally not to check final for untyped functions? Either way, the workaround is easy.

    (There is the --check-untyped-defs option but that does not work to produce the error, which I guess makes sense since the docs say it enables checking the body of a function).

  2. tfranzel commented on Nov 1, 2020

    @tfranzel
    Author

    hey @a-reich, i cannot reproduce your statement. adding some other decorator didn't change the outcome.

    class Derived(Base):
        @cached_property
        @final
        def foo(self):   
            return 2 

    also using @final again with derived did nothing:

    class Derived(Base):
        @final
        def foo(self):   
            return 2 
  3. a-reich commented on Nov 1, 2020

    @a-reich

    @tfranzel Did you look at the mypy-playground example I linked to? Here's the content:

    from typing import final
    
    class Base:
        @final
        def foo(self):
            pass
    
        def bar(self):
            pass
    
    class Derived(Base):
        def foo(self) -> None:
            pass
    

    It's the same as your OP but with a return annotation " -> None" added to Derived.foo. When I said this is fixed by adding an annotation I meant in the usual PEP 484 sense of type annotations, not decorators.

  4. tfranzel commented on Nov 1, 2020

    @tfranzel
    Author

    ahh sorry i somehow missed the link. i see, so the functionality is there. nonetheless kind of weird to only activate the check once a type hint is present.

  5. JelleZijlstra commented on Nov 1, 2020

    @JelleZijlstra
    Member

    I suppose this is because of mypy's general principle to not give errors for unannotated code. It feels like we should give the error if --check-untyped-defs is on, though.

  6. changed the title [-]`@final` decorator is not honored. no error on override.[/-] [+]`@final` decorator is not honored on override with --check-untyped-defs[/+] on Aug 13, 2023
  7. added 3 commits that reference this issue on May 21, 2024
    41c00ca
    a463090
    3b5105e
  8. added a commit that references this issue on May 23, 2024
    25087fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions