Skip to content

Commit c1083a9

Browse files
Merge branch 'master' into weak-proxy
2 parents 4314545 + db67888 commit c1083a9

File tree

222 files changed

+9048
-3006
lines changed

Some content is hidden

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

222 files changed

+9048
-3006
lines changed

.github/workflows/mypy_primer.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
runs-on: ubuntu-latest
2929
strategy:
3030
matrix:
31-
shard-index: [0, 1, 2, 3, 4]
31+
shard-index: [0, 1, 2, 3, 4, 5]
3232
fail-fast: false
3333
timeout-minutes: 60
3434
steps:
@@ -63,11 +63,10 @@ jobs:
6363
mypy_primer \
6464
--repo mypy_to_test \
6565
--new $GITHUB_SHA --old base_commit \
66-
--num-shards 5 --shard-index ${{ matrix.shard-index }} \
66+
--num-shards 6 --shard-index ${{ matrix.shard-index }} \
6767
--debug \
6868
--additional-flags="--debug-serialize" \
6969
--output concise \
70-
--show-speed-regression \
7170
| tee diff_${{ matrix.shard-index }}.txt
7271
) || [ $? -eq 1 ]
7372
- if: ${{ matrix.shard-index == 0 }}

CHANGELOG.md

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

33
## Next Release
44

5+
### Remove Support for targeting Python 3.8
6+
7+
Mypy now requires `--python-version 3.9` or greater. Support for only Python 3.8 is
8+
fully removed now. Given an unsupported version, mypy will default to the oldest
9+
supported one, currently 3.9.
10+
11+
This change is necessary because typeshed stopped supporting Python 3.8 after it
12+
reached its End of Life in October 2024.
13+
14+
Contributed by Marc Mueller
15+
(PR [19157](https://github.com/python/mypy/pull/19157), PR [19162](https://github.com/python/mypy/pull/19162)).
16+
17+
### Initial Support for Python 3.14
18+
19+
Mypy is now tested on 3.14 and mypyc works with 3.14.0b3 and later.
20+
Mypyc compiled wheels of mypy itself will be available for new versions after 3.14.0rc1 is released.
21+
22+
Note that not all new features might be supported just yet.
23+
24+
Contributed by Marc Mueller (PR [19164](https://github.com/python/mypy/pull/19164))
25+
26+
### Deprecated Flag: \--force-uppercase-builtins
27+
28+
Mypy only supports Python 3.9+. The \--force-uppercase-builtins flag is now deprecated and a no-op. It will be removed in a future version.
29+
30+
Contributed by Marc Mueller (PR [19176](https://github.com/python/mypy/pull/19176))
31+
532
## Mypy 1.16
633

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

docs/source/command_line.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ of the above sections.
845845
x = 'a string'
846846
x.trim() # error: "str" has no attribute "trim" [attr-defined]
847847
848+
848849
.. _configuring-error-messages:
849850

850851
Configuring error messages
@@ -936,11 +937,6 @@ in error messages.
936937
useful or they may be overly noisy. If ``N`` is negative, there is
937938
no limit. The default limit is -1.
938939

939-
.. option:: --force-uppercase-builtins
940-
941-
Always use ``List`` instead of ``list`` in error messages,
942-
even on Python 3.9+.
943-
944940
.. option:: --force-union-syntax
945941

946942
Always use ``Union[]`` and ``Optional[]`` for union types

docs/source/config_file.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,6 @@ These options may only be set in the global section (``[mypy]``).
922922

923923
Show absolute paths to files.
924924

925-
.. confval:: force_uppercase_builtins
926-
927-
:type: boolean
928-
:default: False
929-
930-
Always use ``List`` instead of ``list`` in error messages,
931-
even on Python 3.9+.
932-
933925
.. confval:: force_union_syntax
934926

935927
:type: boolean

docs/source/error_code_list.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,35 @@ You can use :py:class:`~collections.abc.Callable` as the type for callable objec
215215
for x in objs:
216216
f(x)
217217
218+
.. _code-metaclass:
219+
220+
Check the validity of a class's metaclass [metaclass]
221+
-----------------------------------------------------
222+
223+
Mypy checks whether the metaclass of a class is valid. The metaclass
224+
must be a subclass of ``type``. Further, the class hierarchy must yield
225+
a consistent metaclass. For more details, see the
226+
`Python documentation <https://docs.python.org/3.13/reference/datamodel.html#determining-the-appropriate-metaclass>`_
227+
228+
Note that mypy's metaclass checking is limited and may produce false-positives.
229+
See also :ref:`limitations`.
230+
231+
Example with an error:
232+
233+
.. code-block:: python
234+
235+
class GoodMeta(type):
236+
pass
237+
238+
class BadMeta:
239+
pass
240+
241+
class A1(metaclass=GoodMeta): # OK
242+
pass
243+
244+
class A2(metaclass=BadMeta): # Error: Metaclasses not inheriting from "type" are not supported [metaclass]
245+
pass
246+
218247
.. _code-var-annotated:
219248

220249
Require annotation if variable type is unclear [var-annotated]

docs/source/error_code_list2.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,44 @@ Example:
612612
# mypy: disallow-any-explicit
613613
from typing import Any
614614
x: Any = 1 # Error: Explicit "Any" type annotation [explicit-any]
615+
616+
617+
.. _code-exhaustive-match:
618+
619+
Check that match statements match exhaustively [exhaustive-match]
620+
-----------------------------------------------------------------------
621+
622+
If enabled with :option:`--enable-error-code exhaustive-match <mypy --enable-error-code>`,
623+
mypy generates an error if a match statement does not match all possible cases/types.
624+
625+
626+
Example:
627+
628+
.. code-block:: python
629+
630+
import enum
631+
632+
633+
class Color(enum.Enum):
634+
RED = 1
635+
BLUE = 2
636+
637+
val: Color = Color.RED
638+
639+
# OK without --enable-error-code exhaustive-match
640+
match val:
641+
case Color.RED:
642+
print("red")
643+
644+
# With --enable-error-code exhaustive-match
645+
# Error: Match statement has unhandled case for values of type "Literal[Color.BLUE]"
646+
match val:
647+
case Color.RED:
648+
print("red")
649+
650+
# OK with or without --enable-error-code exhaustive-match, since all cases are handled
651+
match val:
652+
case Color.RED:
653+
print("red")
654+
case _:
655+
print("other")

docs/source/generics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ Let us illustrate this by few simple examples:
630630
631631
my_circles: list[Circle] = []
632632
add_one(my_circles) # This may appear safe, but...
633-
my_circles[-1].rotate() # ...this will fail, since my_circles[0] is now a Shape, not a Circle
633+
my_circles[0].rotate() # ...this will fail, since my_circles[0] is now a Shape, not a Circle
634634
635635
Another example of invariant type is ``dict``. Most mutable containers
636636
are invariant.

docs/source/literal_types.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ If we forget to handle one of the cases, mypy will generate an error:
468468
assert_never(direction) # E: Argument 1 to "assert_never" has incompatible type "Direction"; expected "NoReturn"
469469
470470
Exhaustiveness checking is also supported for match statements (Python 3.10 and later).
471+
For match statements specifically, inexhaustive matches can be caught
472+
without needing to use ``assert_never`` by using
473+
:option:`--enable-error-code exhaustive-match <mypy --enable-error-code>`.
474+
471475

472476
Extra Enum checks
473477
*****************

docs/source/metaclasses.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,28 @@ so it's better not to combine metaclasses and class hierarchies:
9090
* ``Self`` is not allowed as annotation in metaclasses as per `PEP 673`_.
9191

9292
.. _PEP 673: https://peps.python.org/pep-0673/#valid-locations-for-self
93+
94+
For some builtin types, mypy may think their metaclass is :py:class:`abc.ABCMeta`
95+
even if it is :py:class:`type` at runtime. In those cases, you can either:
96+
97+
* use :py:class:`abc.ABCMeta` instead of :py:class:`type` as the
98+
superclass of your metaclass if that works in your use-case
99+
* mute the error with ``# type: ignore[metaclass]``
100+
101+
.. code-block:: python
102+
103+
import abc
104+
105+
assert type(tuple) is type # metaclass of tuple is type at runtime
106+
107+
# The problem:
108+
class M0(type): pass
109+
class A0(tuple, metaclass=M0): pass # Mypy Error: metaclass conflict
110+
111+
# Option 1: use ABCMeta instead of type
112+
class M1(abc.ABCMeta): pass
113+
class A1(tuple, metaclass=M1): pass
114+
115+
# Option 2: mute the error
116+
class M2(type): pass
117+
class A2(tuple, metaclass=M2): pass # type: ignore[metaclass]

docs/source/protocols.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,53 @@ the parameters are positional-only. Example (using the legacy syntax for generic
352352
copy_a = copy_b # OK
353353
copy_b = copy_a # Also OK
354354
355+
Binding of types in protocol attributes
356+
***************************************
357+
358+
All protocol attributes annotations are treated as externally visible types
359+
of those attributes. This means that for example callables are not bound,
360+
and descriptors are not invoked:
361+
362+
.. code-block:: python
363+
364+
from typing import Callable, Protocol, overload
365+
366+
class Integer:
367+
@overload
368+
def __get__(self, instance: None, owner: object) -> Integer: ...
369+
@overload
370+
def __get__(self, instance: object, owner: object) -> int: ...
371+
# <some implementation>
372+
373+
class Example(Protocol):
374+
foo: Callable[[object], int]
375+
bar: Integer
376+
377+
ex: Example
378+
reveal_type(ex.foo) # Revealed type is Callable[[object], int]
379+
reveal_type(ex.bar) # Revealed type is Integer
380+
381+
In other words, protocol attribute types are handled as they would appear in a
382+
``self`` attribute annotation in a regular class. If you want some protocol
383+
attributes to be handled as though they were defined at class level, you should
384+
declare them explicitly using ``ClassVar[...]``. Continuing previous example:
385+
386+
.. code-block:: python
387+
388+
from typing import ClassVar
389+
390+
class OtherExample(Protocol):
391+
# This style is *not recommended*, but may be needed to reuse
392+
# some complex callable types. Otherwise use regular methods.
393+
foo: ClassVar[Callable[[object], int]]
394+
# This may be needed to mimic descriptor access on Type[...] types,
395+
# otherwise use a plain "bar: int" style.
396+
bar: ClassVar[Integer]
397+
398+
ex2: OtherExample
399+
reveal_type(ex2.foo) # Revealed type is Callable[[], int]
400+
reveal_type(ex2.bar) # Revealed type is int
401+
355402
.. _predefined_protocols_reference:
356403

357404
Predefined protocol reference

0 commit comments

Comments
 (0)