Skip to content

Commit 6f2b0cf

Browse files
authored
Merge branch 'master' into default-strict-bytes
2 parents 2587d64 + e5cd1f9 commit 6f2b0cf

File tree

373 files changed

+28331
-3530
lines changed

Some content is hidden

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

373 files changed

+28331
-3530
lines changed

.github/workflows/test.yml

Lines changed: 7 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'
@@ -200,7 +205,7 @@ jobs:
200205
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
201206
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
202207
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
208+
pip install tox==4.26.0
204209
205210
- name: Compiled with mypyc
206211
if: ${{ matrix.test_mypyc }}
@@ -263,7 +268,7 @@ jobs:
263268
default: 3.11.1
264269
command: python -c "import platform; print(f'{platform.architecture()=} {platform.machine()=}');"
265270
- name: Install tox
266-
run: pip install setuptools==75.1.0 tox==4.26.0
271+
run: pip install tox==4.26.0
267272
- name: Setup tox environment
268273
run: tox run -e py --notest
269274
- 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: 547 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/command_line.rst

Lines changed: 26 additions & 7 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,8 +598,8 @@ 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
601+
unrelated type. This flag enables the redefinition of *unannotated*
602+
variables with an arbitrary type. You will also need to enable
603603
:option:`--local-partial-types <mypy --local-partial-types>`.
604604
Example:
605605

@@ -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
635647
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
636-
The feature is still experimental, and the semantics may still change.
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
@@ -689,7 +707,7 @@ of the above sections.
689707
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
690708
691709
Note: this option is always implicitly enabled in mypy daemon and will become
692-
enabled by default for mypy in a future release.
710+
enabled by default in mypy v2.0 release.
693711

694712
.. option:: --no-implicit-reexport
695713

@@ -1000,9 +1018,10 @@ beyond what incremental mode can offer, try running mypy in
10001018
writing to the cache, use ``--cache-dir=/dev/null`` (UNIX)
10011019
or ``--cache-dir=nul`` (Windows).
10021020

1003-
.. option:: --sqlite-cache
1021+
.. option:: --no-sqlite-cache
10041022

1005-
Use an `SQLite`_ database to store the cache.
1023+
Avoid using `SQLite`_ database to store the cache, instead write cache data
1024+
out to individual files.
10061025

10071026
.. option:: --cache-fine-grained
10081027

docs/source/common_issues.rst

Lines changed: 18 additions & 5 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

docs/source/config_file.rst

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,8 @@ section of the command line docs.
719719
:default: False
720720

721721
By default, mypy won't allow a variable to be redefined with an
722-
unrelated type. This *experimental* flag enables the redefinition of
723-
unannotated variables with an arbitrary type. You will also need to enable
722+
unrelated type. This flag enables the redefinition of unannotated
723+
variables with an arbitrary type. You will also need to enable
724724
:confval:`local_partial_types`.
725725
Example:
726726

@@ -748,10 +748,22 @@ section of the command line docs.
748748
# Type of "x" is "str" here.
749749
...
750750
751+
Function arguments are special, changing their type within function body
752+
is allowed even if they are annotated, but that annotation is used to infer
753+
types of r.h.s. of all subsequent assignments. Such middle-ground semantics
754+
provides good balance for majority of common use cases. For example:
755+
756+
.. code-block:: python
757+
758+
def process(values: list[float]) -> None:
759+
if not values:
760+
values = [0, 0, 0]
761+
reveal_type(values) # Revealed type is list[float]
762+
751763
Note: We are planning to turn this flag on by default in a future mypy
752764
release, along with :confval:`local_partial_types`.
753765

754-
.. confval:: allow_redefinition
766+
.. confval:: allow_redefinition_old
755767

756768
:type: boolean
757769
:default: False
@@ -777,14 +789,22 @@ section of the command line docs.
777789
items = "100" # valid, items now has type str
778790
items = int(items) # valid, items now has type int
779791
792+
.. confval:: allow_redefinition
793+
794+
:type: boolean
795+
:default: False
796+
797+
An alias to :confval:`allow_redefinition_old`, in mypy v2.0 this will point to
798+
:confval:`allow_redefinition_new`, and will eventually became the default.
799+
780800
.. confval:: local_partial_types
781801

782802
:type: boolean
783803
:default: False
784804

785805
Disallows inferring variable type for ``None`` from two assignments in different scopes.
786806
This is always implicitly enabled when using the :ref:`mypy daemon <mypy_daemon>`.
787-
This will be enabled by default in a future mypy release.
807+
This will be enabled by default in mypy v2.0 release.
788808

789809
.. confval:: disable_error_code
790810

@@ -959,7 +979,7 @@ These options may only be set in the global section (``[mypy]``).
959979
.. confval:: sqlite_cache
960980

961981
:type: boolean
962-
:default: False
982+
:default: True
963983

964984
Use an `SQLite`_ database to store the cache.
965985

docs/source/generics.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,11 @@ contravariant, use type variables defined with special keyword arguments
693693
my_box = Box(Square())
694694
look_into(my_box) # OK, but mypy would complain here for an invariant type
695695
696+
.. _value-constrained-type-variables:
696697
.. _type-variable-value-restriction:
697698

698-
Type variables with value restriction
699-
*************************************
699+
Value-constrained type variables
700+
*********************************
700701

701702
By default, a type variable can be replaced with any type -- or any type that
702703
is a subtype of the upper bound, which defaults to ``object``. However, sometimes
@@ -727,8 +728,9 @@ The same thing is also possibly using the legacy syntax (Python 3.11 or earlier)
727728
def concat(x: AnyStr, y: AnyStr) -> AnyStr:
728729
return x + y
729730
730-
No matter which syntax you use, such a type variable is called a type variable
731-
with a value restriction. Importantly, this is different from a union type,
731+
No matter which syntax you use, such a type variable is called a
732+
value-constrained type variable (the allowed types are accessible at runtime
733+
via ``TypeVar.__constraints__``). Importantly, this is different from a union type,
732734
since combinations of ``str`` and ``bytes`` are not accepted:
733735

734736
.. code-block:: python
@@ -760,7 +762,7 @@ for the type variable, which in this case is ``str``.
760762

761763
This is thus subtly different from using ``str | bytes`` as an upper bound,
762764
where the return type would be ``S`` (see :ref:`type-variable-upper-bound`).
763-
Using a value restriction is correct for ``concat``, since ``concat``
765+
Using a value constraint is correct for ``concat``, since ``concat``
764766
actually returns a ``str`` instance in the above example:
765767

766768
.. code-block:: python
@@ -775,7 +777,7 @@ value of :py:func:`re.compile`, where ``S`` can be either ``str``
775777
or ``bytes``. Regular expressions can be based on a string or a
776778
bytes pattern.
777779

778-
A type variable may not have both a value restriction and an upper bound.
780+
A type variable may not have both value constraints and an upper bound.
779781

780782
Note that you may come across :py:data:`~typing.AnyStr` imported from
781783
:py:mod:`typing`. This feature is now deprecated, but it means the same
@@ -1330,7 +1332,7 @@ Here are examples using the legacy syntax (Python 3.11 and earlier):
13301332
for i, j in NewVec[int]():
13311333
...
13321334
1333-
Using type variable bounds or value restriction in generic aliases has
1335+
Using type variable bounds or value constraints in generic aliases has
13341336
the same effect as in generic classes and functions.
13351337

13361338

docs/source/more_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ Here is the same example using the legacy syntax (Python 3.11 and earlier):
303303
.. note::
304304

305305
If you just need to constrain a type variable to certain types or
306-
subtypes, you can use a :ref:`value restriction
307-
<type-variable-value-restriction>`.
306+
subtypes, you can use a :ref:`value-constrained type variable
307+
<value-constrained-type-variables>`.
308308

309309
The default values of a function's arguments don't affect its signature -- only
310310
the absence or presence of a default value does. So in order to reduce

docs/source/stubs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ For example:
126126
"""Some docstring"""
127127
pass
128128
129-
# Error: Incompatible default for argument "foo" (default has
130-
# type "ellipsis", argument has type "list[str]")
129+
# Error: Incompatible default for parameter "foo" (default has
130+
# type "ellipsis", parameter has type "list[str]")
131131
def not_ok(self, foo: list[str] = ...) -> None:
132132
print(foo)

0 commit comments

Comments
 (0)