python / python/mypy

treat uppercase variables as `Final`

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

Nobody has claimed this yet.

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

Description

Feature

A = 1
A = 2

make A be treated as

from typing import Final


A: Final = 1
A = 2

thus, it will emit error with

test.py:1: error: Cannot assign to final name "A" (because it is uppercase)
Found 1 error in 1 file (checked 1 source file)

Pitch
It would be convenient to not add Final to uppercase variables.
Since pep 8 states that constants are named with uppercase, uppercase variables could be considered Final.

image
Pylance also uses this convention and warns when UPPERCASE variables are mutated.

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 tracing how mypy currently handles assignments to names annotated with typing.Final, then compare that path with the uppercase-constant convention described in the issue. Use the test.py examples as the behavioral cases: an uppercase name initialized once should be treated as final, and a later assignment should produce the shown error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.