python / python/mypy

Assignment to Final attribute incorrectly flagged in conditional assignment within __init__

Open
#20,443 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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 supplied Python reproducer and run it with mypy 1.19.1 to confirm the diagnostic on the else-branch assignment. Trace the handling of Final attributes during conditional assignment and add regression coverage for the example; done means both branches type-check without the reassignment error.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.