diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 099fc2c572cf..72caf65845ff 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -595,7 +595,7 @@ of the above sections. This flag causes mypy to suppress errors caused by not being able to fully infer the types of global and class variables. -.. option:: --allow-redefinition-new +.. option:: --allow-redefinition By default, mypy won't allow a variable to be redefined with an unrelated type. This flag enables the redefinition of *unannotated* @@ -613,7 +613,7 @@ of the above sections. # Type of "x" is "int | str" here. return x - Without the new flag, mypy only supports inferring optional types + Without this flag, mypy only supports inferring optional types (``X | None``) from multiple assignments. With this option enabled, mypy can infer arbitrary union types. @@ -646,24 +646,21 @@ of the above sections. Note: We are planning to turn this flag on by default in a future mypy release. -.. option:: --allow-redefinition +.. option:: --allow-redefinition-new - This is an alias to :option:`--allow-redefinition-old `. - In mypy v2.0 this will point to - :option:`--allow-redefinition-new `, and will - eventually became the default. + Deprecated alias for :option:`--allow-redefinition `. .. option:: --allow-redefinition-old - This is an older variant of - :option:`--allow-redefinition-new `. + This is an older, more limited variant of + :option:`--allow-redefinition `. This flag enables redefinition of a variable with an arbitrary type *in some contexts*: only redefinitions within the same block and nesting depth as the original definition are allowed. - We have no plans to remove this flag, but we expect that - :option:`--allow-redefinition-new ` - will replace this flag for new use cases eventually. + We have no plans to remove this flag, but + :option:`--allow-redefinition ` + is recommended for new use cases. Example where this can be useful: diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index eb21f1fad8a2..05520e97908e 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -713,7 +713,7 @@ section of the command line docs. Causes mypy to suppress errors caused by not being able to fully infer the types of global and class variables. -.. confval:: allow_redefinition_new +.. confval:: allow_redefinition :type: boolean :default: False @@ -763,6 +763,13 @@ section of the command line docs. Note: We are planning to turn this flag on by default in a future mypy release. +.. confval:: allow_redefinition_new + + :type: boolean + :default: False + + Deprecated alias for :confval:`allow_redefinition`. + .. confval:: allow_redefinition_old :type: boolean @@ -789,14 +796,6 @@ section of the command line docs. items = "100" # valid, items now has type str items = int(items) # valid, items now has type int -.. confval:: allow_redefinition - - :type: boolean - :default: False - - An alias to :confval:`allow_redefinition_old`, in mypy v2.0 this will point to - :confval:`allow_redefinition_new`, and will eventually became the default. - .. confval:: local_partial_types :type: boolean diff --git a/mypy/binder.py b/mypy/binder.py index adcc812badc9..8f1180973508 100644 --- a/mypy/binder.py +++ b/mypy/binder.py @@ -199,8 +199,8 @@ 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 + # flexible inference of variable types (--allow-redefinition). + self.bind_all = options.allow_redefinition # This tracks any externally visible changes in binder to invalidate # expression caches when needed. @@ -339,7 +339,7 @@ def update_from_options(self, frames: list[Frame]) -> bool: continue # Remove exact duplicates to save pointless work later, this is - # a micro-optimization for --allow-redefinition-new. + # a micro-optimization for --allow-redefinition. seen_types = set() resulting_types = [] for rv in resulting_values: diff --git a/mypy/build.py b/mypy/build.py index 204451e2fa4d..ec73e81fd41f 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -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()) diff --git a/mypy/checker.py b/mypy/checker.py index 5174952d2270..7dd94ccfbb89 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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") @@ -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 @@ -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 @@ -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 @@ -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 @@ -4599,15 +4599,15 @@ 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 using --allow-redefinition, None types aren't special # when inferring simple variable types. partial_type = PartialType(None, name) elif isinstance(init_type, Instance): @@ -4855,7 +4855,7 @@ def check_simple_assignment( get_proper_type(lvalue_type), AnyType ) - # If redefinitions are allowed (i.e. we have --allow-redefinition-new + # If redefinitions are allowed (i.e. we have --allow-redefinition # and a variable without annotation) or if a variable has union type we # try inferring r.h.s. twice with a fallback type context. The only exception # is TypedDicts, they are often useless without context. @@ -5024,8 +5024,8 @@ 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: - # When using --allow-redefinition-new, binder tracks all types of + if self.options.allow_redefinition: + # When using --allow-redefinition, binder tracks all types of # simple variables. n = NameExpr(var.name) n.node = var @@ -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) @@ -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()) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 747981916960..97fa01b8dd21 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -507,9 +507,9 @@ def parse_section( # Here we use `key` for original config section key, and `options_key` for # the corresponding Options attribute. options_key = key - # Match aliasing for command line flag. - if key.endswith("allow_redefinition"): - options_key += "_old" + # Match aliasing for deprecated config option name. + if options_key == "allow_redefinition_new": + options_key = "allow_redefinition" if key in config_types: ct = config_types[key] elif key in invalid_options: diff --git a/mypy/main.py b/mypy/main.py index e90ee961fc70..6159b914953d 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -103,16 +103,16 @@ 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", + "error: --local-partial-types must be enabled if using --allow-redefinition", 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", + "--allow-redefinition-old and --allow-redefinition should not be used together", stderr, options, ) @@ -888,9 +888,8 @@ def add_invertible_flag( "--allow-redefinition", default=False, strict_flag=False, - help="Alias to --allow-redefinition-old; will point to --allow-redefinition-new in v2.0", + help="Allow flexible variable redefinition with a new type", group=strictness_group, - dest="allow_redefinition_old", ) add_invertible_flag( @@ -905,8 +904,9 @@ def add_invertible_flag( "--allow-redefinition-new", default=False, strict_flag=False, - help="Allow more flexible variable redefinition semantics", + help="Deprecated alias for --allow-redefinition", group=strictness_group, + dest="allow_redefinition", ) add_invertible_flag( diff --git a/mypy/nativeparse.py b/mypy/nativeparse.py index fd90d85fa355..c1bdf6907b21 100644 --- a/mypy/nativeparse.py +++ b/mypy/nativeparse.py @@ -1,4 +1,4 @@ -# mypy: allow-redefinition-new, local-partial-types +# mypy: allow-redefinition, local-partial-types """Python parser that directly constructs a native AST (when compiled). Use a Rust extension to generate a serialized AST, and deserialize the AST directly diff --git a/mypy/options.py b/mypy/options.py index 81fd88345a43..7664684b2d4d 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -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", @@ -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. diff --git a/mypy/semanal.py b/mypy/semanal.py index 8fd09f5381aa..7c81663ffea8 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -706,23 +706,21 @@ 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: + self.fail("--local-partial-types must be enabled if using --allow-redefinition", n) + if self.options.allow_redefinition and self.options.allow_redefinition_old: n = TempNode(AnyType(TypeOfAny.special_form)) n.line = 1 n.column = 0 n.end_line = 1 n.end_column = 0 self.fail( - "--allow-redefinition-old and --allow-redefinition-new" - " should not be used together", - n, + "--allow-redefinition-old and --allow-redefinition should not be used together", n ) self.recurse_into_functions = False self.add_implicit_module_attrs(file_node) @@ -4493,9 +4491,9 @@ 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 + # This isn't needed with --allow-redefinition, since arbitrary # types can be assigned to '_' anyway. typ = AnyType(TypeOfAny.special_form) self.store_declared_types(lvalue, typ) diff --git a/test-data/unit/check-final.test b/test-data/unit/check-final.test index fb0fadb3d43e..8608962e00a4 100644 --- a/test-data/unit/check-final.test +++ b/test-data/unit/check-final.test @@ -385,7 +385,7 @@ class C: -- Reassignments [case testFinalReassignModuleVar] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import Final x: Final = 1 @@ -412,7 +412,7 @@ z: Final = 2 # E: Cannot redefine an existing name as final z = 3 # E: Cannot assign to final name "z" [case testFinalReassignModuleVar2] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import Final x: Final = 1 @@ -429,7 +429,7 @@ y y: Final = 3 # E: Cannot redefine an existing name as final [case testFinalReassignModuleVar3] -# flags: --disallow-redefinition +# flags: --disallow-redefinition-old from typing import Final x: Final = 1 diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 6a493fa21148..fbfb8c11ba8c 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2626,7 +2626,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineLocalWithDifferentTypeInline] -# mypy: allow-redefinition +# mypy: allow-redefinition-old def f() -> None: x = 0 reveal_type(x) # N: Revealed type is "builtins.int" @@ -2649,7 +2649,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [file mypy.ini] \[mypy] -allow_redefinition = true +allow_redefinition_old = true [case testRedefineLocalWithDifferentTypeConfigOld] # flags: --config-file tmp/mypy.ini @@ -2670,7 +2670,7 @@ def f() -> None: x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [file mypy.ini] \[mypy] -disallow_redefinition = true +disallow_redefinition_old = true [case testRevealSimpleTypes_no_verbose_reveal] from typing import Generic, TypeVar, Callable @@ -2701,7 +2701,7 @@ reveal_type(fx) # N: Revealed type is "def [Ts] (int, *args: *tuple[*Ts, int]) [builtins fixtures/tuple.pyi] [case testOnlyOneRedefinitionModeAllowed] -# flags: --allow-redefinition --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition --allow-redefinition-old x = 1 [out] -main:1: error: --allow-redefinition-old and --allow-redefinition-new should not be used together +main:1: error: --allow-redefinition-old and --allow-redefinition should not be used together diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index 55edba6c9c20..f4aab1ebfd75 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -7116,9 +7116,9 @@ from .lib import NT [out2] [case testNewRedefineAffectsCache] -# flags: --local-partial-types --allow-redefinition-new +# flags: --local-partial-types --allow-redefinition # flags2: --local-partial-types -# flags3: --local-partial-types --allow-redefinition-new +# flags3: --local-partial-types --allow-redefinition x = 0 if int(): x = "" diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index c08908eb8a3a..8830099fb300 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1201,7 +1201,7 @@ if int(): [builtins fixtures/for.pyi] [case testReusingInferredForIndex2] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: for a in [A()]: pass @@ -1221,7 +1221,7 @@ class B: pass [out] [case testReusingInferredForIndex3] -# flags: --disallow-redefinition +# flags: --disallow-redefinition-old def f() -> None: for a in [A()]: pass a = A() @@ -3050,14 +3050,14 @@ _ = 0 _ = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testUnusedTargetNotClass] -# flags: --allow-redefinition +# flags: --allow-redefinition-old class C: _, _ = 0, 0 _ = '' reveal_type(C._) # N: Revealed type is "builtins.str" [case testUnusedTargetNotClass2] -# flags: --disallow-redefinition +# flags: --disallow-redefinition-old class C: _, _ = 0, 0 _ = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") @@ -4351,7 +4351,7 @@ def g() -> None: reveal_type(x) # N: Revealed type is "builtins.int | None" [case testGlobalVariableNoneInitMultipleFuncsRedefine] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition # Widening this is intentionally prohibited (for now). x = None diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 8afcb682712e..c510dc924d87 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -2801,7 +2801,7 @@ while x is not None and b(): [builtins fixtures/primitives.pyi] [case testAvoidFalseNonOverlappingEqualityCheckInLoop1] -# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable +# flags: --allow-redefinition --strict-equality --warn-unreachable def f(x: int) -> None: while True: @@ -2813,7 +2813,7 @@ def f(x: int) -> None: [builtins fixtures/primitives.pyi] [case testAvoidFalseNonOverlappingEqualityCheckInLoop2] -# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable +# flags: --allow-redefinition --strict-equality --warn-unreachable class A: ... class B: ... @@ -2870,7 +2870,7 @@ while b(): [builtins fixtures/bool.pyi] [case testAvoidFalseUnreachableInFinally] -# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable +# flags: --allow-redefinition --strict-equality --warn-unreachable def f() -> None: try: x = 1 diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index b9dc59fd1ebf..d155ba915e5d 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -2979,7 +2979,7 @@ def match_stmt_error5(x: Optional[str]) -> None: [builtins fixtures/tuple.pyi] [case testMatchSubjectRedefinition] -# flags: --allow-redefinition --strict-equality --warn-unreachable +# flags: --allow-redefinition-old --strict-equality --warn-unreachable def transform1(a: str) -> int: ... @@ -3510,7 +3510,7 @@ class Two: ... [builtins fixtures/tuple.pyi] [case testNewRedefineMatchBasics] -# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable +# flags: --allow-redefinition --strict-equality --warn-unreachable def f1(x: int | str | list[bytes]) -> None: match x: @@ -3523,7 +3523,7 @@ def f1(x: int | str | list[bytes]) -> None: reveal_type(y) # N: Revealed type is "builtins.str | builtins.bytes" [case testNewRedefineLoopWithMatch] -# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable +# flags: --allow-redefinition --strict-equality --warn-unreachable def f1() -> None: while True: diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index 51b47b6746fd..92b6946cc284 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -279,7 +279,7 @@ def loop(): [builtins fixtures/exception.pyi] [case testRedefineLocalWithinExceptStarTryClauses] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def fn_str(_: str) -> int: ... def fn_int(_: int) -> None: ... def fn_exc(_: Exception) -> str: ... diff --git a/test-data/unit/check-redefine.test b/test-data/unit/check-redefine.test index c5636c28caf0..f21ab805e17f 100644 --- a/test-data/unit/check-redefine.test +++ b/test-data/unit/check-redefine.test @@ -6,7 +6,7 @@ [case testRedefineLocalWithDifferentType] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x = 0 reveal_type(x) # N: Revealed type is "builtins.int" @@ -14,7 +14,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testCannotConditionallyRedefineLocalWithDifferentType] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: y = 0 reveal_type(y) # N: Revealed type is "builtins.int" @@ -25,7 +25,7 @@ def f() -> None: reveal_type(y) # N: Revealed type is "builtins.int" [case testRedefineFunctionArg] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f(x: int) -> None: g(x) x = '' @@ -36,7 +36,7 @@ def g(x: int) -> None: reveal_type(x) # N: Revealed type is "builtins.int" [case testRedefineAnnotationOnly] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x: int x = '' \ @@ -50,7 +50,7 @@ def g() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineLocalUsingOldValue] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import TypeVar, Union T = TypeVar('T') @@ -65,7 +65,7 @@ def f(x: int) -> None: def g(x: T) -> Union[T, str]: pass [case testRedefineLocalForLoopIndexVariable] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import Iterable def f(a: Iterable[int], b: Iterable[str]) -> None: for x in a: @@ -87,7 +87,7 @@ def h(a: Iterable[int]) -> None: for x in a: pass [case testCannotRedefineLocalWithinTry] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def g(): pass def f() -> None: try: @@ -104,7 +104,7 @@ def f() -> None: y = '' [case testRedefineLocalWithinTryClauses] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def fn_str(_: str) -> int: ... def fn_int(_: int) -> None: ... @@ -140,7 +140,7 @@ def across_blocks() -> None: reveal_type(a) # N: Revealed type is "builtins.str" [case testRedefineLocalExceptVar] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def fn_exc(_: Exception) -> str: ... def exc_name() -> None: @@ -151,7 +151,7 @@ def exc_name() -> None: [builtins fixtures/exception.pyi] [case testRedefineNestedInTry] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def fn_int(_: int) -> None: ... @@ -166,7 +166,7 @@ except: pass [case testRedefineLocalWithinWith] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: with g(): x = 0 @@ -181,7 +181,7 @@ def f() -> None: def g(): pass [case testCannotRedefineAcrossNestedFunction] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x = 0 x @@ -196,7 +196,7 @@ def f() -> None: y = '' [case testCannotRedefineAcrossNestedDecoratedFunction] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def dec(f): return f def f() -> None: @@ -214,7 +214,7 @@ def f() -> None: y = '' [case testCannotRedefineAcrossNestedOverloadedFunction] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import overload def f() -> None: @@ -235,7 +235,7 @@ def f() -> None: y = '' [case testRedefineLocalInMultipleAssignment] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x, x = 1, '' reveal_type(x) # N: Revealed type is "builtins.str" @@ -249,7 +249,7 @@ def g() -> None: # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testRedefineUnderscore] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: _, _ = 1, '' if 1: @@ -258,7 +258,7 @@ def f() -> None: reveal_type(_) # N: Revealed type is "builtins.int" [case testRedefineWithBreakAndContinue] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: y = 0 y @@ -292,7 +292,7 @@ def g() -> None: def h(): pass [case testRedefineLocalAndNestedLoops] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: z = 0 z @@ -310,7 +310,7 @@ def f() -> None: z = '' [case testCannotRedefineVarAsFunction] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: def x(): pass x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]") @@ -319,7 +319,7 @@ def f() -> None: def y(): pass # E: Name "y" already defined on line 6 [case testCannotRedefineVarAsClass] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: class x: pass x = 1 # E: Cannot assign to a type \ @@ -328,7 +328,7 @@ def f() -> None: class y: pass # E: Name "y" already defined on line 5 [case testRedefineVarAsTypeVar] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import TypeVar def f() -> None: x = TypeVar('x') @@ -344,7 +344,7 @@ def f() -> None: [typing fixtures/typing-full.pyi] [case testCannotRedefineVarAsModule] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: import typing as m m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module) @@ -354,7 +354,7 @@ def f() -> None: [typing fixtures/typing-full.pyi] [case testRedefineLocalWithTypeAnnotation] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x = 1 reveal_type(x) # N: Revealed type is "builtins.int" @@ -377,7 +377,7 @@ def farg2(x: int) -> None: x: str = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") [case testRedefineLocalWithTypeAnnotationSpecialCases] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f() -> None: x: object x = 1 @@ -392,7 +392,7 @@ def f() -> None: [case testCannotRedefineSelf] -# flags: --allow-redefinition +# flags: --allow-redefinition-old class A: x = 0 @@ -410,7 +410,7 @@ def f() -> A: return A() [case testRedefineGlobalWithDifferentType] -# flags: --allow-redefinition +# flags: --allow-redefinition-old import m reveal_type(m.x) # N: Revealed type is "builtins.str" [file m.py] @@ -422,7 +422,7 @@ x = '' reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineGlobalForIndex] -# flags: --allow-redefinition +# flags: --allow-redefinition-old import m reveal_type(m.x) # N: Revealed type is "builtins.str" [file m.py] @@ -437,7 +437,7 @@ for x in it2: reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineGlobalBasedOnPreviousValues] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import TypeVar, Iterable T = TypeVar('T') def f(x: T) -> Iterable[T]: pass @@ -446,7 +446,7 @@ a = f(a) reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.int]" [case testRedefineGlobalWithSeparateDeclaration] -# flags: --allow-redefinition +# flags: --allow-redefinition-old x = '' reveal_type(x) # N: Revealed type is "builtins.str" x: int @@ -459,7 +459,7 @@ if int(): x = object() [case testRedefineGlobalUsingForLoop] -# flags: --allow-redefinition +# flags: --allow-redefinition-old from typing import Iterable, TypeVar, Union T = TypeVar('T') def f(x: T) -> Iterable[Union[T, str]]: pass @@ -470,7 +470,7 @@ for x in f(x): reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNoRedefinitionIfOnlyInitialized] -# flags: --allow-redefinition --no-strict-optional +# flags: --allow-redefinition-old --no-strict-optional x = None # type: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int") @@ -481,14 +481,14 @@ y = 0 y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testNoRedefinitionIfNoValueAssigned] -# flags: --allow-redefinition +# flags: --allow-redefinition-old x: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" x: object [case testNoRedefinitionIfExplicitlyDisallowed] -# flags: --disallow-redefinition +# flags: --disallow-redefinition-old x = 0 x = 2 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") @@ -506,7 +506,7 @@ def g() -> None: [builtins fixtures/tuple.pyi] [case testRedefineAsException] -# flags: --allow-redefinition +# flags: --allow-redefinition-old e = 1 reveal_type(e) # N: Revealed type is "builtins.int" try: @@ -518,7 +518,7 @@ reveal_type(e) # N: Revealed type is "builtins.str" [builtins fixtures/exception.pyi] [case testRedefineUsingWithStatement] -# flags: --allow-redefinition +# flags: --allow-redefinition-old class A: def __enter__(self) -> int: ... def __exit__(self, x, y, z) -> None: ... diff --git a/test-data/unit/check-redefine2.test b/test-data/unit/check-redefine2.test index d8a7ccbfc4a4..0edffe90493d 100644 --- a/test-data/unit/check-redefine2.test +++ b/test-data/unit/check-redefine2.test @@ -1,7 +1,7 @@ -- Test cases for the redefinition of variable with a different type (new version). [case testNewRedefineLocalWithDifferentType] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> None: x = 0 reveal_type(x) # N: Revealed type is "builtins.int" @@ -9,7 +9,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineConditionalLocalWithDifferentType] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> None: if int(): x = 0 @@ -19,7 +19,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineMergeConditionalLocal1] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if int(): x = 0 @@ -35,7 +35,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.int | None" [case testNewRedefineMergeConditionalLocal2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def nested_ifs() -> None: if int(): if int(): @@ -52,7 +52,7 @@ def nested_ifs() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None | builtins.bytes" [case testNewRedefineUninitializedCodePath1] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if int(): x = 0 @@ -61,7 +61,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineUninitializedCodePath2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union def f1() -> None: @@ -72,7 +72,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineUninitializedCodePath3] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union def f1() -> None: @@ -83,7 +83,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineUninitializedCodePath4] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union def f1() -> None: @@ -92,7 +92,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineUninitializedCodePath5] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union def f1() -> None: @@ -104,7 +104,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.int | None" [case testNewRedefineUninitializedCodePath6] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union x: Union[str, None] @@ -115,7 +115,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.str | None" [case testNewRedefineHasAttr] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union def test(lst: list[object]) -> None: @@ -148,7 +148,7 @@ reveal_type(y) # N: Revealed type is "Literal[False] | Any" [builtins fixtures/isinstancelist.pyi] [case testNewRedefineGlobalVariableSimple] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition if int(): x = 0 reveal_type(x) # N: Revealed type is "builtins.int" @@ -169,7 +169,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineGlobalVariableNoneInit] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition x = None def f() -> None: @@ -181,7 +181,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "None" [case testNewRedefineParameterTypes] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Optional def f1(x: Optional[str] = None) -> None: @@ -201,7 +201,7 @@ class C: [case testNewRedefineClassBody] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class C: if int(): x = 0 @@ -214,7 +214,7 @@ class C: reveal_type(C.x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineNestedFunctionBasics] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if int(): x = 0 @@ -239,7 +239,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineLambdaBasics] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x = 0 if int(): @@ -252,7 +252,7 @@ def f1() -> None: reveal_type(f) # N: Revealed type is "def () -> builtins.int | builtins.str" [case testNewRedefineAssignmentExpression] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if x := int(): reveal_type(x) # N: Revealed type is "builtins.int" @@ -275,7 +275,7 @@ def f3() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineOperatorAssignment] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class D: pass class C: def __add__(self, x: C) -> D: ... @@ -287,7 +287,7 @@ if int(): reveal_type(c) # N: Revealed type is "__main__.C | __main__.D" [case testNewRedefineImportFrom-xfail] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition if int(): from m import x else: @@ -306,7 +306,7 @@ x = 1 y = "" [case testNewRedefineImport] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition if int(): import m else: @@ -322,7 +322,7 @@ y = "" [builtins fixtures/module.pyi] [case testNewRedefineOptionalTypesSimple] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x = None if int(): @@ -363,7 +363,7 @@ else: reveal_type(z) # N: Revealed type is "None | builtins.int | builtins.str" [case testNewRedefinePartialTypeForInstanceVariable] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class C1: def __init__(self) -> None: self.x = None @@ -415,7 +415,7 @@ reveal_type(C5().x) # N: Revealed type is "builtins.list[builtins.str] | None" [builtins fixtures/list.pyi] [case testNewRedefinePartialGenericTypes] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: a = [] a.append(1) @@ -465,7 +465,7 @@ def f6() -> None: [builtins fixtures/list.pyi] [case testNewRedefineFinalLiteral] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Final, Literal x: Final = "foo" @@ -479,7 +479,7 @@ reveal_type(B.x) # N: Revealed type is "Literal['bar']?" [builtins fixtures/tuple.pyi] [case testNewRedefineAnnotatedVariable] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Optional def f1() -> None: @@ -513,7 +513,7 @@ class C: reveal_type(self.x) # N: Revealed type is "builtins.str" [case testNewRedefineAnyType1] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def a(): pass def f1() -> None: @@ -574,7 +574,7 @@ def f7() -> None: reveal_type(x) # N: Revealed type is "builtins.int" [case testNewRedefineAnyType2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any def f1() -> None: @@ -599,7 +599,7 @@ def f3(x) -> None: reveal_type(x) # N: Revealed type is "Any | builtins.int" [case tetNewRedefineDel] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x = "" reveal_type(x) # N: Revealed type is "builtins.str" @@ -642,7 +642,7 @@ def f5() -> None: x = "" reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineWhileLoopSimple] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> None: while int(): x = "" @@ -660,7 +660,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]" [case testNewRedefineWhileLoopOptional] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x = None while int(): @@ -677,7 +677,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "None | builtins.str" [case testNewRedefineWhileLoopPartialType] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x = [] while int(): @@ -686,7 +686,7 @@ def f1() -> None: [builtins fixtures/list.pyi] [case testNewRedefineWhileLoopComplex1] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: while True: @@ -697,7 +697,7 @@ def f1() -> None: [builtins fixtures/exception.pyi] [case testNewRedefineWhileLoopComplex2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class C: def __enter__(self) -> str: ... @@ -719,7 +719,7 @@ y = "" [builtins fixtures/tuple.pyi] [case testNewRedefineReturn] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if int(): x = 0 @@ -737,7 +737,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineBreakAndContinue] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def b() -> None: while int(): x = "" @@ -761,7 +761,7 @@ def c() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" [case testNewRedefineUnderscore] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> None: if int(): _ = 0 @@ -772,7 +772,7 @@ def f() -> None: reveal_type(_) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineWithStatement] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class C: def __enter__(self) -> int: ... def __exit__(self, x, y, z): ... @@ -796,7 +796,7 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineNoWideningDeferred] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def deferred() -> None: c: C x = 1 @@ -811,7 +811,7 @@ class C: def defer() -> int: ... [case testNewRedefineTryStatement] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class E(Exception): pass def g(): ... @@ -870,7 +870,7 @@ def f4() -> None: [builtins fixtures/exception.pyi] [case testNewRedefineRaiseStatement] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: if int(): x = "" @@ -895,7 +895,7 @@ def f2() -> None: [case testNewRedefineMultipleAssignment] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: x, y = 1, "" reveal_type(x) # N: Revealed type is "builtins.int" @@ -917,7 +917,7 @@ def f2() -> None: reveal_type(y) # N: Revealed type is "builtins.str | builtins.int" [case testNewRedefineForLoopBasics] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: for x in [1]: reveal_type(x) # N: Revealed type is "builtins.int" @@ -939,7 +939,7 @@ def f2() -> None: [builtins fixtures/for.pyi] [case testNewRedefineForLoop1] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def l() -> list[int]: return [] @@ -962,7 +962,7 @@ def f3() -> None: [builtins fixtures/for.pyi] [case testNewRedefineForLoop2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any def f(a: Any) -> None: @@ -972,7 +972,7 @@ def f(a: Any) -> None: [builtins fixtures/isinstance.pyi] [case testNewRedefineForStatementIndexNarrowing] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import TypedDict class X(TypedDict): @@ -1002,7 +1002,7 @@ for b in ("hourly", "daily"): [typing fixtures/typing-full.pyi] [case testNewRedefineForLoopIndexWidening] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f1() -> None: for x in [1]: @@ -1027,7 +1027,7 @@ def f3() -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineVariableAnnotatedInLoop] -# flags: --allow-redefinition-new --local-partial-types --enable-error-code=redundant-expr +# flags: --allow-redefinition --enable-error-code=redundant-expr from typing import Optional def f1() -> None: @@ -1052,7 +1052,7 @@ def f2(e: Optional[str]) -> None: reveal_type(e) # N: Revealed type is "builtins.str | None" [case testNewRedefineLoopAndPartialTypesSpecialCase] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> list[str]: a = [] # type: ignore o = [] @@ -1069,7 +1069,7 @@ def f() -> list[str]: [builtins fixtures/list.pyi] [case testNewRedefineFinalVariable] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Final x: Final = "foo" @@ -1082,12 +1082,11 @@ class C: # E: Incompatible types in assignment (expression has type "int", variable has type "str") [case testNewRedefineEnableUsingComment] -# flags: --local-partial-types import a import b [file a.py] -# mypy: allow-redefinition-new +# mypy: allow-redefinition if int(): x = 0 else: @@ -1106,22 +1105,22 @@ import a import b [file a.py] -# mypy: local-partial-types, allow-redefinition-new +# mypy: local-partial-types, allow-redefinition x = 0 if int(): x = "" [file b.py] -# mypy: local-partial-types=false, allow-redefinition-new +# mypy: local-partial-types=false, allow-redefinition x = 0 if int(): x = "" [out] -tmp/b.py:1: error: --local-partial-types must be enabled if using --allow-redefinition-new +tmp/b.py:1: error: --local-partial-types must be enabled if using --allow-redefinition [case testNewRedefineNestedLoopInfiniteExpansion] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def a(): ... def f() -> None: @@ -1136,7 +1135,7 @@ def f() -> None: [builtins fixtures/tuple.pyi] [case testNewRedefinePartialNoneEmptyList] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def func() -> None: l = None # E: Need type annotation for "l" @@ -1147,7 +1146,7 @@ def func() -> None: [builtins fixtures/list.pyi] [case testNewRedefineLoopProgressiveWidening] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def foo() -> None: while int(): if int(): @@ -1162,7 +1161,7 @@ def foo() -> None: [builtins fixtures/isinstancelist.pyi] [case testNewRedefineAnyOrEmptyUnpackInLoop] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any def foo() -> None: @@ -1174,7 +1173,7 @@ def foo() -> None: [builtins fixtures/isinstancelist.pyi] [case testNewRedefineNarrowingSpecialCase] -# flags: --allow-redefinition-new --local-partial-types --warn-unreachable +# flags: --allow-redefinition --warn-unreachable from typing import Any, Union def get() -> Union[tuple[Any, Any], tuple[None, None]]: ... @@ -1190,7 +1189,7 @@ def f() -> None: [builtins fixtures/tuple.pyi] [case testNewRedefinitionPartialTypeInLoopDeferred] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f(self) -> None: x = [] @@ -1207,7 +1206,7 @@ def defer() -> int: ... [builtins fixtures/list.pyi] [case testNewRedefineCorrectChangedOnPop] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Optional def foo() -> None: @@ -1219,7 +1218,7 @@ def foo() -> None: name = "a" [case testNewRedefinePartialTypeForUnderscore] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def t() -> tuple[int]: return (42,) @@ -1240,7 +1239,7 @@ def f4() -> None: [builtins fixtures/tuple.pyi] [case testNewRedefineUseInferredTypedDictTypeForContext] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import TypedDict class TD(TypedDict): @@ -1254,14 +1253,14 @@ def f() -> None: [typing fixtures/typing-typeddict.pyi] [case testNewRedefineEmptyGeneratorUsingUnderscore] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def f() -> None: gen = (_ for _ in ()) reveal_type(gen) # N: Revealed type is "typing.Generator[Any, None, None]" [builtins fixtures/tuple.pyi] [case testNewRedefineCannotWidenImportedVariable] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition import a import b reveal_type(a.x) # N: Revealed type is "builtins.str" @@ -1275,7 +1274,7 @@ if int(): x = "a" [case testNewRedefineCannotWidenGlobalOrClassVariableWithMemberRef] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import ClassVar import a @@ -1295,7 +1294,7 @@ reveal_type(C.y) # N: Revealed type is "builtins.str" x = "a" [case testNewRedefineWidenGlobalInInitModule] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition import pkg [file pkg/__init__.py] @@ -1305,7 +1304,7 @@ if int(): reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineNarrowingOnFirstAssignment] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any li: list[int] @@ -1322,7 +1321,7 @@ def test() -> None: [builtins fixtures/primitives.pyi] [case testNewRedefineAnyNoSpecialCasing] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any def test() -> None: @@ -1339,7 +1338,7 @@ def test() -> None: reveal_type(y) # N: Revealed type is "Any | builtins.int" [case testNewRedefineSequentialRedefinitionToEmpty] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition items = ["a", "b", "c"] if bool(): items = ["a", "b", "c"] @@ -1351,7 +1350,7 @@ reveal_type(items) # N: Revealed type is "builtins.list[builtins.str] | builtin [builtins fixtures/primitives.pyi] [case testNewRedefineSequentialRedefinitionToEmpty2] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition items = ["a", "b", "c"] reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]" items = [item.split() for item in items] @@ -1368,7 +1367,7 @@ x = [] reveal_type(x) # N: Revealed type is "builtins.list[Never]" [case testNewRedefineRedefineToSubtypeItem] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition class Super: ... class Sub(Super): ... @@ -1379,7 +1378,7 @@ else: reveal_type(x[0]) # N: Revealed type is "__main__.Sub" [case testNewRedefineFunctionArgumentsFullContext] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Optional def process(items: list[Optional[str]]) -> None: @@ -1389,7 +1388,7 @@ def process(items: list[Optional[str]]) -> None: process(items) # OK [case testNewRedefineUnionArgumentFallbackUnion] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Union, Optional, TypeVar, Sequence T = TypeVar("T") @@ -1401,7 +1400,7 @@ def test(x: Optional[str]) -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineUnionArgumentFallbackAsync] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Optional, TypeVar T = TypeVar("T") @@ -1413,7 +1412,7 @@ async def test(x: Optional[str]) -> None: reveal_type(x) # N: Revealed type is "builtins.str" [case testNewRedefineFunctionArgumentsEmptyContext] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def process1(items: list[str]) -> None: items = [item.split() for item in items] reveal_type(items) # N: Revealed type is "builtins.list[builtins.list[builtins.str]]" @@ -1432,7 +1431,7 @@ def process3(items: list[str]) -> None: [builtins fixtures/primitives.pyi] [case testNewRedefineNarrowingForNestedUnionWithAny] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any, Union a: Any @@ -1446,7 +1445,7 @@ def test(x: Union[str, Int]) -> None: reveal_type(y) # N: Revealed type is "Any" [case testNewRedefineNarrowingForNestedUnionWithNone] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition from typing import Any, Union a: Any @@ -1486,7 +1485,7 @@ def test(x: Union[str, Int]) -> None: reveal_type(y) # N: Revealed type is "builtins.str | Any | builtins.int" [case testNewRedefineWidenedArgumentDeferral] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition def foo(x: int) -> None: reveal_type(x) # N: Revealed type is "builtins.int" if bool(): diff --git a/test-data/unit/check-union-error-syntax.test b/test-data/unit/check-union-error-syntax.test index 59ada4153566..c72f7316ea6c 100644 --- a/test-data/unit/check-union-error-syntax.test +++ b/test-data/unit/check-union-error-syntax.test @@ -26,7 +26,7 @@ x = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", v [builtins fixtures/tuple.pyi] [case testOrSyntaxRecombined] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition --local-partial-types # The following revealed type is recombined because the finally body is visited twice. try: x = 1 diff --git a/test-data/unit/semanal-basic.test b/test-data/unit/semanal-basic.test index 773092cab623..6cdb7db73753 100644 --- a/test-data/unit/semanal-basic.test +++ b/test-data/unit/semanal-basic.test @@ -341,7 +341,7 @@ MypyFile:1( NameExpr(self [l])))))) [case testGlobalDefinedInBlock] -# flags: --allow-redefinition +# flags: --allow-redefinition-old if object: x = object() x = x diff --git a/test-data/unit/semanal-statements.test b/test-data/unit/semanal-statements.test index 58a073e0507b..13999c4ed02d 100644 --- a/test-data/unit/semanal-statements.test +++ b/test-data/unit/semanal-statements.test @@ -157,7 +157,7 @@ MypyFile:1( NameExpr(x [l]))))) [case testReusingForLoopIndexVariable] -# flags: --allow-redefinition +# flags: --allow-redefinition-old for x in None: pass for x in None: @@ -176,7 +176,7 @@ MypyFile:1( PassStmt:5()))) [case testReusingForLoopIndexVariable2] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f(): for x in None: pass @@ -357,7 +357,7 @@ MypyFile:1( IntExpr(1))) [case testStarLvalues] -# flags: --allow-redefinition +# flags: --allow-redefinition-old *x, y = 1 *x, (y, *z) = 1 *(x, q), r = 1 @@ -388,7 +388,7 @@ MypyFile:1( IntExpr(1))) [case testMultipleDefinition] -# flags: --allow-redefinition +# flags: --allow-redefinition-old x, y = 1 x, y = 2 [out] @@ -824,7 +824,7 @@ MypyFile:1( NameExpr(Err [__main__.Err]) Args()))))) [case testMultipleAssignmentWithPartialNewDef] -# flags: --allow-redefinition +# flags: --allow-redefinition-old o = None x, o = o, o [out] @@ -950,7 +950,7 @@ MypyFile:1( NameExpr(b [l])))))))) [case testRenameGlobalVariable] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f(a): pass x = 0 f(x) @@ -984,7 +984,7 @@ MypyFile:1( NameExpr(x [__main__.x]))))) [case testNoRenameGlobalVariable] -# flags: --disallow-redefinition +# flags: --disallow-redefinition-old def f(a): pass x = 0 f(x) @@ -1017,7 +1017,7 @@ MypyFile:1( NameExpr(x [__main__.x]))))) [case testRenameLocalVariable] -# flags: --allow-redefinition +# flags: --allow-redefinition-old def f(a): f(a) a = '' @@ -1044,7 +1044,7 @@ MypyFile:1( NameExpr(a' [l]))))))) [case testCannotRenameExternalVarWithinClass] -# flags: --allow-redefinition +# flags: --allow-redefinition-old x = 0 x class A: