Skip to content

Commit 19e777a

Browse files
authored
Internally rename allow_redefiniton to allow_redefinition_old (#20764)
Ref #20726 This also make `--allow-redefinition` simply an alias to `--allow-redefinition-old`. In v2.0 we can simply flip this to point to `--allow-redefinition-new`. IMO this way there is less confusion between publicly visible flags and internal mypy options. Also I no longer call `--allow-redefinition-new` experimental, and clarify timeline to mention v2.0 release. I also use this opportunity to use more consistent variable names in config parser (`key` vs `options_key`).
1 parent a022715 commit 19e777a

11 files changed

Lines changed: 109 additions & 25 deletions

File tree

docs/source/command_line.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ of the above sections.
598598
.. option:: --allow-redefinition-new
599599

600600
By default, mypy won't allow a variable to be redefined with an
601-
unrelated type. This *experimental* flag enables the redefinition of
602-
unannotated variables with an arbitrary type. You will also need to enable
601+
unrelated type. This flag enables the redefinition of unannotated
602+
variables with an arbitrary type. You will also need to enable
603603
:option:`--local-partial-types <mypy --local-partial-types>`.
604604
Example:
605605

@@ -637,6 +637,13 @@ of the above sections.
637637

638638
.. option:: --allow-redefinition
639639

640+
This is an alias to :option:`--allow-redefinition-old <mypy --allow-redefinition-old>`.
641+
In mypy v2.0 this will point to
642+
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`, and will
643+
eventually became the default.
644+
645+
.. option:: --allow-redefinition-old
646+
640647
This is an older variant of
641648
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
642649
This flag enables redefinition of a variable with an
@@ -689,7 +696,7 @@ of the above sections.
689696
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
690697
691698
Note: this option is always implicitly enabled in mypy daemon and will become
692-
enabled by default for mypy in a future release.
699+
enabled by default in mypy v2.0 release.
693700

694701
.. option:: --no-implicit-reexport
695702

docs/source/config_file.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,8 @@ section of the command line docs.
719719
:default: False
720720

721721
By default, mypy won't allow a variable to be redefined with an
722-
unrelated type. This *experimental* flag enables the redefinition of
723-
unannotated variables with an arbitrary type. You will also need to enable
722+
unrelated type. This flag enables the redefinition of unannotated
723+
variables with an arbitrary type. You will also need to enable
724724
:confval:`local_partial_types`.
725725
Example:
726726

@@ -751,7 +751,7 @@ section of the command line docs.
751751
Note: We are planning to turn this flag on by default in a future mypy
752752
release, along with :confval:`local_partial_types`.
753753

754-
.. confval:: allow_redefinition
754+
.. confval:: allow_redefinition_old
755755

756756
:type: boolean
757757
:default: False
@@ -777,14 +777,22 @@ section of the command line docs.
777777
items = "100" # valid, items now has type str
778778
items = int(items) # valid, items now has type int
779779
780+
.. confval:: allow_redefinition
781+
782+
:type: boolean
783+
:default: False
784+
785+
An alias to :confval:`allow_redefinition_old`, in mypy v2.0 this will point to
786+
:confval:`allow_redefinition_new`, and will eventually became the default.
787+
780788
.. confval:: local_partial_types
781789

782790
:type: boolean
783791
:default: False
784792

785793
Disallows inferring variable type for ``None`` from two assignments in different scopes.
786794
This is always implicitly enabled when using the :ref:`mypy daemon <mypy_daemon>`.
787-
This will be enabled by default in a future mypy release.
795+
This will be enabled by default in mypy v2.0 release.
788796

789797
.. confval:: disable_error_code
790798

mypy/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ def semantic_analysis_pass1(self) -> None:
27642764
# Perform some low-key variable renaming when assignments can't
27652765
# widen inferred types
27662766
self.tree.accept(LimitedVariableRenameVisitor())
2767-
if options.allow_redefinition:
2767+
if options.allow_redefinition_old:
27682768
# Perform more renaming across the AST to allow variable redefinitions
27692769
self.tree.accept(VariableRenameVisitor())
27702770
self.time_spent_us += time_spent_us(t0)

mypy/config_parser.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,12 @@ def parse_section(
504504

505505
for key in section:
506506
invert = False
507+
# Here we use `key` for original config section key, and `options_key` for
508+
# the corresponding Options attribute.
507509
options_key = key
510+
# Match aliasing for command line flag.
511+
if key.endswith("allow_redefinition"):
512+
options_key += "_old"
508513
if key in config_types:
509514
ct = config_types[key]
510515
elif key in invalid_options:
@@ -515,7 +520,7 @@ def parse_section(
515520
)
516521
continue
517522
else:
518-
dv = getattr(template, key, None)
523+
dv = getattr(template, options_key, None)
519524
if dv is None:
520525
if key.endswith("_report"):
521526
report_type = key[:-7].replace("_", "-")
@@ -526,17 +531,17 @@ def parse_section(
526531
continue
527532
if key.startswith("x_"):
528533
pass # Don't complain about `x_blah` flags
529-
elif key.startswith("no_") and hasattr(template, key[3:]):
530-
options_key = key[3:]
534+
elif key.startswith("no_") and hasattr(template, options_key[3:]):
535+
options_key = options_key[3:]
531536
invert = True
532-
elif key.startswith("allow") and hasattr(template, "dis" + key):
533-
options_key = "dis" + key
537+
elif key.startswith("allow") and hasattr(template, "dis" + options_key):
538+
options_key = "dis" + options_key
534539
invert = True
535-
elif key.startswith("disallow") and hasattr(template, key[3:]):
536-
options_key = key[3:]
540+
elif key.startswith("disallow") and hasattr(template, options_key[3:]):
541+
options_key = options_key[3:]
537542
invert = True
538-
elif key.startswith("show_") and hasattr(template, "hide_" + key[5:]):
539-
options_key = "hide_" + key[5:]
543+
elif key.startswith("show_") and hasattr(template, "hide_" + options_key[5:]):
544+
options_key = "hide_" + options_key[5:]
540545
invert = True
541546
elif key == "strict":
542547
pass # Special handling below

mypy/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,15 @@ def add_invertible_flag(
891891
"--allow-redefinition",
892892
default=False,
893893
strict_flag=False,
894+
help="Alias to --allow-redefinition-old; will point to --allow-redefinition-new in v2.0",
895+
group=strictness_group,
896+
dest="allow_redefinition_old",
897+
)
898+
899+
add_invertible_flag(
900+
"--allow-redefinition-old",
901+
default=False,
902+
strict_flag=False,
894903
help="Allow restricted, unconditional variable redefinition with a new type",
895904
group=strictness_group,
896905
)
@@ -899,7 +908,7 @@ def add_invertible_flag(
899908
"--allow-redefinition-new",
900909
default=False,
901910
strict_flag=False,
902-
help="Allow more flexible variable redefinition semantics (experimental)",
911+
help="Allow more flexible variable redefinition semantics",
903912
group=strictness_group,
904913
)
905914

mypy/options.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BuildType:
2121

2222
PER_MODULE_OPTIONS: Final = {
2323
# Please keep this list sorted
24-
"allow_redefinition",
24+
"allow_redefinition_old",
2525
"allow_redefinition_new",
2626
"allow_untyped_globals",
2727
"always_false",
@@ -222,10 +222,10 @@ def __init__(self) -> None:
222222

223223
# Allow variable to be redefined with an arbitrary type in the same block
224224
# and the same nesting level as the initialization
225-
self.allow_redefinition = False
225+
self.allow_redefinition_old = False
226226

227227
# Allow flexible variable redefinition with an arbitrary type, in different
228-
# blocks and and at different nesting levels
228+
# blocks and at different nesting levels
229229
self.allow_redefinition_new = False
230230

231231
# Prohibit equality, identity, and container checks for non-overlapping types.

mypyc/irbuild/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ def get_default() -> Value:
15311531

15321532

15331533
def remangle_redefinition_name(name: str) -> str:
1534-
"""Remangle names produced by mypy when allow-redefinition is used and a name
1534+
"""Remangle names produced by mypy when allow-redefinition-old is used and a name
15351535
is used with multiple types within a single block.
15361536
15371537
We only need to do this for locals, because the name is used as the name of the register;

mypyc/test-data/irbuild-basic.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,7 @@ L4:
34693469
return 1
34703470

34713471
[case testLocalRedefinition]
3472-
# mypy: allow-redefinition
3472+
# mypy: allow-redefinition-old
34733473
def f() -> None:
34743474
i = 0
34753475
i += 1

mypyc/test-data/run-misc.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ def test_underscore() -> None:
11341134
B()
11351135

11361136
[case testGlobalRedefinition_toplevel]
1137-
# mypy: allow-redefinition
1137+
# mypy: allow-redefinition-old
11381138
i = 0
11391139
i += 1
11401140
i = "foo"

mypyc/test-data/run-vecs-misc.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-- vec[i64] test cases are in run-vecs-i64.test.
66

77
[case testVecMiscBasicOps_librt_experimental]
8-
# mypy: allow-redefinition
8+
# mypy: allow-redefinition-old
99

1010
from typing import Any, cast
1111
from mypy_extensions import i64, i32, i16, u8

0 commit comments

Comments
 (0)