Skip to content
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
38 changes: 17 additions & 21 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ of the above sections.

By default, mypy won't allow a variable to be redefined with an
unrelated type. This 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:
variables with an arbitrary type. This also requires
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is
enabled by default starting from mypy 2.0. Example:

.. code-block:: python

Expand Down Expand Up @@ -644,7 +644,7 @@ of the above sections.
reveal_type(values) # Revealed type is list[float]

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>`.
release.

.. option:: --allow-redefinition

Expand Down Expand Up @@ -684,30 +684,26 @@ of the above sections.
items = "100" # valid, items now has type str
items = int(items) # valid, items now has type int

.. option:: --local-partial-types
.. option:: --no-local-partial-types

In mypy, the most common cases for partial types are variables initialized using ``None``,
but without explicit ``X | None`` annotations. By default, mypy won't check partial types
spanning module top level or class top level. This flag changes the behavior to only allow
partial types at local level, therefore it disallows inferring variable type for ``None``
from two assignments in different scopes. For example:
Disable local partial types to enable legacy type inference mode for
containers.

.. code-block:: python
Local partial types prevent inferring a container type for a variable, when
the initial assignment happens at module top level or in a class body, and
the container item type is only set in a function. Example:

a = None # Need type annotation here if using --local-partial-types
b: int | None = None
.. code-block:: python

class Foo:
bar = None # Need type annotation here if using --local-partial-types
baz: int | None = None
a = [] # Need type annotation unless using --no-local-partial-types

def __init__(self) -> None:
self.bar = 1
def func() -> None:
a.append(1)

reveal_type(Foo().bar) # 'int | None' without --local-partial-types
reveal_type(a) # "list[int]" if using --no-local-partial-types

Note: this option is always implicitly enabled in mypy daemon and will become
enabled by default in mypy v2.0 release.
Local partial types are enabled by default starting from mypy 2.0. The
mypy daemon requires local partial types.

.. option:: --no-implicit-reexport

Expand Down
16 changes: 8 additions & 8 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ section of the command line docs.

By default, mypy won't allow a variable to be redefined with an
unrelated type. This flag enables the redefinition of unannotated
variables with an arbitrary type. You will also need to enable
:confval:`local_partial_types`.
Example:
variables with an arbitrary type. This also requires
:confval:`local_partial_types`, which is enabled by default starting
from mypy 2.0. Example:

.. code-block:: python

Expand Down Expand Up @@ -761,7 +761,7 @@ section of the command line docs.
reveal_type(values) # Revealed type is list[float]

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

.. confval:: allow_redefinition_old

Expand Down Expand Up @@ -800,11 +800,11 @@ section of the command line docs.
.. confval:: local_partial_types

:type: boolean
:default: False
:default: True

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 mypy v2.0 release.
This prevents inferring a variable type from an empty container (such as a list or
a dictionary) created at module top level or class body and updated in
a function. This must be enabled when using the :ref:`mypy daemon <mypy_daemon>`.

.. confval:: disable_error_code

Expand Down
3 changes: 2 additions & 1 deletion docs/source/mypy_daemon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ you have a large codebase.)

.. note::

The mypy daemon requires ``--local-partial-types`` and automatically enables it.
The mypy daemon requires ``--local-partial-types``, which is enabled
by default starting from mypy 2.0.


Daemon client commands
Expand Down
Loading