Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
|
The mypy_primer diff is expected: the new cwltool errors are real errors in cwltool that this change unmasks, not a regression from the implementation. The downstream code is _generation: Final[str] = "http://commonwl.org/cwltool#generation"
def set_generation(self, obj: CWLFileType) -> None:
"""Register a File for mutation tracking."""
loc = obj["location"]
current = self.generations.get(loc, _MutationState(0, [], ""))
obj[_generation] = current.generation # type: ignore[literal-required]
def unset_generation(self, obj: CWLFileType) -> None:
"""Remove the mutation tracking metadata from a File."""
obj.pop(_generation, None) # type: ignore[misc]
Before this PR the type of from typing import Final, TypedDict
_generation: Final[str] = "http://commonwl.org/cwltool#generation"
class CWLFileType(TypedDict):
location: str
def set_generation(obj: CWLFileType) -> None:
obj[_generation] = 1 # type: ignore[literal-required]
def unset_generation(obj: CWLFileType) -> None:
obj.pop(_generation, None) # type: ignore[misc]So these are expected new errors: the code writes and pops a key that its |
|
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
pywin32 (https://github.com/mhammond/pywin32)
./pythonwin/pywin/test/_dbgscript.py: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
- version: 2.5.0+dev.b83696ea8099ef3772a5a2bc2c49f2b05340c37a
+ version: 2.5.0+dev.35851a1ca3746ed15c55224f00ee7d3462be1237
|
Fixes #22427.
A module-level
Finalcannot be rebound, so the value of its initializer is the value of every read of the variable. mypy already inferred the literal when the annotation was omitted; with an explicit annotation the declared type won, so the value was lost as soon as the variable was read from another module, where there is no code flow information to narrow it.Behavior
The value is recorded in
Instance.last_known_valuewhen a module-levelFinaldeclaration is checked — the same mechanism a bareFinal = 1already used:IS_WIN: Final[bool] = sys.platform == "win32"Literal[False]?NUM: Final[int] = 1Literal[1]?NAME: Final[str] = "hi"Literal['hi']?COLOR: Final[Color] = Color.REDLiteral[Color.RED]?MAYBE_WIN: bool = sys.platform == "win32"boolNO_INFO: Final[Base] = Sentinel()BaseITEMS: Final[list[int]] = []list[int]ClassWithFinal.class_final: Final[int] = 1intOnly literal values (
int/str/bool/enum member) are exposed. Widening any other inferred type would leak a subclass into everything inferred from such a read, which is why aFinalannotated with a supertype keeps it. Class-body, instance and function-localFinals keep the declared type, since a constructor can still assign them.A
Final[bool]initialized from a static platform or version check is folded withinfer_condition_value(), restricted toALWAYS_TRUE/ALWAYS_FALSE:TYPE_CHECKINGand friends are true for mypy but false at runtime, so those staybool.Branches guarded by such a variable are still analyzed —
if FLAG:withFLAG: Final = Falseis not pruned. That is #14604 and is not addressed here.Notable consequences
==/!=start propagating the constant, becausenarrow_type_by_identity_equalitycoerces the target throughcoerce_to_literal():x: Literal["hi", "bye"]; if x == NAME:now narrows toLiteral['hi'].Final, so'{:c}'.format(FINAL_STR)is an error when the value is not a single character.Tests
check-final.test: module-level literals, cross-module reads, non-literal initializers, non-module scopes, statically known bool initializers.testLiteralFinalDirectInstanceTypesSupersedeInferredLiteralandtestLiteralFineGrainedChainedViaFinalupdated: their "declared type wins" expectations no longer hold for module-level variables.testcheck.pyandtestfinegrained.pypass (9117 passed),mypy_self_check.iniclean,blackandruffclean.