Skip to content

Document --allow-redefinition-new #19153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,58 @@ 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
.. option:: --allow-redefinition-new

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

.. code-block:: python

def maybe_convert(n: int, b: bool) -> int | str:
if b:
x = str(n) # Assign "str"
else:
x = n # Assign "int"
# Type of "x" is "int | str" here.
return x

Without the new flag, mypy only supports inferring optional types
(``X | None``) from multiple assignments. With this option enabled,
mypy can infer arbitrary union types.

This also enables an unannotated variable to have different types in different
code locations:

.. code-block:: python

if check():
for x in range(n):
# Type of "x" is "int" here.
...
else:
for x in ['a', 'b']:
# Type of "x" is "str" here.
...

Note: We are planning to turn this flag on by default in a future mypy
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
The feature is still experimental, and the semantics may still change.

.. option:: --allow-redefinition

This is an older variant of
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
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 <mypy --allow-redefinition-new>`
will replace this flag for new use cases eventually.

Example where this can be useful:

.. code-block:: python
Expand Down
39 changes: 39 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,44 @@ 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

:type: boolean
:default: False

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

.. code-block:: python

def maybe_convert(n: int, b: bool) -> int | str:
if b:
x = str(n) # Assign "str"
else:
x = n # Assign "int"
# Type of "x" is "int | str" here.
return x

This also enables an unannotated variable to have different types in different
code locations:

.. code-block:: python

if check():
for x in range(n):
# Type of "x" is "int" here.
...
else:
for x in ['a', 'b']:
# Type of "x" is "str" here.
...

Note: We are planning to turn this flag on by default in a future mypy
release, along with :confval:`local_partial_types`.

.. confval:: allow_redefinition

:type: boolean
Expand Down Expand Up @@ -746,6 +784,7 @@ section of the command line docs.

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

.. confval:: disable_error_code

Expand Down