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

Skip to content

Commit e325234

Browse files
authored
Merge branch 'master' into callback-method
2 parents b1b4a94 + f4db3cd commit e325234

421 files changed

Lines changed: 24556 additions & 5178 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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ jobs:
112112
toxenv: py
113113
tox_extra_args: "-n 4 --mypy-num-workers=4 mypy/test/testcheck.py"
114114
test_mypyc: true
115+
- name: Parallel tests with py314-windows-64, interpreted
116+
python: '3.14'
117+
os: windows-latest
118+
toxenv: py
119+
tox_extra_args: "-n 2 --mypy-num-workers=2 mypy/test/testcheck.py -k 'incremental or modules or classes'"
115120

116121
- name: Type check our own code (py310-ubuntu)
117122
python: '3.10'
@@ -134,6 +139,7 @@ jobs:
134139
timeout-minutes: 60
135140
env:
136141
TOX_SKIP_MISSING_INTERPRETERS: False
142+
VIRTUALENV_SYSTEM_SITE_PACKAGES: ${{ matrix.test_mypyc && 1 || 0 }}
137143
# Rich (pip) -- Disable color for windows + pytest
138144
FORCE_COLOR: ${{ !(startsWith(matrix.os, 'windows-') && startsWith(matrix.toxenv, 'py')) && 1 || 0 }}
139145
# Tox
@@ -200,12 +206,14 @@ jobs:
200206
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
201207
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
202208
echo os.sched_getaffinity; python -c 'import os; print(len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
203-
pip install setuptools==75.1.0 tox==4.26.0
209+
pip install tox==4.26.0
204210
205211
- name: Compiled with mypyc
206212
if: ${{ matrix.test_mypyc }}
213+
# Use local version of librt during self-compilation in tests.
207214
run: |
208215
pip install -r test-requirements.txt
216+
pip install -U mypyc/lib-rt
209217
CC=clang MYPYC_OPT_LEVEL=0 MYPY_USE_MYPYC=1 pip install -e .
210218
211219
- name: Setup tox environment
@@ -263,7 +271,7 @@ jobs:
263271
default: 3.11.1
264272
command: python -c "import platform; print(f'{platform.architecture()=} {platform.machine()=}');"
265273
- name: Install tox
266-
run: pip install setuptools==75.1.0 tox==4.26.0
274+
run: pip install tox==4.26.0
267275
- name: Setup tox environment
268276
run: tox run -e py --notest
269277
- 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: 600 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: 50 additions & 33 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

@@ -598,10 +598,10 @@ of the above sections.
598598
.. option:: --allow-redefinition-new
599599

600600
By default, mypy won't allow a variable to be redefined with an
601-
unrelated type. This *experimental* flag enables the redefinition of
602-
unannotated 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
@@ -631,12 +631,30 @@ of the above sections.
631631
# Type of "x" is "str" here.
632632
...
633633
634+
Function arguments are special, changing their type within function body
635+
is allowed even if they are annotated, but that annotation is used to infer
636+
types of r.h.s. of all subsequent assignments. Such middle-ground semantics
637+
provides good balance for majority of common use cases. For example:
638+
639+
.. code-block:: python
640+
641+
def process(values: list[float]) -> None:
642+
if not values:
643+
values = [0, 0, 0]
644+
reveal_type(values) # Revealed type is list[float]
645+
634646
Note: We are planning to turn this flag on by default in a future mypy
635-
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
636-
The feature is still experimental, and the semantics may still change.
647+
release.
637648

638649
.. option:: --allow-redefinition
639650

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.
655+
656+
.. option:: --allow-redefinition-old
657+
640658
This is an older variant of
641659
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
642660
This flag enables redefinition of a variable with an
@@ -666,30 +684,26 @@ of the above sections.
666684
items = "100" # valid, items now has type str
667685
items = int(items) # valid, items now has type int
668686
669-
.. option:: --local-partial-types
687+
.. option:: --no-local-partial-types
670688

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

677-
.. 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:
678695

679-
a = None # Need type annotation here if using --local-partial-types
680-
b: int | None = None
696+
.. code-block:: python
681697
682-
class Foo:
683-
bar = None # Need type annotation here if using --local-partial-types
684-
baz: int | None = None
698+
a = [] # Need type annotation unless using --no-local-partial-types
685699
686-
def __init__(self) -> None:
687-
self.bar = 1
700+
def func() -> None:
701+
a.append(1)
688702
689-
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
703+
reveal_type(a) # "list[int]" if using --no-local-partial-types
690704
691-
Note: this option is always implicitly enabled in mypy daemon and will become
692-
enabled by default for mypy in a future release.
705+
Local partial types are enabled by default starting from mypy 2.0. The
706+
mypy daemon requires local partial types.
693707

694708
.. option:: --no-implicit-reexport
695709

@@ -746,11 +760,11 @@ of the above sections.
746760
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
747761
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.
748762

749-
.. option:: --strict-bytes
763+
.. option:: --no-strict-bytes
750764

751-
By default, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes`` which
752-
is not true at runtime. Use this flag to disable this behavior. ``--strict-bytes`` will
753-
be enabled by default in *mypy 2.0*.
765+
Treat ``bytearray`` and ``memoryview`` as subtypes of ``bytes``. This is not true
766+
at runtime and can lead to unexpected behavior. This was the default behavior prior
767+
to mypy 2.0.
754768

755769
.. code-block:: python
756770
@@ -759,10 +773,12 @@ of the above sections.
759773
with open("binary_file", "wb") as fp:
760774
fp.write(buf)
761775
762-
f(bytearray(b"")) # error: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
763-
f(memoryview(b"")) # error: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
776+
# Using --no-strict-bytes disables the following errors
777+
f(bytearray(b"")) # Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
778+
f(memoryview(b"")) # Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
764779
765-
# If `f` accepts any object that implements the buffer protocol, consider using:
780+
# If `f` accepts any object that implements the buffer protocol,
781+
# consider using Buffer instead:
766782
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier
767783
768784
def f(buf: Buffer) -> None:
@@ -1000,9 +1016,10 @@ beyond what incremental mode can offer, try running mypy in
10001016
writing to the cache, use ``--cache-dir=/dev/null`` (UNIX)
10011017
or ``--cache-dir=nul`` (Windows).
10021018

1003-
.. option:: --sqlite-cache
1019+
.. option:: --no-sqlite-cache
10041020

1005-
Use an `SQLite`_ database to store the cache.
1021+
Avoid using `SQLite`_ database to store the cache, instead write cache data
1022+
out to individual files.
10061023

10071024
.. option:: --cache-fine-grained
10081025

docs/source/common_issues.rst

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ error:
148148
The second line is now fine, since the ignore comment causes the name
149149
``frobnicate`` to get an implicit ``Any`` type.
150150

151+
The type ignore comment must be at the start of the comments on a line.
152+
This type ignore will not take effect:
153+
154+
.. code-block:: python
155+
156+
import frobnicate #example other comment # type: ignore
157+
frobnicate.start()
158+
151159
.. note::
152160

153161
You can use the form ``# type: ignore[<code>]`` to only ignore
@@ -220,11 +228,12 @@ make cold mypy runs several times faster.
220228

221229
Furthermore: as of `mypy 1.13 <https://mypy-lang.blogspot.com/2024/10/mypy-113-released.html>`_,
222230
mypy allows use of the orjson library for handling the cache instead of the stdlib json, for
223-
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:
224232

225233
python3 -m pip install -U mypy[faster-cache]
226234

227-
Mypy may depend on orjson by default in the future.
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.
228237

229238
Types of empty collections
230239
--------------------------
@@ -294,9 +303,13 @@ See :ref:`type-narrowing` for more information.
294303
Invariance vs covariance
295304
------------------------
296305

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

302315
.. code-block:: python
@@ -658,7 +671,7 @@ subtly different, and it's important to understand how they differ to avoid pitf
658671

659672
.. code-block:: python
660673
661-
from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier
674+
from typing import TypeAlias
662675
663676
class A: ...
664677
Alias: TypeAlias = A

0 commit comments

Comments
 (0)