Skip to content

Commit 992c5b8

Browse files
Merge branch 'master' into patch-10
2 parents ca2f7d4 + c3d9b5c commit 992c5b8

738 files changed

Lines changed: 30461 additions & 11723 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
with:
4545
python-version: '3.12'
4646
- name: Install tox
47-
run: pip install tox==4.26.0
47+
run: pip install tox==4.53.1
4848
- name: Setup tox environment
4949
run: tox run -e ${{ env.TOXENV }} --notest
5050
- name: Test

.github/workflows/test.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ jobs:
5959
toxenv: py
6060
tox_extra_args: "-n 4"
6161
test_mypyc: true
62+
- name: Test suite with py315-ubuntu, mypyc-compiled
63+
python: '3.15'
64+
os: ubuntu-24.04-arm
65+
toxenv: py
66+
tox_extra_args: "-n 4"
67+
test_mypyc: true
6268
- name: Test suite with py314t-ubuntu, mypyc-compiled
6369
python: '3.14t'
6470
os: ubuntu-24.04-arm
@@ -139,6 +145,7 @@ jobs:
139145
timeout-minutes: 60
140146
env:
141147
TOX_SKIP_MISSING_INTERPRETERS: False
148+
VIRTUALENV_SYSTEM_SITE_PACKAGES: ${{ matrix.test_mypyc && 1 || 0 }}
142149
# Rich (pip) -- Disable color for windows + pytest
143150
FORCE_COLOR: ${{ !(startsWith(matrix.os, 'windows-') && startsWith(matrix.toxenv, 'py')) && 1 || 0 }}
144151
# Tox
@@ -195,6 +202,7 @@ jobs:
195202
if: ${{ !(matrix.debug_build || endsWith(matrix.python, '-dev')) }}
196203
with:
197204
python-version: ${{ matrix.python }}
205+
allow-prereleases: true
198206

199207
- name: Install tox
200208
run: |
@@ -205,12 +213,14 @@ jobs:
205213
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
206214
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
207215
echo os.sched_getaffinity; python -c 'import os; print(len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
208-
pip install tox==4.26.0
216+
pip install tox==4.53.1
209217
210218
- name: Compiled with mypyc
211219
if: ${{ matrix.test_mypyc }}
220+
# Use local version of librt during self-compilation in tests.
212221
run: |
213222
pip install -r test-requirements.txt
223+
pip install -U mypyc/lib-rt
214224
CC=clang MYPYC_OPT_LEVEL=0 MYPY_USE_MYPYC=1 pip install -e .
215225
216226
- name: Setup tox environment
@@ -268,7 +278,7 @@ jobs:
268278
default: 3.11.1
269279
command: python -c "import platform; print(f'{platform.architecture()=} {platform.machine()=}');"
270280
- name: Install tox
271-
run: pip install tox==4.26.0
281+
run: pip install tox==4.53.1
272282
- name: Setup tox environment
273283
run: tox run -e py --notest
274284
- name: Test

CHANGELOG.md

Lines changed: 353 additions & 0 deletions
Large diffs are not rendered by default.

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ tox -e dev --override testenv:dev.allowlist_externals+=env -- env # inspect the
123123
If you don't already have `tox` installed, you can use a virtual environment as
124124
described above to install `tox` via `pip` (e.g., ``python -m pip install tox``).
125125

126+
## LLM-assisted contributions
127+
128+
In general, mypy takes a neutral stance on using various LLM code assistants to
129+
make contributions. LLMs are just another tool, and contributors bear full
130+
responsibility for the code they are submitting. Disclosing the use of LLMs in
131+
pull request description is recommended, but not required.
132+
133+
However, we discourage use of LLMs by *new* contributors. We are interested in
134+
growing long-term contributors who have good understanding of mypy code.
135+
Pull requests from new contributors that are mostly generated by LLMs
136+
with little human input will be closed.
137+
126138
## First time contributors
127139

128140
If you're looking for things to help with, browse our [issue tracker](https://github.com/python/mypy/issues)!

docs/source/builtin_types.rst

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ See :ref:`dynamic-typing` for more details.
4040
Generic types
4141
.............
4242

43-
In Python 3.9 and later, built-in collection type objects support
44-
indexing:
43+
Built-in collection type objects support indexing:
4544

4645
====================== ===============================
4746
Type Description
@@ -65,13 +64,11 @@ strings and ``dict[Any, Any]`` is a dictionary of dynamically typed
6564
Python protocols. For example, a ``str`` object or a ``list[str]`` object is
6665
valid when ``Iterable[str]`` or ``Sequence[str]`` is expected.
6766
You can import them from :py:mod:`collections.abc` instead of importing from
68-
:py:mod:`typing` in Python 3.9.
67+
:py:mod:`typing`.
6968

70-
See :ref:`generic-builtins` for more details, including how you can
71-
use these in annotations also in Python 3.7 and 3.8.
69+
See :ref:`generic-builtins` for more details.
7270

73-
These legacy types defined in :py:mod:`typing` are needed if you need to support
74-
Python 3.8 and earlier:
71+
These legacy types defined in :py:mod:`typing` are also supported:
7572

7673
====================== ===============================
7774
Type Description
@@ -80,17 +77,5 @@ Type Description
8077
``Tuple[int, int]`` tuple of two ``int`` objects (``Tuple[()]`` is the empty tuple)
8178
``Tuple[int, ...]`` tuple of an arbitrary number of ``int`` objects
8279
``Dict[str, int]`` dictionary from ``str`` keys to ``int`` values
83-
``Iterable[int]`` iterable object containing ints
84-
``Sequence[bool]`` sequence of booleans (read-only)
85-
``Mapping[str, int]`` mapping from ``str`` keys to ``int`` values (read-only)
8680
``Type[C]`` type object of ``C`` (``C`` is a class/type variable/union of types)
8781
====================== ===============================
88-
89-
``List`` is an alias for the built-in type ``list`` that supports
90-
indexing (and similarly for ``dict``/``Dict`` and
91-
``tuple``/``Tuple``).
92-
93-
Note that even though ``Iterable``, ``Sequence`` and ``Mapping`` look
94-
similar to abstract base classes defined in :py:mod:`collections.abc`
95-
(formerly ``collections``), they are not identical, since the latter
96-
don't support indexing prior to Python 3.9.

docs/source/cheat_sheet_py3.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ Useful built-in types
4343
x: str = "test"
4444
x: bytes = b"test"
4545
46-
# For collections on Python 3.9+, the type of the collection item is in brackets
46+
# For collections, the type of the collection item is in brackets
4747
x: list[int] = [1]
4848
x: set[int] = {6, 7}
4949
5050
# For mappings, we need the types of both keys and values
51-
x: dict[str, float] = {"field": 2.0} # Python 3.9+
51+
x: dict[str, float] = {"field": 2.0}
5252
5353
# For tuples of fixed size, we specify the types of all the elements
54-
x: tuple[int, str, float] = (3, "yes", 7.5) # Python 3.9+
54+
x: tuple[int, str, float] = (3, "yes", 7.5)
5555
5656
# For tuples of variable size, we use one type and ellipsis
57-
x: tuple[int, ...] = (1, 2, 3) # Python 3.9+
57+
x: tuple[int, ...] = (1, 2, 3)
5858
5959
# On Python 3.8 and earlier, the name of the collection type is
6060
# capitalized, and the type is imported from the 'typing' module
@@ -67,13 +67,12 @@ Useful built-in types
6767
6868
from typing import Union, Optional
6969
70-
# On Python 3.10+, use the | operator when something could be one of a few types
71-
x: list[int | str] = [3, 5, "test", "fun"] # Python 3.10+
72-
# On earlier versions, use Union
70+
# Use the | operator when something could be one of a few types
71+
x: list[int | str] = [3, 5, "test", "fun"]
72+
# Union is equivalent
7373
x: list[Union[int, str]] = [3, 5, "test", "fun"]
7474
75-
# Use X | None for a value that could be None on Python 3.10+
76-
# Use Optional[X] on 3.9 and earlier; Optional[X] is the same as 'X | None'
75+
# Use X | None for a value that could be None; Optional[X] is the same as X | None
7776
x: str | None = "something" if some_condition() else None
7877
if x is not None:
7978
# Mypy understands x won't be None here because of the if-statement

docs/source/command_line.rst

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,13 @@ of the above sections.
595595
This flag causes mypy to suppress errors caused by not being able to fully
596596
infer the types of global and class variables.
597597

598-
.. option:: --allow-redefinition-new
598+
.. option:: --allow-redefinition
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
@@ -613,7 +613,7 @@ of the above sections.
613613
# Type of "x" is "int | str" here.
614614
return x
615615
616-
Without the new flag, mypy only supports inferring optional types
616+
Without this flag, mypy only supports inferring optional types
617617
(``X | None``) from multiple assignments. With this option enabled,
618618
mypy can infer arbitrary union types.
619619

@@ -644,26 +644,23 @@ 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

649-
.. option:: --allow-redefinition
649+
.. option:: --allow-redefinition-new
650650

651-
This is an alias to :option:`--allow-redefinition-old <mypy --allow-redefinition-old>`.
652-
In mypy v2.0 this will point to
653-
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`, and will
654-
eventually became the default.
651+
Deprecated alias for :option:`--allow-redefinition <mypy --allow-redefinition>`.
655652

656653
.. option:: --allow-redefinition-old
657654

658-
This is an older variant of
659-
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
655+
This is an older, more limited variant of
656+
:option:`--allow-redefinition <mypy --allow-redefinition>`.
660657
This flag enables redefinition of a variable with an
661658
arbitrary type *in some contexts*: only redefinitions within the
662659
same block and nesting depth as the original definition are allowed.
663660

664-
We have no plans to remove this flag, but we expect that
665-
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`
666-
will replace this flag for new use cases eventually.
661+
We have no plans to remove this flag, but
662+
:option:`--allow-redefinition <mypy --allow-redefinition>`
663+
is recommended for new use cases.
667664

668665
Example where this can be useful:
669666

@@ -684,30 +681,26 @@ of the above sections.
684681
items = "100" # valid, items now has type str
685682
items = int(items) # valid, items now has type int
686683
687-
.. option:: --local-partial-types
684+
.. option:: --no-local-partial-types
688685

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:
686+
Disable local partial types to enable legacy type inference mode for
687+
containers.
694688

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

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

712705
.. option:: --no-implicit-reexport
713706

@@ -764,11 +757,11 @@ of the above sections.
764757
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
765758
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.
766759

767-
.. option:: --strict-bytes
760+
.. option:: --no-strict-bytes
768761

769-
By default, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes`` which
770-
is not true at runtime. Use this flag to disable this behavior. ``--strict-bytes`` will
771-
be enabled by default in *mypy 2.0*.
762+
Treat ``bytearray`` and ``memoryview`` as subtypes of ``bytes``. This is not true
763+
at runtime and can lead to unexpected behavior. This was the default behavior prior
764+
to mypy 2.0.
772765

773766
.. code-block:: python
774767
@@ -777,10 +770,12 @@ of the above sections.
777770
with open("binary_file", "wb") as fp:
778771
fp.write(buf)
779772
780-
f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
781-
f(memoryview(b"")) # error: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
773+
# Using --no-strict-bytes disables the following errors
774+
f(bytearray(b"")) # Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
775+
f(memoryview(b"")) # Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
782776
783-
# If `f` accepts any object that implements the buffer protocol, consider using:
777+
# If `f` accepts any object that implements the buffer protocol,
778+
# consider using Buffer instead:
784779
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier
785780
786781
def f(buf: Buffer) -> None:
@@ -1037,6 +1032,43 @@ beyond what incremental mode can offer, try running mypy in
10371032
Skip cache internal consistency checks based on mtime.
10381033

10391034

1035+
.. _parallel:
1036+
1037+
Parallel type-checking
1038+
**********************
1039+
1040+
By default, mypy checks all modules in the same Python process. This can be slow
1041+
for large code bases. Mypy offers experimental parallel type-checking mode using
1042+
multiple worker processes. In parallel mode, modules that do not depend om each
1043+
other are type-checked in parallel. :ref:`Incremental cache <incremental>` is
1044+
used to manage most of the shared state. Parallel type-checking also requires
1045+
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is
1046+
enabled by default starting from mypy 2.0.
1047+
1048+
.. option:: -n NUMBER, --num-workers NUMBER
1049+
1050+
Use ``NUMBER`` parallel worker processes (in addition to the coordinator
1051+
process) to perform type-checking. Specifying ``--num-workers 0`` (default)
1052+
disables parallel checking. Automatic detection of the optimal number
1053+
of workers is not supported yet.
1054+
1055+
This setting will override the ``MYPY_NUM_WORKERS`` environment
1056+
variable if it is set.
1057+
1058+
Notes:
1059+
1060+
* An import cycle is always processed as a whole by a worker process. Thus,
1061+
avoiding large import cycles in your code will *significantly* improve
1062+
type-checking speed.
1063+
1064+
* Specifying a number of workers that is larger than the number of *physical*
1065+
CPU cores is not beneficial, since mypy is usually CPU bound. Best way to
1066+
tune the number of workers on a given machine is to start from 3-4 workers
1067+
and increase the number while you see a performance improvement.
1068+
1069+
* Parallel mode requires and automatically enables :option:`--native-parser`.
1070+
1071+
10401072
Advanced options
10411073
****************
10421074

@@ -1110,6 +1142,11 @@ in developing or debugging mypy internals.
11101142
cause mypy to type check the contents of ``temp.py`` instead of ``original.py``,
11111143
but error messages will still reference ``original.py``.
11121144

1145+
.. option:: --native-parser
1146+
1147+
This enables fast Rust-based parser that parses directly to mypy AST.
1148+
It will become the default parser in one of the next mypy releases.
1149+
11131150

11141151
Report generation
11151152
*****************
@@ -1174,7 +1211,7 @@ format into the specified directory.
11741211
Enabling incomplete/experimental features
11751212
*****************************************
11761213

1177-
.. option:: --enable-incomplete-feature {PreciseTupleTypes,InlineTypedDict,TypeForm}
1214+
.. option:: --enable-incomplete-feature {PreciseTupleTypes,InlineTypedDict}
11781215

11791216
Some features may require several mypy releases to implement, for example
11801217
due to their complexity, potential for backwards incompatibility, or
@@ -1229,8 +1266,6 @@ List of currently incomplete/experimental features:
12291266
def test_values() -> {"width": int, "description": str}:
12301267
return {"width": 42, "description": "test"}
12311268
1232-
* ``TypeForm``: this feature enables ``TypeForm``, as described in
1233-
`PEP 747 – Annotating Type Forms <https://peps.python.org/pep-0747/>_`.
12341269
12351270
12361271
Miscellaneous

docs/source/common_issues.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ improved performance. You can ensure the presence of orjson using the ``faster-c
232232

233233
python3 -m pip install -U mypy[faster-cache]
234234

235-
Mypy may depend on orjson by default in the future. To use faster, native parser, use the
236-
``native-parse`` extra. Native parser will be default in near future.
235+
Mypy may depend on orjson by default in the future.
237236

238237
Types of empty collections
239238
--------------------------
@@ -671,7 +670,7 @@ subtly different, and it's important to understand how they differ to avoid pitf
671670

672671
.. code-block:: python
673672
674-
from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier
673+
from typing import TypeAlias
675674
676675
class A: ...
677676
Alias: TypeAlias = A

0 commit comments

Comments
 (0)