python / python/mypy

Overloading an abstract attribute with a Final one

Open
#14,863 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-final topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

In the following code it would be useful to mark toto as final to make sure no code modifies it by error. However this triggers Cannot override writable attribute "toto" with a final one

from typing import Final

class Base:
    toto: str = NotImplemented
    def access_toto(self) -> str:
        return self.toto

class CmdA(Base):
    toto: Final[str] = "toto"

In fact I tend to agree with that error, in that what I'd really like to do is to annotate toto as "abstract final", which is not possible either as the use of toto: Final[str] triggers Final name must be initialized with a value, and toto: Final[str] = NotImplemented still gets the
Cannot override final attribute "toto" (previously declared in base class "Base") the previous attempt also showed.

  • Mypy version used: 1.1.1, 0.812
  • Python version used: 3.9.2

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 by reproducing the supplied example with mypy 1.1.1 and 0.812, then compare the diagnostics for Final attributes and inheritance. No files or tests are named in the report; done requires an agreed interpretation of abstract-final attributes and corresponding behavior or diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.