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

Skip to content

Commit a2503de

Browse files
authored
Merge branch 'master' into fix-20821-dunder-qualname
2 parents 448874a + f4db3cd commit a2503de

138 files changed

Lines changed: 2292 additions & 1379 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.

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@
22

33
## Next Release
44

5+
### Enabling `--local-partial-types` by default
6+
7+
This flag affects the inference of types based on assignments in other scopes.
8+
For now, explicitly disabling this continues to be supported, but this support will be removed
9+
in the future as the legacy behaviour is hard to support with other current and future features
10+
in mypy, like the daemon or the new implementation of flexible redefinitions.
11+
12+
Contributed by Ivan Levkivskyi, Jukka Lehtosalo, Shantanu in [PR 21163](https://github.com/python/mypy/pull/21163)
13+
14+
### Enabling `--strict-bytes` by default
15+
16+
Per [PEP 688](https://peps.python.org/pep-0688), mypy no longer treats `bytearray` and `memoryview`
17+
values as assignable to the `bytes` type.
18+
19+
Contributed by Shantanu in [PR 18371](https://github.com/python/mypy/pull/18371)
20+
21+
### Drop Support for Targeting Python 3.9
22+
23+
Mypy no longer supports type checking code with `--python-version 3.9`.
24+
Use `--python-version 3.10` or newer.
25+
26+
Contributed by Shantanu, Marc Mueller in [PR 21243](https://github.com/python/mypy/pull/21243)
27+
28+
### Remove special casing of legacy bundled stubs
29+
30+
Mypy used to bundle stubs for a few packages in versions 0.812 and earlier. To navigate the
31+
transition, mypy used to report missing types for these packages even if `--ignore-missing-imports`
32+
was set. Mypy now consistently respects `--ignore-missing-imports` for all packages.
33+
34+
Contributed by Shantanu in [PR 18372](https://github.com/python/mypy/pull/18372)
35+
36+
### Prevent assignment to None for non-Optional class variables with type comments
37+
38+
Mypy used to allow assignment to None for class variables when using type comments. This was a
39+
common idiom in Python 3.5 and earlier, prior to the introduction of variable annotations.
40+
However, this was a soundness hole and has now been removed.
41+
42+
Contributed by Shantanu in [PR 20054](https://github.com/python/mypy/pull/20054)
43+
544
## Mypy 1.20
645

746
We’ve just uploaded mypy 1.20.0 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)).

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: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ of the above sections.
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
@@ -644,7 +644,7 @@ 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

649649
.. option:: --allow-redefinition
650650

@@ -684,30 +684,26 @@ of the above sections.
684684
items = "100" # valid, items now has type str
685685
items = int(items) # valid, items now has type int
686686
687-
.. option:: --local-partial-types
687+
.. option:: --no-local-partial-types
688688

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:
689+
Disable local partial types to enable legacy type inference mode for
690+
containers.
694691

695-
.. 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:
696695

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

712708
.. option:: --no-implicit-reexport
713709

@@ -764,11 +760,11 @@ of the above sections.
764760
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>`
765761
only works in combination with :option:`--strict-equality <mypy --strict-equality>`.
766762

767-
.. option:: --strict-bytes
763+
.. option:: --no-strict-bytes
768764

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*.
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.
772768

773769
.. code-block:: python
774770
@@ -777,10 +773,12 @@ of the above sections.
777773
with open("binary_file", "wb") as fp:
778774
fp.write(buf)
779775
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"
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"
782779
783-
# 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:
784782
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier
785783
786784
def f(buf: Buffer) -> None:

docs/source/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ subtly different, and it's important to understand how they differ to avoid pitf
671671

672672
.. code-block:: python
673673
674-
from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier
674+
from typing import TypeAlias
675675
676676
class A: ...
677677
Alias: TypeAlias = A

docs/source/config_file.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Platform configuration
432432

433433
Specifies the Python version used to parse and check the target
434434
program. The string should be in the format ``MAJOR.MINOR`` --
435-
for example ``3.9``. The default is the version of the Python
435+
for example ``3.10``. The default is the version of the Python
436436
interpreter used to run mypy.
437437

438438
This option may only be set in the global section (``[mypy]``).
@@ -720,9 +720,9 @@ section of the command line docs.
720720

721721
By default, mypy won't allow a variable to be redefined with an
722722
unrelated type. This flag enables the redefinition of unannotated
723-
variables with an arbitrary type. You will also need to enable
724-
:confval:`local_partial_types`.
725-
Example:
723+
variables with an arbitrary type. This also requires
724+
:confval:`local_partial_types`, which is enabled by default starting
725+
from mypy 2.0. Example:
726726

727727
.. code-block:: python
728728
@@ -761,7 +761,7 @@ section of the command line docs.
761761
reveal_type(values) # Revealed type is list[float]
762762
763763
Note: We are planning to turn this flag on by default in a future mypy
764-
release, along with :confval:`local_partial_types`.
764+
release.
765765

766766
.. confval:: allow_redefinition_old
767767

@@ -800,11 +800,11 @@ section of the command line docs.
800800
.. confval:: local_partial_types
801801

802802
:type: boolean
803-
:default: False
803+
:default: True
804804

805-
Disallows inferring variable type for ``None`` from two assignments in different scopes.
806-
This is always implicitly enabled when using the :ref:`mypy daemon <mypy_daemon>`.
807-
This will be enabled by default in mypy v2.0 release.
805+
This prevents inferring a variable type from an empty container (such as a list or
806+
a dictionary) created at module top level or class body and updated in
807+
a function. This must be enabled when using the :ref:`mypy daemon <mypy_daemon>`.
808808

809809
.. confval:: disable_error_code
810810

@@ -867,10 +867,10 @@ section of the command line docs.
867867
.. confval:: strict_bytes
868868

869869
:type: boolean
870-
:default: False
870+
:default: True
871871

872-
Disable treating ``bytearray`` and ``memoryview`` as subtypes of ``bytes``.
873-
This will be enabled by default in *mypy 2.0*.
872+
If disabled, mypy treats ``bytearray`` and ``memoryview`` as subtypes of ``bytes``.
873+
This has been enabled by default since mypy 2.0.
874874

875875
.. confval:: strict
876876

@@ -1255,7 +1255,7 @@ of your repo (or append it to the end of an existing ``pyproject.toml`` file) an
12551255
# mypy global options:
12561256
12571257
[tool.mypy]
1258-
python_version = "3.9"
1258+
python_version = "3.10"
12591259
warn_return_any = true
12601260
warn_unused_configs = true
12611261
exclude = [

docs/source/duck_type_compatibility.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ supported for a small set of built-in types:
88

99
* ``int`` is duck type compatible with ``float`` and ``complex``.
1010
* ``float`` is duck type compatible with ``complex``.
11-
* ``bytearray`` and ``memoryview`` are duck type compatible with ``bytes``.
12-
(this will be disabled by default in **mypy 2.0**, and currently can be
13-
disabled with :option:`--strict-bytes <mypy --strict-bytes>`.)
11+
12+
.. note::
13+
``bytearray`` and ``memoryview`` were duck type compatible with ``bytes``
14+
by default prior to mypy 2.0. This can still be enabled with
15+
:option:`--no-strict-bytes <mypy --no-strict-bytes>`.
1416

1517
For example, mypy considers an ``int`` object to be valid whenever a
1618
``float`` object is expected. Thus code like this is nice and clean

docs/source/generics.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,7 @@ class in more recent versions of Python:
14191419

14201420
.. code-block:: python
14211421
1422-
>>> # Only relevant for Python 3.8 and below
1423-
>>> # If using Python 3.9 or newer, prefer the 'list[int]' syntax
1422+
>>> # Prefer the 'list[int]' syntax
14241423
>>> from typing import List
14251424
>>> List[int]
14261425
typing.List[int]

docs/source/getting_started.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,6 @@ union type. For example, ``int`` is a subtype of ``int | str``:
197197
else:
198198
return user_id
199199
200-
.. note::
201-
202-
If using Python 3.9 or earlier, use ``typing.Union[int, str]`` instead of
203-
``int | str``, or use ``from __future__ import annotations`` at the top of
204-
the file (see :ref:`runtime_troubles`).
205-
206200
The :py:mod:`typing` module contains many other useful types.
207201

208202
For a quick overview, look through the :ref:`mypy cheatsheet <cheat-sheet-py3>`.

docs/source/kinds_of_types.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ such as ``int | None``. This is called an *optional type*:
315315
return None # Error: None not compatible with int
316316
return len(s)
317317
318-
To support Python 3.9 and earlier, you can use the :py:data:`~typing.Optional`
319-
type modifier instead, such as ``Optional[int]`` (``Optional[X]`` is
320-
the preferred shorthand for ``Union[X, None]``):
318+
You can also use the :py:data:`~typing.Optional` type modifier, such as
319+
``Optional[int]`` (``Optional[X]`` is the shorthand for ``Union[X, None]``):
321320

322321
.. code-block:: python
323322
@@ -515,12 +514,11 @@ distinguish them from implicit type aliases:
515514
it can't be used in contexts which require a class object. For example, it's
516515
not valid as a base class and it can't be used to construct instances.
517516

518-
There is also use an older syntax for defining explicit type aliases, which was
519-
introduced in Python 3.10 (:pep:`613`):
517+
There is also use an older syntax for defining explicit type aliases (:pep:`613`):
520518

521519
.. code-block:: python
522520
523-
from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier
521+
from typing import TypeAlias
524522
525523
AliasType: TypeAlias = list[dict[tuple[int, str], set[int]]] | tuple[str, list[str]]
526524

0 commit comments

Comments
 (0)