Skip to content

Resolve module-level Final to its initializer literal - #22494

Open
DeviousCardi wants to merge 1 commit into
python:masterfrom
DeviousCardi:final-literal-module-level
Open

DeviousCardi wants to merge 1 commit into
python:masterfrom
DeviousCardi:final-literal-module-level

Conversation

@DeviousCardi

@DeviousCardi DeviousCardi commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #22427.

A module-level Final cannot be rebound, so a read from another module can use the statically known value of its initializer even when the annotation is explicit.

This stores the initializer literal as last_known_value on the module-level Var's Instance type. That field already serializes, so incremental and cross-module checks see it without a new AST field. The scope is deliberately limited:

  • only module-scope Final variables are affected;
  • mutable containers keep their declared type;
  • a supertype annotation such as Final[object] = Sentinel() keeps the declared type;
  • bool platform/version checks are folded only when they are true or false at runtime (TYPE_CHECKING and similar are not exposed).

The tests cover cross-module reads, direct imports, enum members, literals, bool expressions, containers and the class/instance/function-local exclusions. Updated two tests that pinned the old behaviour, plus the formatting/diff effects. Docs are updated.

Verification:

  • pytest mypy/test/testcheck.py -q: 8326 passed, 27 skipped, 7 xfailed
  • pytest mypy/test/testfinegrained.py -q: 756 passed, 27 skipped
  • pytest mypy/test/testfinegrainedcache.py -q: 558 passed, 229 skipped
  • pytest mypy/test/testmerge.py mypy/test/testdiff.py mypy/test/testdeps.py -q: 352 passed, 1 skipped
  • mypy self-check, black and ruff on the changed files: clean

This change was developed with AI assistance; the generated patch was reviewed and verified against the full checker and incremental test suites.

A module-level Final cannot be rebound, so a read from another module can use the statically known initializer value even when the annotation is explicit. Store the literal as last_known_value on the module-level Var's Instance type, which already serializes for incremental and cross-module checks. Limit this to module-scope Final variables and non-container instance types, and fold bool platform/version checks that are true or false at runtime.
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/mutation.py:92: error: Unused "type: ignore" comment  [unused-ignore]
+ cwltool/mutation.py: note: In member "set_generation" of class "MutationManager":
+ cwltool/mutation.py:92:13: error: TypedDict "CWLFileType" has no key "http://commonwl.org/cwltool#generation"  [typeddict-unknown-key]
+ cwltool/mutation.py:92:13: note: Error code "typeddict-unknown-key" not covered by "type: ignore[literal-required]" comment
+ cwltool/mutation.py: note: At top level:
+ cwltool/mutation.py:96: error: Unused "type: ignore" comment  [unused-ignore]
+ cwltool/mutation.py: note: In member "unset_generation" of class "MutationManager":
+ cwltool/mutation.py:96:17: error: TypedDict "CWLFileType" has no key "http://commonwl.org/cwltool#generation"  [typeddict-item]
+ cwltool/mutation.py:96:17: note: Error code "typeddict-item" not covered by "type: ignore[misc]" comment

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Resolve module-level Final to the literal value of its initializer

1 participant