python / python/mypy

Strange error when declaring `@final` property

Open
#15,325 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Consider this code:

from typing import final

class A:
    def __init__(self):
        self._x = 4

    @property
    @final  # E: @final should be applied only to overload implementation
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value) -> None:
        self._x = value
        
        
class B(A):

    @property  # E: Cannot override final attribute "x" (previously declared in base class "A")
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value) -> None:
        self._x = value

https://mypy-play.net/?mypy=latest&python=3.11&gist=a4a1981a3db4ac546b9947bb344a5fd5

Mypy complains that @final is not applied to the "overload implementation", but the property is still successfully declared final.

I tried moving the @final decorator to the setter – in that case, mypy's first error goes away, but then also the second error vanishes, meaning that x wasn't actually marked as final.

(I also tried moving @final above @property but that produces the same result.)

Environment:
See mypy-play link above.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible example in the issue or the linked mypy-play case, comparing @final placement on the property getter and setter. Trace how mypy validates final decorators and records property attributes, then add coverage for this declaration and verify that the diagnostics consistently reflect whether the property is final.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.