Repository navigation
Resolve module-level Final to its initializer literal - #22494
Open
DeviousCardi wants to merge 1 commit into
Open
DeviousCardi wants to merge 1 commit into
DeviousCardi wants to merge 1 commit into
Conversation
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.
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22427.
A module-level
Finalcannot 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_valueon the module-levelVar'sInstancetype. That field already serializes, so incremental and cross-module checks see it without a new AST field. The scope is deliberately limited:Finalvariables are affected;Final[object] = Sentinel()keeps the declared type;TYPE_CHECKINGand 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 xfailedpytest mypy/test/testfinegrained.py -q: 756 passed, 27 skippedpytest mypy/test/testfinegrainedcache.py -q: 558 passed, 229 skippedpytest mypy/test/testmerge.py mypy/test/testdiff.py mypy/test/testdeps.py -q: 352 passed, 1 skippedThis change was developed with AI assistance; the generated patch was reviewed and verified against the full checker and incremental test suites.