Skip to content

Commit 8dfa9d6

Browse files
Merge branch 'master' into literals_as_anyof_types
2 parents 8d53181 + a7bdffd commit 8dfa9d6

219 files changed

Lines changed: 8650 additions & 1477 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ jobs:
205205
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
206206
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
207207
echo os.sched_getaffinity; python -c 'import os; print(len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
208-
pip install setuptools==75.1.0 tox==4.26.0
208+
pip install tox==4.26.0
209209
210210
- name: Compiled with mypyc
211211
if: ${{ matrix.test_mypyc }}
@@ -268,7 +268,7 @@ jobs:
268268
default: 3.11.1
269269
command: python -c "import platform; print(f'{platform.architecture()=} {platform.machine()=}');"
270270
- name: Install tox
271-
run: pip install setuptools==75.1.0 tox==4.26.0
271+
run: pip install tox==4.26.0
272272
- name: Setup tox environment
273273
run: tox run -e py --notest
274274
- 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: 17 additions & 5 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,7 +598,7 @@ 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 flag enables the redefinition of unannotated
601+
unrelated type. This flag enables the redefinition of *unannotated*
602602
variables with an arbitrary type. You will also need to enable
603603
:option:`--local-partial-types <mypy --local-partial-types>`.
604604
Example:
@@ -631,9 +631,20 @@ 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

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

1010-
.. option:: --sqlite-cache
1021+
.. option:: --no-sqlite-cache
10111022

1012-
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.
10131025

10141026
.. option:: --cache-fine-grained
10151027

docs/source/common_issues.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ 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

235-
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.
236237

237238
Types of empty collections
238239
--------------------------
@@ -302,9 +303,13 @@ See :ref:`type-narrowing` for more information.
302303
Invariance vs covariance
303304
------------------------
304305

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
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
308313
unexpected errors when combined with type inference. For example:
309314

310315
.. code-block:: python

docs/source/config_file.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ 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

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

969981
:type: boolean
970-
:default: False
982+
:default: True
971983

972984
Use an `SQLite`_ database to store the cache.
973985

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)