Skip to content

Commit a499d9f

Browse files
committed
Document --allow-redefinition-new (#19153)
The feature was introduced in #18727.
1 parent 96525a2 commit a499d9f

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

docs/source/command_line.rst

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,58 @@ of the above sections.
593593
This flag causes mypy to suppress errors caused by not being able to fully
594594
infer the types of global and class variables.
595595

596-
.. option:: --allow-redefinition
596+
.. option:: --allow-redefinition-new
597597

598598
By default, mypy won't allow a variable to be redefined with an
599-
unrelated type. This flag enables redefinition of a variable with an
599+
unrelated type. This *experimental* flag enables the redefinition of
600+
unannotated variables with an arbitrary type. You will also need to enable
601+
:option:`--local-partial-types <mypy --local-partial-types>`.
602+
Example:
603+
604+
.. code-block:: python
605+
606+
def maybe_convert(n: int, b: bool) -> int | str:
607+
if b:
608+
x = str(n) # Assign "str"
609+
else:
610+
x = n # Assign "int"
611+
# Type of "x" is "int | str" here.
612+
return x
613+
614+
Without the new flag, mypy only supports inferring optional types
615+
(``X | None``) from multiple assignments. With this option enabled,
616+
mypy can infer arbitrary union types.
617+
618+
This also enables an unannotated variable to have different types in different
619+
code locations:
620+
621+
.. code-block:: python
622+
623+
if check():
624+
for x in range(n):
625+
# Type of "x" is "int" here.
626+
...
627+
else:
628+
for x in ['a', 'b']:
629+
# Type of "x" is "str" here.
630+
...
631+
632+
Note: We are planning to turn this flag on by default in a future mypy
633+
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
634+
The feature is still experimental, and the semantics may still change.
635+
636+
.. option:: --allow-redefinition
637+
638+
This is an older variant of
639+
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
640+
This flag enables redefinition of a variable with an
600641
arbitrary type *in some contexts*: only redefinitions within the
601642
same block and nesting depth as the original definition are allowed.
643+
644+
We have no plans to remove this flag, but we expect that
645+
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`
646+
will replace this flag for new use cases eventually.
647+
602648
Example where this can be useful:
603649

604650
.. code-block:: python

docs/source/config_file.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,44 @@ section of the command line docs.
713713
Causes mypy to suppress errors caused by not being able to fully
714714
infer the types of global and class variables.
715715

716+
.. confval:: allow_redefinition_new
717+
718+
:type: boolean
719+
:default: False
720+
721+
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
724+
:confval:`local_partial_types`.
725+
Example:
726+
727+
.. code-block:: python
728+
729+
def maybe_convert(n: int, b: bool) -> int | str:
730+
if b:
731+
x = str(n) # Assign "str"
732+
else:
733+
x = n # Assign "int"
734+
# Type of "x" is "int | str" here.
735+
return x
736+
737+
This also enables an unannotated variable to have different types in different
738+
code locations:
739+
740+
.. code-block:: python
741+
742+
if check():
743+
for x in range(n):
744+
# Type of "x" is "int" here.
745+
...
746+
else:
747+
for x in ['a', 'b']:
748+
# Type of "x" is "str" here.
749+
...
750+
751+
Note: We are planning to turn this flag on by default in a future mypy
752+
release, along with :confval:`local_partial_types`.
753+
716754
.. confval:: allow_redefinition
717755

718756
:type: boolean
@@ -746,6 +784,7 @@ section of the command line docs.
746784

747785
Disallows inferring variable type for ``None`` from two assignments in different scopes.
748786
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.
749788

750789
.. confval:: disable_error_code
751790

0 commit comments

Comments
 (0)