Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc6f6ee

Browse files
authored
Document that --local-partial-types are enabled by default (#21274)
The flag was enabled by default in #21163. Also simplify the description of the feature (and make it up to date) since this feature is mostly a legacy thing now.
1 parent bf62d3e commit dc6f6ee

3 files changed

Lines changed: 27 additions & 30 deletions

File tree

docs/source/command_line.rst

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ of the above sections.
599599

600600
By default, mypy won't allow a variable to be redefined with an
601601
unrelated type. This flag enables the redefinition of *unannotated*
602-
variables with an arbitrary type. You will also need to enable
603-
:option:`--local-partial-types <mypy --local-partial-types>`.
604-
Example:
602+
variables with an arbitrary type. This also requires
603+
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is
604+
enabled by default starting from mypy 2.0. Example:
605605

606606
.. code-block:: python
607607
@@ -644,7 +644,7 @@ of the above sections.
644644
reveal_type(values) # Revealed type is list[float]
645645
646646
Note: We are planning to turn this flag on by default in a future mypy
647-
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
647+
release.
648648

649649
.. option:: --allow-redefinition
650650

@@ -684,30 +684,26 @@ of the above sections.
684684
items = "100" # valid, items now has type str
685685
items = int(items) # valid, items now has type int
686686
687-
.. option:: --local-partial-types
687+
.. option:: --no-local-partial-types
688688

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

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

697-
a = None # Need type annotation here if using --local-partial-types
698-
b: int | None = None
696+
.. code-block:: python
699697
700-
class Foo:
701-
bar = None # Need type annotation here if using --local-partial-types
702-
baz: int | None = None
698+
a = [] # Need type annotation unless using --no-local-partial-types
703699
704-
def __init__(self) -> None:
705-
self.bar = 1
700+
def func() -> None:
701+
a.append(1)
706702
707-
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
703+
reveal_type(a) # "list[int]" if using --no-local-partial-types
708704
709-
Note: this option is always implicitly enabled in mypy daemon and will become
710-
enabled by default in mypy v2.0 release.
705+
Local partial types are enabled by default starting from mypy 2.0. The
706+
mypy daemon requires local partial types.
711707

712708
.. option:: --no-implicit-reexport
713709

docs/source/config_file.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,9 @@ section of the command line docs.
720720

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

727727
.. code-block:: python
728728
@@ -761,7 +761,7 @@ section of the command line docs.
761761
reveal_type(values) # Revealed type is list[float]
762762
763763
Note: We are planning to turn this flag on by default in a future mypy
764-
release, along with :confval:`local_partial_types`.
764+
release.
765765

766766
.. confval:: allow_redefinition_old
767767

@@ -800,11 +800,11 @@ section of the command line docs.
800800
.. confval:: local_partial_types
801801

802802
:type: boolean
803-
:default: False
803+
:default: True
804804

805-
Disallows inferring variable type for ``None`` from two assignments in different scopes.
806-
This is always implicitly enabled when using the :ref:`mypy daemon <mypy_daemon>`.
807-
This will be enabled by default in mypy v2.0 release.
805+
This prevents inferring a variable type from an empty container (such as a list or
806+
a dictionary) created at module top level or class body and updated in
807+
a function. This must be enabled when using the :ref:`mypy daemon <mypy_daemon>`.
808808

809809
.. confval:: disable_error_code
810810

docs/source/mypy_daemon.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ you have a large codebase.)
6161

6262
.. note::
6363

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

6667

6768
Daemon client commands

0 commit comments

Comments
 (0)