python / python/mypy

Assignment to Final attribute incorrectly flagged in conditional assignment within __init__

Offen
#20,443 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-final
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Documentation on typing.Final says (emphasis mine):

Final names cannot be reassigned in any scope. Final names declared in class scopes cannot be overridden in subclasses.

The problem comes in a class's init, if the final member is conditionally assigned to different values (e.g. assignments in an if/else expression, if the assignment to the member occurs in both blocks). In the if/else case, mypy flags the assignment in the else block.

To Reproduce

from typing import Final


class Foo:
    attr: Final[str]

    def __init__(self, flag: bool) -> None:
        if flag:
            self.attr = "bar"
        else:
            self.attr = "baz"  # mypy flags with:  error: Cannot assign to final attribute "attr"  [misc]

Expected Behavior

I would expect mypy to not flag this code at all. For either possible value of flag, there is only a single assignment, so no reassignment occurs.

Actual Behavior

Mypy flags line 11 with: error: Cannot assign to final attribute "attr" [misc]

Your Environment

  • Mypy version used: mypy 1.19.1 (compiled: yes)
  • Mypy command-line flags: none; just the path to the repro test script
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
python_version = "3.13"

# set mypy_path and explicit_package_bases, otherwise mypy gets confused about package names
# due to the use of namespace packages.
mypy_path = "src"
explicit_package_bases = true

pretty = true
disallow_any_unimported = true
disallow_any_expr = false
disallow_any_explicit = false
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_decorators = true
implicit_optional = true
strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
exclude = ["dist/.*/.*\\.py$"]
  • Python version used: 3.13.7

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem bereitgestellten Python-Reproducer und führe ihn mit mypy 1.19.1 aus, um die Diagnose bei der Zuweisung im else-Zweig zu bestätigen. Verfolge die Behandlung von Final-Attributen während der bedingten Zuweisung und füge eine Regressionstestabdeckung für das Beispiel hinzu; abgeschlossen ist die Aufgabe, wenn beide Zweige ohne den Reassignment-Fehler typgeprüft werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.