Skip to content

Error on conditional initialization of Final class variables #1917

Closed

Description

Describe the bug
Conditional initialization of a Final class variable is reported as a reassignment error on elif/else branches.

To Reproduce

from typing import Final

class A:
    x: Final[int]

    def __init__(self, a, b):
        if a:
            self.x = 1
        elif b:
            self.x = 2
        else:
            self.x = 3
No configuration file found.
No pyproject.toml file found.
stubPath /home/omer/tmp/pyright/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
/home/omer/tmp/pyright/deneme.py
  /home/omer/tmp/pyright/deneme.py:10:18 - error: "x" is declared as Final and cannot be reassigned
  /home/omer/tmp/pyright/deneme.py:12:18 - error: "x" is declared as Final and cannot be reassigned
2 errors, 0 warnings, 0 infos
Completed in 0.596sec

Expected behavior
No errors on the first correct assignment of a final variable regardless of branching.

VS Code extension or command-line
CLI pyright 1.1.144

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions