Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Un-support narrowing final globals in functions
  • Loading branch information
ilevkivskyi committed Apr 15, 2026
commit 4e7df67864fd0a0483f5ea09c8cc4d5c48d44fc6
4 changes: 0 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,10 +1659,6 @@ def is_var_redefined_in_outer_context(self, v: Var, after_line: int) -> bool:
Note that this doesn't do a full CFG analysis but uses a line number based
heuristic that isn't correct in some (rare) cases.
"""
if v.is_final:
# Final vars are definitely never reassigned.
return False

outers = self.tscope.outer_functions()
if not outers:
# Top-level function -- outer context is top level, and we can't reason about
Expand Down
9 changes: 3 additions & 6 deletions test-data/unit/check-final.test
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,7 @@ def check_final_init() -> None:
new_instance.__init__()
[builtins fixtures/tuple.pyi]

-- This is tricky and expensive to support in parallel mode.
-- We may want to stop supporting this niche use case
[case testNarrowingOfFinalPersistsInFunctions_no_parallel]
[case testNarrowingOfFinalInFunctionsNotSpecial]
from typing import Final, Union

def _init() -> Union[int, None]:
Expand All @@ -1249,18 +1247,17 @@ def _init() -> Union[int, None]:
FOO: Final = _init()

class Example:

if FOO is not None:
reveal_type(FOO) # N: Revealed type is "builtins.int"

def fn(self) -> int:
return FOO
return FOO # E: Incompatible return value type (got "int | None", expected "int")

if FOO is not None:
reveal_type(FOO) # N: Revealed type is "builtins.int"

def func() -> int:
return FOO
return FOO # E: Incompatible return value type (got "int | None", expected "int")

[case testDisjointBase]
from typing_extensions import disjoint_base
Expand Down
Loading