Skip to content

Commit b38ac0f

Browse files
Merge branch 'master' into fix_variance_polymorphic_constructor
2 parents d2d8491 + 680c1ac commit b38ac0f

317 files changed

Lines changed: 13695 additions & 3930 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/test.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,17 @@ jobs:
106106
os: ubuntu-24.04-arm
107107
toxenv: py
108108
tox_extra_args: "-n 4 --mypy-num-workers=4 mypy/test/testcheck.py"
109-
dev_ast_serialize: true
110109
- name: Parallel tests with py314-ubuntu, mypyc-compiled
111110
python: '3.14'
112111
os: ubuntu-24.04-arm
113112
toxenv: py
114113
tox_extra_args: "-n 4 --mypy-num-workers=4 mypy/test/testcheck.py"
115114
test_mypyc: true
116-
dev_ast_serialize: true
117115
- name: Parallel tests with py314-windows-64, interpreted
118116
python: '3.14'
119117
os: windows-latest
120118
toxenv: py
121119
tox_extra_args: "-n 2 --mypy-num-workers=2 mypy/test/testcheck.py -k 'incremental or modules or classes'"
122-
dev_ast_serialize: true
123120

124121
- name: Type check our own code (py310-ubuntu)
125122
python: '3.10'
@@ -142,7 +139,7 @@ jobs:
142139
timeout-minutes: 60
143140
env:
144141
TOX_SKIP_MISSING_INTERPRETERS: False
145-
VIRTUALENV_SYSTEM_SITE_PACKAGES: ${{ matrix.dev_ast_serialize && 1 || 0 }}
142+
VIRTUALENV_SYSTEM_SITE_PACKAGES: ${{ matrix.test_mypyc && 1 || 0 }}
146143
# Rich (pip) -- Disable color for windows + pytest
147144
FORCE_COLOR: ${{ !(startsWith(matrix.os, 'windows-') && startsWith(matrix.toxenv, 'py')) && 1 || 0 }}
148145
# Tox
@@ -209,19 +206,16 @@ jobs:
209206
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
210207
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
211208
echo os.sched_getaffinity; python -c 'import os; print(len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
212-
pip install setuptools==75.1.0 tox==4.26.0
209+
pip install tox==4.26.0
213210
214211
- name: Compiled with mypyc
215212
if: ${{ matrix.test_mypyc }}
213+
# Use local version of librt during self-compilation in tests.
216214
run: |
217215
pip install -r test-requirements.txt
216+
pip install -U mypyc/lib-rt
218217
CC=clang MYPYC_OPT_LEVEL=0 MYPY_USE_MYPYC=1 pip install -e .
219218
220-
# To speed-up process until ast_serialize is on PyPI.
221-
- name: Install pinned ast-serialize
222-
if: ${{ matrix.dev_ast_serialize }}
223-
run: pip install ast-serialize@git+https://github.com/mypyc/ast_serialize.git@da5a16cf268dbec63ed6b2e6b715470576e2d1a6
224-
225219
- name: Setup tox environment
226220
run: |
227221
tox run -e ${{ matrix.toxenv }} --notest
@@ -277,7 +271,7 @@ jobs:
277271
default: 3.11.1
278272
command: python -c "import platform; print(f'{platform.architecture()=} {platform.machine()=}');"
279273
- name: Install tox
280-
run: pip install setuptools==75.1.0 tox==4.26.0
274+
run: pip install tox==4.26.0
281275
- name: Setup tox environment
282276
run: tox run -e py --notest
283277
- name: Test

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
hooks:
2727
- id: codespell
2828
args:
29-
- --ignore-words-list=HAX,ccompiler,ot,statics,whet,zar
29+
- --ignore-words-list=HAX,Nam,ccompiler,ot,statics,whet,zar
3030
exclude: ^(mypy/test/|mypy/typeshed/|mypyc/test-data/|test-data/).+$
3131
- repo: https://github.com/rhysd/actionlint
3232
rev: v1.7.7

CHANGELOG.md

Lines changed: 612 additions & 2 deletions
Large diffs are not rendered by default.

MANIFEST.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
prune mypy/typeshed
66
include mypy/typeshed/LICENSE
77
include mypy/typeshed/stdlib/VERSIONS
8+
include mypy/typeshed/stdlib/_typeshed/README.md
89
recursive-include mypy/typeshed *.pyi
910

1011
# mypy and mypyc
@@ -38,14 +39,16 @@ include test-requirements.in
3839
include test-requirements.txt
3940
include mypy_self_check.ini
4041
prune misc
42+
include misc/diff-cache.py
43+
include misc/apply-cache-diff.py
4144
graft test-data
4245
graft mypy/test
4346
include conftest.py
4447
include runtests.py
4548
include tox.ini
4649

47-
include LICENSE mypyc/README.md CHANGELOG.md
48-
exclude .gitmodules CONTRIBUTING.md CREDITS ROADMAP.md action.yml .editorconfig
50+
include LICENSE mypyc/README.md CHANGELOG.md CREDITS
51+
exclude .gitmodules CONTRIBUTING.md ROADMAP.md action.yml .editorconfig
4952
exclude .git-blame-ignore-revs .pre-commit-config.yaml
5053

5154
global-exclude *.py[cod]

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: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ potentially problematic or redundant in some way.
551551
.. note::
552552

553553
Mypy currently cannot detect and report unreachable or redundant code
554-
inside any functions using :ref:`type-variable-value-restriction`.
554+
inside any functions using :ref:`value-constrained type variables <value-constrained-type-variables>`.
555555

556556
This limitation will be removed in future releases of mypy.
557557

@@ -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
601-
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:
601+
unrelated type. This flag enables the redefinition of *unannotated*
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,27 +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>`.
648-
The feature is still experimental, and the semantics may still change.
647+
release.
649648

650-
.. option:: --allow-redefinition
649+
.. option:: --allow-redefinition-new
651650

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

657653
.. option:: --allow-redefinition-old
658654

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

665-
We have no plans to remove this flag, but we expect that
666-
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`
667-
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.
668664

669665
Example where this can be useful:
670666

@@ -685,30 +681,26 @@ of the above sections.
685681
items = "100" # valid, items now has type str
686682
items = int(items) # valid, items now has type int
687683
688-
.. option:: --local-partial-types
684+
.. option:: --no-local-partial-types
689685

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

696-
.. 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:
697692

698-
a = None # Need type annotation here if using --local-partial-types
699-
b: int | None = None
693+
.. code-block:: python
700694
701-
class Foo:
702-
bar = None # Need type annotation here if using --local-partial-types
703-
baz: int | None = None
695+
a = [] # Need type annotation unless using --no-local-partial-types
704696
705-
def __init__(self) -> None:
706-
self.bar = 1
697+
def func() -> None:
698+
a.append(1)
707699
708-
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
700+
reveal_type(a) # "list[int]" if using --no-local-partial-types
709701
710-
Note: this option is always implicitly enabled in mypy daemon and will become
711-
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.
712704

713705
.. option:: --no-implicit-reexport
714706

@@ -765,11 +757,11 @@ of the above sections.
765757
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
766758
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.
767759

768-
.. option:: --strict-bytes
760+
.. option:: --no-strict-bytes
769761

770-
By default, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes`` which
771-
is not true at runtime. Use this flag to disable this behavior. ``--strict-bytes`` will
772-
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.
773765

774766
.. code-block:: python
775767
@@ -778,10 +770,12 @@ of the above sections.
778770
with open("binary_file", "wb") as fp:
779771
fp.write(buf)
780772
781-
f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
782-
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"
783776
784-
# 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:
785779
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier
786780
787781
def f(buf: Buffer) -> None:
@@ -1019,9 +1013,10 @@ beyond what incremental mode can offer, try running mypy in
10191013
writing to the cache, use ``--cache-dir=/dev/null`` (UNIX)
10201014
or ``--cache-dir=nul`` (Windows).
10211015

1022-
.. option:: --sqlite-cache
1016+
.. option:: --no-sqlite-cache
10231017

1024-
Use an `SQLite`_ database to store the cache.
1018+
Avoid using `SQLite`_ database to store the cache, instead write cache data
1019+
out to individual files.
10251020

10261021
.. option:: --cache-fine-grained
10271022

docs/source/common_issues.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ make cold mypy runs several times faster.
228228

229229
Furthermore: as of `mypy 1.13 <https://mypy-lang.blogspot.com/2024/10/mypy-113-released.html>`_,
230230
mypy allows use of the orjson library for handling the cache instead of the stdlib json, for
231-
improved performance. You can ensure the presence of orjson using the faster-cache extra:
231+
improved performance. You can ensure the presence of orjson using the ``faster-cache`` extra:
232232

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

@@ -302,9 +302,13 @@ See :ref:`type-narrowing` for more information.
302302
Invariance vs covariance
303303
------------------------
304304

305-
Most mutable generic collections are invariant, and mypy considers all
306-
user-defined generic classes invariant by default
307-
(see :ref:`variance-of-generics` for motivation). This could lead to some
305+
Most mutable generic collections are invariant. When using the legacy
306+
``TypeVar`` syntax, mypy considers all user-defined generic classes invariant
307+
by default (see :ref:`variance-of-generics` for motivation). When using the
308+
:pep:`695` syntax (``class MyClass[T]: ...``), variance is inferred from
309+
usage rather than defaulting to invariant.
310+
311+
The fact that mutable sequences are usually invariant can lead to some
308312
unexpected errors when combined with type inference. For example:
309313

310314
.. code-block:: python
@@ -666,7 +670,7 @@ subtly different, and it's important to understand how they differ to avoid pitf
666670

667671
.. code-block:: python
668672
669-
from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier
673+
from typing import TypeAlias
670674
671675
class A: ...
672676
Alias: TypeAlias = A

0 commit comments

Comments
 (0)