Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a41b0e

Browse files
committed
Minor updates
1 parent 87b52bc commit 5a41b0e

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

mypy/checker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class TypeChecker(NodeVisitor[None], TypeCheckerSharedApi, SplittingVisitor):
425425
# NOTE: The names might not be unique, they are only for debugging purposes.
426426
widened_vars: list[str]
427427
# Global variables widened inside a function body, to be propagated to
428-
# the module-level binder after the function is type checked.
428+
# the module-level binder after the function is type checked (with --allow-redefinition-new).
429429
_globals_widened_in_func: list[tuple[NameExpr, Type]]
430430
globals: SymbolTable
431431
modules: dict[str, MypyFile]
@@ -4964,7 +4964,7 @@ def can_widen_in_scope(self, name: NameExpr, orig_type: Type) -> bool:
49644964
"""Can a variable type be widened via assignment in the current scope?
49654965
49664966
Globals can only be widened from within a function if the original type
4967-
is None (backward compat with partial type handling of ``x = None``).
4967+
is None (backward compat with partial type handling of `x = None`).
49684968
"""
49694969
if (
49704970
name.kind == GDEF
@@ -8372,6 +8372,7 @@ def visit_nonlocal_decl(self, o: NonlocalDecl, /) -> None:
83728372

83738373
def visit_global_decl(self, o: GlobalDecl, /) -> None:
83748374
if self.options.allow_redefinition_new:
8375+
# Add names to binder, since their types could be widened
83758376
for name in o.names:
83768377
sym = self.globals.get(name)
83778378
if sym and isinstance(sym.node, Var) and sym.node.type is not None:

test-data/unit/fine-grained.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11736,3 +11736,29 @@ def bar() -> str: return "a"
1173611736
main:2: note: Revealed type is "None | builtins.int"
1173711737
==
1173811738
main:2: note: Revealed type is "None | builtins.str"
11739+
11740+
[case testGlobalNoneWidenedInFuncWithRedefinition3]
11741+
import m
11742+
reveal_type(m.x)
11743+
11744+
[file m.py]
11745+
# mypy: allow-redefinition-new
11746+
import m2
11747+
11748+
x = None
11749+
11750+
def foo() -> None:
11751+
def nested(self) -> None:
11752+
global x
11753+
x = m2.bar()
11754+
11755+
[file m2.py]
11756+
def bar() -> int: return 0
11757+
11758+
[file m2.py.2]
11759+
def bar() -> str: return "a"
11760+
11761+
[out]
11762+
main:2: note: Revealed type is "None | builtins.int"
11763+
==
11764+
main:2: note: Revealed type is "None | builtins.str"

0 commit comments

Comments
 (0)