Skip to content

Commit 8ea65c0

Browse files
authored
Merge branch 'master' into master
2 parents 9b3fe05 + 3c09b32 commit 8ea65c0

Some content is hidden

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

76 files changed

+2472
-823
lines changed

.git-blame-ignore-revs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
f98f78216ba9d6ab68c8e69c19e9f3c7926c5efe
77
# run pyupgrade (#12711)
88
fc335cb16315964b923eb1927e3aad1516891c28
9+
# update black to 23.3.0 (#15059)
10+
4276308be01ea498d946a79554b4a10b1cf13ccb
11+
# Update black to 24.1.1 (#16847)
12+
8107e53158d83d30bb04d290ac10d8d3ccd344f8

.github/workflows/test.yml

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ jobs:
7171
tox_extra_args: "-n 4"
7272
test_mypyc: true
7373

74+
- name: Test suite with py313-dev-ubuntu, mypyc-compiled
75+
python: '3.13-dev'
76+
arch: x64
77+
os: ubuntu-latest
78+
toxenv: py
79+
tox_extra_args: "-n 4"
80+
test_mypyc: true
81+
# - name: Test suite with py314-dev-ubuntu
82+
# python: '3.14-dev'
83+
# arch: x64
84+
# os: ubuntu-latest
85+
# toxenv: py
86+
# tox_extra_args: "-n 4"
87+
# allow_failure: true
88+
# test_mypyc: true
89+
7490
- name: mypyc runtime tests with py39-macos
7591
python: '3.9.18'
7692
arch: x64
@@ -119,51 +135,69 @@ jobs:
119135
MYPY_FORCE_TERMINAL_WIDTH: 200
120136
# Pytest
121137
PYTEST_ADDOPTS: --color=yes
138+
122139
steps:
123140
- uses: actions/checkout@v4
124-
- uses: actions/setup-python@v5
125-
with:
126-
python-version: ${{ matrix.python }}
127-
architecture: ${{ matrix.arch }}
141+
128142
- name: Debug build
129143
if: ${{ matrix.debug_build }}
130144
run: |
131145
PYTHONVERSION=${{ matrix.python }}
132146
PYTHONDIR=~/python-debug/python-$PYTHONVERSION
133147
VENV=$PYTHONDIR/env
134148
./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
149+
# TODO: does this do anything? env vars aren't passed to the next step right
135150
source $VENV/bin/activate
151+
- name: Latest Dev build
152+
if: ${{ endsWith(matrix.python, '-dev') }}
153+
run: |
154+
sudo apt-get update
155+
sudo apt-get install -y --no-install-recommends \
156+
build-essential gdb lcov libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev \
157+
libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev
158+
git clone --depth 1 https://github.com/python/cpython.git /tmp/cpython --branch $( echo ${{ matrix.python }} | sed 's/-dev//' )
159+
cd /tmp/cpython
160+
echo git rev-parse HEAD; git rev-parse HEAD
161+
git show --no-patch
162+
./configure --prefix=/opt/pythondev
163+
make -j$(nproc)
164+
sudo make install
165+
sudo ln -s /opt/pythondev/bin/python3 /opt/pythondev/bin/python
166+
sudo ln -s /opt/pythondev/bin/pip3 /opt/pythondev/bin/pip
167+
echo "/opt/pythondev/bin" >> $GITHUB_PATH
168+
- uses: actions/setup-python@v5
169+
if: ${{ !(matrix.debug_build || endsWith(matrix.python, '-dev')) }}
170+
with:
171+
python-version: ${{ matrix.python }}
172+
architecture: ${{ matrix.arch }}
173+
136174
- name: Install tox
137-
run: pip install setuptools==68.2.2 tox==4.11.0
175+
run: |
176+
echo PATH; echo $PATH
177+
echo which python; which python
178+
echo which pip; which pip
179+
echo python version; python -c 'import sys; print(sys.version)'
180+
echo debug build; python -c 'import sysconfig; print(bool(sysconfig.get_config_var("Py_DEBUG")))'
181+
echo os.cpu_count; python -c 'import os; print(os.cpu_count())'
182+
echo os.sched_getaffinity; python -c 'import os; print(len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
183+
pip install setuptools==68.2.2 tox==4.11.0
184+
138185
- name: Compiled with mypyc
139186
if: ${{ matrix.test_mypyc }}
140187
run: |
141188
pip install -r test-requirements.txt
142189
CC=clang MYPYC_OPT_LEVEL=0 MYPY_USE_MYPYC=1 pip install -e .
190+
143191
- name: Setup tox environment
144192
run: |
145-
tox run -e ${{ matrix.toxenv }} --notest
146-
python -c 'import os; print("os.cpu_count", os.cpu_count(), "os.sched_getaffinity", len(getattr(os, "sched_getaffinity", lambda *args: [])(0)))'
193+
tox run -e ${{ matrix.toxenv }} --notes
147194
- name: Test
148195
run: tox run -e ${{ matrix.toxenv }} --skip-pkg-install -- ${{ matrix.tox_extra_args }}
196+
continue-on-error: ${{ matrix.allow_failure == 'true' }}
149197

150-
python-nightly:
151-
runs-on: ubuntu-latest
152-
name: Test suite with Python nightly
153-
steps:
154-
- uses: actions/checkout@v3
155-
- uses: actions/setup-python@v4
156-
with:
157-
python-version: '3.13-dev'
158-
- name: Install tox
159-
run: pip install setuptools==68.2.2 tox==4.11.0
160-
- name: Setup tox environment
161-
run: tox run -e py --notest
162-
- name: Test
163-
run: tox run -e py --skip-pkg-install -- "-n 4"
164-
continue-on-error: true
165-
- name: Mark as a success
166-
run: exit 0
198+
- name: Mark as success (check failures manually)
199+
if: ${{ matrix.allow_failure == 'true' }}
200+
run: exit 0
167201

168202
python_32bits:
169203
runs-on: ubuntu-latest

docs/source/additional_features.rst

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,18 @@ define dataclasses. For example:
4646
UnorderedPoint(1, 2) < UnorderedPoint(3, 4) # Error: Unsupported operand types
4747
4848
Dataclasses can be generic and can be used in any other way a normal
49-
class can be used:
49+
class can be used (Python 3.12 syntax):
5050

5151
.. code-block:: python
5252
5353
from dataclasses import dataclass
54-
from typing import Generic, TypeVar
55-
56-
T = TypeVar('T')
5754
5855
@dataclass
59-
class BoxedData(Generic[T]):
56+
class BoxedData[T]:
6057
data: T
6158
label: str
6259
63-
def unbox(bd: BoxedData[T]) -> T:
60+
def unbox[T](bd: BoxedData[T]) -> T:
6461
...
6562
6663
val = unbox(BoxedData(42, "<important>")) # OK, inferred type is int
@@ -98,17 +95,16 @@ does **not** work:
9895
9996
10097
To have Mypy recognize a wrapper of :py:func:`dataclasses.dataclass <dataclasses.dataclass>`
101-
as a dataclass decorator, consider using the :py:func:`~typing.dataclass_transform` decorator:
98+
as a dataclass decorator, consider using the :py:func:`~typing.dataclass_transform`
99+
decorator (example uses Python 3.12 syntax):
102100

103101
.. code-block:: python
104102
105103
from dataclasses import dataclass, Field
106-
from typing import TypeVar, dataclass_transform
107-
108-
T = TypeVar('T')
104+
from typing import dataclass_transform
109105
110106
@dataclass_transform(field_specifiers=(Field,))
111-
def my_dataclass(cls: type[T]) -> type[T]:
107+
def my_dataclass[T](cls: type[T]) -> type[T]:
112108
...
113109
return dataclass(cls)
114110
@@ -367,20 +363,20 @@ Extended Callable types
367363
This feature is deprecated. You can use
368364
:ref:`callback protocols <callback_protocols>` as a replacement.
369365

370-
As an experimental mypy extension, you can specify :py:data:`~typing.Callable` types
366+
As an experimental mypy extension, you can specify :py:class:`~collections.abc.Callable` types
371367
that support keyword arguments, optional arguments, and more. When
372-
you specify the arguments of a :py:data:`~typing.Callable`, you can choose to supply just
368+
you specify the arguments of a :py:class:`~collections.abc.Callable`, you can choose to supply just
373369
the type of a nameless positional argument, or an "argument specifier"
374370
representing a more complicated form of argument. This allows one to
375371
more closely emulate the full range of possibilities given by the
376372
``def`` statement in Python.
377373

378374
As an example, here's a complicated function definition and the
379-
corresponding :py:data:`~typing.Callable`:
375+
corresponding :py:class:`~collections.abc.Callable`:
380376

381377
.. code-block:: python
382378
383-
from typing import Callable
379+
from collections.abc import Callable
384380
from mypy_extensions import (Arg, DefaultArg, NamedArg,
385381
DefaultNamedArg, VarArg, KwArg)
386382
@@ -453,7 +449,7 @@ purpose:
453449
In all cases, the ``type`` argument defaults to ``Any``, and if the
454450
``name`` argument is omitted the argument has no name (the name is
455451
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
456-
:py:data:`~typing.Callable` such as
452+
:py:class:`~collections.abc.Callable` such as
457453

458454
.. code-block:: python
459455
@@ -465,7 +461,7 @@ is equivalent to the following:
465461
466462
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float]
467463
468-
A :py:data:`~typing.Callable` with unspecified argument types, such as
464+
A :py:class:`~collections.abc.Callable` with unspecified argument types, such as
469465

470466
.. code-block:: python
471467

docs/source/cheat_sheet_py3.rst

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ Useful built-in types
7272
# On earlier versions, use Union
7373
x: list[Union[int, str]] = [3, 5, "test", "fun"]
7474
75-
# Use Optional[X] for a value that could be None
76-
# Optional[X] is the same as X | None or Union[X, None]
77-
x: Optional[str] = "something" if some_condition() else None
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'
77+
x: str | None = "something" if some_condition() else None
7878
if x is not None:
7979
# Mypy understands x won't be None here because of the if-statement
8080
print(x.upper())
@@ -88,7 +88,8 @@ Functions
8888

8989
.. code-block:: python
9090
91-
from typing import Callable, Iterator, Union, Optional
91+
from collections.abc import Iterator, Callable
92+
from typing import Union, Optional
9293
9394
# This is how you annotate a function definition
9495
def stringify(num: int) -> str:
@@ -121,13 +122,14 @@ Functions
121122
i += 1
122123
123124
# You can of course split a function annotation over multiple lines
124-
def send_email(address: Union[str, list[str]],
125-
sender: str,
126-
cc: Optional[list[str]],
127-
bcc: Optional[list[str]],
128-
subject: str = '',
129-
body: Optional[list[str]] = None
130-
) -> bool:
125+
def send_email(
126+
address: str | list[str],
127+
sender: str,
128+
cc: list[str] | None,
129+
bcc: list[str] | None,
130+
subject: str = '',
131+
body: list[str] | None = None,
132+
) -> bool:
131133
...
132134
133135
# Mypy understands positional-only and keyword-only arguments
@@ -230,7 +232,7 @@ When you're puzzled or when things are complicated
230232
# If you initialize a variable with an empty container or "None"
231233
# you may have to help mypy a bit by providing an explicit type annotation
232234
x: list[str] = []
233-
x: Optional[str] = None
235+
x: str | None = None
234236
235237
# Use Any if you don't know the type of something or it's too
236238
# dynamic to write a type for
@@ -274,7 +276,8 @@ that are common in idiomatic Python are standardized.
274276

275277
.. code-block:: python
276278
277-
from typing import Mapping, MutableMapping, Sequence, Iterable
279+
from collections.abc import Mapping, MutableMapping, Sequence, Iterable
280+
# or 'from typing import ...' (required in Python 3.8)
278281
279282
# Use Iterable for generic iterables (anything usable in "for"),
280283
# and Sequence where a sequence (supporting "len" and "__getitem__") is
@@ -349,11 +352,26 @@ Decorators
349352
**********
350353

351354
Decorator functions can be expressed via generics. See
352-
:ref:`declaring-decorators` for more details.
355+
:ref:`declaring-decorators` for more details. Example using Python 3.12
356+
syntax:
357+
358+
.. code-block:: python
359+
360+
from collections.abc import Callable
361+
from typing import Any
362+
363+
def bare_decorator[F: Callable[..., Any]](func: F) -> F:
364+
...
365+
366+
def decorator_args[F: Callable[..., Any]](url: str) -> Callable[[F], F]:
367+
...
368+
369+
The same example using pre-3.12 syntax:
353370

354371
.. code-block:: python
355372
356-
from typing import Any, Callable, TypeVar
373+
from collections.abc import Callable
374+
from typing import Any, TypeVar
357375
358376
F = TypeVar('F', bound=Callable[..., Any])
359377

docs/source/class_basics.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ between class and instance variables with callable types. For example:
152152

153153
.. code-block:: python
154154
155-
from typing import Callable, ClassVar
155+
from collections.abc import Callable
156+
from typing import ClassVar
156157
157158
class A:
158159
foo: Callable[[int], None]

docs/source/command_line.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Optional arguments
9696

9797
Show program's version number and exit.
9898

99+
.. option:: -O FORMAT, --output FORMAT {json}
100+
101+
Set a custom output format.
102+
99103
.. _config-file-flag:
100104

101105
Config file
@@ -420,11 +424,11 @@ The following flags adjust how mypy handles values of type ``None``.
420424

421425
.. option:: --implicit-optional
422426

423-
This flag causes mypy to treat arguments with a ``None``
424-
default value as having an implicit :py:data:`~typing.Optional` type.
427+
This flag causes mypy to treat parameters with a ``None``
428+
default value as having an implicit optional type (``T | None``).
425429

426430
For example, if this flag is set, mypy would assume that the ``x``
427-
parameter is actually of type ``Optional[int]`` in the code snippet below
431+
parameter is actually of type ``int | None`` in the code snippet below,
428432
since the default parameter is ``None``:
429433

430434
.. code-block:: python
@@ -438,7 +442,7 @@ The following flags adjust how mypy handles values of type ``None``.
438442

439443
.. option:: --no-strict-optional
440444

441-
This flag effectively disables checking of :py:data:`~typing.Optional`
445+
This flag effectively disables checking of optional
442446
types and ``None`` values. With this option, mypy doesn't
443447
generally check the use of ``None`` values -- it is treated
444448
as compatible with every type.
@@ -575,26 +579,24 @@ of the above sections.
575579
.. option:: --local-partial-types
576580

577581
In mypy, the most common cases for partial types are variables initialized using ``None``,
578-
but without explicit ``Optional`` annotations. By default, mypy won't check partial types
582+
but without explicit ``X | None`` annotations. By default, mypy won't check partial types
579583
spanning module top level or class top level. This flag changes the behavior to only allow
580584
partial types at local level, therefore it disallows inferring variable type for ``None``
581585
from two assignments in different scopes. For example:
582586

583587
.. code-block:: python
584588
585-
from typing import Optional
586-
587589
a = None # Need type annotation here if using --local-partial-types
588-
b: Optional[int] = None
590+
b: int | None = None
589591
590592
class Foo:
591593
bar = None # Need type annotation here if using --local-partial-types
592-
baz: Optional[int] = None
594+
baz: int | None = None
593595
594596
def __init__(self) -> None:
595597
self.bar = 1
596598
597-
reveal_type(Foo().bar) # Union[int, None] without --local-partial-types
599+
reveal_type(Foo().bar) # 'int | None' without --local-partial-types
598600
599601
Note: this option is always implicitly enabled in mypy daemon and will become
600602
enabled by default for mypy in a future release.
@@ -630,13 +632,11 @@ of the above sections.
630632

631633
.. code-block:: python
632634
633-
from typing import Text
634-
635635
items: list[int]
636636
if 'some string' in items: # Error: non-overlapping container check!
637637
...
638638
639-
text: Text
639+
text: str
640640
if text != b'other bytes': # Error: non-overlapping equality check!
641641
...
642642

0 commit comments

Comments
 (0)