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

Skip to content
Next Next commit
Rename allow_redefinitio_new -> allow_redefinition
  • Loading branch information
JukkaL committed Apr 20, 2026
commit 0590497785353e0757f7937d3c819328e83ca203
2 changes: 1 addition & 1 deletion mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def __init__(self, options: Options) -> None:
# If True, initial assignment to a simple variable (e.g. "x", but not "x.y")
# is added to the binder. This allows more precise narrowing and more
# flexible inference of variable types (--allow-redefinition-new).
self.bind_all = options.allow_redefinition_new
self.bind_all = options.allow_redefinition

# This tracks any externally visible changes in binder to invalidate
# expression caches when needed.
Expand Down
2 changes: 1 addition & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,7 @@ def semantic_analysis_pass1(self) -> None:
# TODO: Do this while constructing the AST?
self.tree.names = SymbolTable()
if not self.tree.is_stub:
if not self.options.allow_redefinition_new:
if not self.options.allow_redefinition:
# Perform some low-key variable renaming when assignments can't
# widen inferred types
self.tree.accept(LimitedVariableRenameVisitor())
Expand Down
22 changes: 11 additions & 11 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ def check_func_def(
new_frame.types[key] = narrowed_type
self.binder.declarations[key] = old_binder.declarations[key]

if self.options.allow_redefinition_new and not self.is_stub:
if self.options.allow_redefinition and not self.is_stub:
# Add formal argument types to the binder.
for arg in defn.arguments:
# TODO: Add these directly using a fast path (possibly "put")
Expand Down Expand Up @@ -3437,7 +3437,7 @@ def check_assignment(
# unpleasant, and a generalization of this would
# be an improvement!
if (
not self.options.allow_redefinition_new
not self.options.allow_redefinition
and is_literal_none(rvalue)
and isinstance(lvalue, NameExpr)
and lvalue.kind == LDEF
Expand Down Expand Up @@ -3497,7 +3497,7 @@ def check_assignment(
):
lvalue.node.type = remove_instance_last_known_values(lvalue_type)
elif (
self.options.allow_redefinition_new
self.options.allow_redefinition
and lvalue_type is not None
and not isinstance(lvalue_type, PartialType)
# Note that `inferred is not None` is not a reliable check here, because
Expand Down Expand Up @@ -4480,7 +4480,7 @@ def check_lvalue(
# When revisiting the initial assignment (for example in a loop),
# treat is as regular if redefinitions are allowed.
skip_definition = (
self.options.allow_redefinition_new
self.options.allow_redefinition
and isinstance(lvalue, NameExpr)
and isinstance(lvalue.node, Var)
and lvalue.node.is_inferred
Expand Down Expand Up @@ -4511,7 +4511,7 @@ def check_lvalue(
elif isinstance(lvalue, NameExpr):
lvalue_type = self.expr_checker.analyze_ref_expr(lvalue, lvalue=True)
if (
self.options.allow_redefinition_new
self.options.allow_redefinition
and isinstance(lvalue.node, Var)
# We allow redefinition for function arguments inside function body.
# Although we normally do this for variables without annotation, users
Expand Down Expand Up @@ -4599,13 +4599,13 @@ def infer_variable_type(
init_type = strip_type(init_type)

self.set_inferred_type(name, lvalue, init_type)
if self.options.allow_redefinition_new:
if self.options.allow_redefinition:
self.binder.assign_type(lvalue, init_type, init_type)

def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool:
init_type = get_proper_type(init_type)
if isinstance(init_type, NoneType) and (
isinstance(lvalue, MemberExpr) or not self.options.allow_redefinition_new
isinstance(lvalue, MemberExpr) or not self.options.allow_redefinition
):
# When using --allow-redefinition-new, None types aren't special
# when inferring simple variable types.
Expand Down Expand Up @@ -5024,7 +5024,7 @@ def replace_partial_type(
# Updating a partial type should invalidate expression caches.
self.binder.version += 1
del partial_types[var]
if self.options.allow_redefinition_new:
if self.options.allow_redefinition:
# When using --allow-redefinition-new, binder tracks all types of
# simple variables.
n = NameExpr(var.name)
Expand Down Expand Up @@ -5442,10 +5442,10 @@ def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
if isinstance(var.node, Var):
new_type = DeletedType(source=source)
var.node.type = new_type
if self.options.allow_redefinition_new:
if self.options.allow_redefinition:
# TODO: Should we use put() here?
self.binder.assign_type(var, new_type, new_type)
if not self.options.allow_redefinition_new:
if not self.options.allow_redefinition:
self.binder.cleanse(var)
if s.else_body:
self.accept(s.else_body)
Expand Down Expand Up @@ -9164,7 +9164,7 @@ def is_valid_inferred_type(
# type could either be NoneType or an Optional type, depending on
# the context. This resolution happens in leave_partial_types when
# we pop a partial types scope.
return is_lvalue_final or (not is_lvalue_member and options.allow_redefinition_new)
return is_lvalue_final or (not is_lvalue_member and options.allow_redefinition)
elif isinstance(proper_type, UninhabitedType):
return False
return not typ.accept(InvalidInferredTypes())
Expand Down
4 changes: 2 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ def main(
if options.cache_dir == os.devnull:
fail("error: cache must be enabled in parallel mode", stderr, options)

if options.allow_redefinition_new and not options.local_partial_types:
if options.allow_redefinition and not options.local_partial_types:
fail(
"error: --local-partial-types must be enabled if using --allow-redefinition-new",
stderr,
options,
)

if options.allow_redefinition_new and options.allow_redefinition_old:
if options.allow_redefinition and options.allow_redefinition_old:
fail(
"--allow-redefinition-old and --allow-redefinition-new should not be used together",
stderr,
Expand Down
4 changes: 2 additions & 2 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BuildType:
PER_MODULE_OPTIONS: Final = {
# Please keep this list sorted
"allow_redefinition_old",
"allow_redefinition_new",
"allow_redefinition",
"allow_untyped_globals",
"always_false",
"always_true",
Expand Down Expand Up @@ -235,7 +235,7 @@ def __init__(self) -> None:

# Allow flexible variable redefinition with an arbitrary type, in different
# blocks and at different nesting levels
self.allow_redefinition_new = False
self.allow_redefinition = False

# Prohibit equality, identity, and container checks for non-overlapping types.
# This makes 1 == '1', 1 in ['1'], and 1 is '1' errors.
Expand Down
6 changes: 3 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,14 @@ def refresh_partial(

def refresh_top_level(self, file_node: MypyFile) -> None:
"""Reanalyze a stale module top-level in fine-grained incremental mode."""
if self.options.allow_redefinition_new and not self.options.local_partial_types:
if self.options.allow_redefinition and not self.options.local_partial_types:
n = TempNode(AnyType(TypeOfAny.special_form))
n.line = 1
n.column = 0
n.end_line = 1
n.end_column = 0
self.fail("--local-partial-types must be enabled if using --allow-redefinition-new", n)
if self.options.allow_redefinition_new and self.options.allow_redefinition_old:
if self.options.allow_redefinition and self.options.allow_redefinition_old:
n = TempNode(AnyType(TypeOfAny.special_form))
n.line = 1
n.column = 0
Expand Down Expand Up @@ -4493,7 +4493,7 @@ def analyze_name_lvalue(
else:
lvalue.fullname = lvalue.name
if self.is_func_scope():
if unmangle(name) == "_" and not self.options.allow_redefinition_new:
if unmangle(name) == "_" and not self.options.allow_redefinition:
# Special case for assignment to local named '_': always infer 'Any'.
# This isn't needed with --allow-redefinition-new, since arbitrary
# types can be assigned to '_' anyway.
Expand Down