Skip to content

Commit 33b1c25

Browse files
Merge branch 'master' into fix_match_callable
2 parents 2011f01 + 64dff42 commit 33b1c25

File tree

197 files changed

+2949
-1696
lines changed

Some content is hidden

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

197 files changed

+2949
-1696
lines changed

.github/ISSUE_TEMPLATE/crash.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ labels: "crash"
1515

1616
**Traceback**
1717

18-
```
18+
```python-traceback
1919
(Insert traceback and other messages from mypy here -- use `--show-traceback`.)
2020
```
2121

@@ -25,6 +25,11 @@ labels: "crash"
2525
appreciated. We also very much appreciate it if you try to narrow the
2626
source down to a small stand-alone example.)
2727

28+
```python
29+
# Ideally, a small sample program that demonstrates the problem.
30+
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
31+
```
32+
2833
**Your Environment**
2934

3035
<!-- Include as many relevant details about the environment you experienced the bug in -->

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ Related PRs:
170170
* Enable ANSI color codes for dmypy client in Windows (wyattscarpenter, PR [19088](https://github.com/python/mypy/pull/19088))
171171
* Extend special case for context-based type variable inference to unions in return position (Stanislav Terliakov, PR [18976](https://github.com/python/mypy/pull/18976))
172172

173+
### Mypy 1.17.1
174+
* Retain `None` as constraints bottom if no bottoms were provided (Stanislav Terliakov, PR [19485](https://github.com/python/mypy/pull/19485))
175+
* Fix "ignored exception in `hasattr`" in dmypy (Stanislav Terliakov, PR [19428](https://github.com/python/mypy/pull/19428))
176+
* Prevent a crash when InitVar is redefined with a method in a subclass (Stanislav Terliakov, PR [19453](https://github.com/python/mypy/pull/19453))
177+
173178
### Acknowledgements
174179

175180
Thanks to all mypy contributors who contributed to this release:

docs/source/command_line.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,14 @@ of the above sections.
812812
Note: the exact list of flags enabled by running :option:`--strict` may change
813813
over time.
814814

815+
.. include:: strict_list.rst
816+
..
817+
The above file is autogenerated and included during html generation.
818+
(That's an include directive, and this is a comment.)
819+
It would be fine to generate it at some other time instead,
820+
theoretically, but we already had a convenient hook during html gen.
821+
822+
815823
.. option:: --disable-error-code
816824

817825
This flag allows disabling one or multiple error codes globally.

docs/source/common_issues.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ daemon <mypy_daemon>`, which can speed up incremental mypy runtimes by
218218
a factor of 10 or more. :ref:`Remote caching <remote-cache>` can
219219
make cold mypy runs several times faster.
220220

221+
Furthermore: as of `mypy 1.13 <https://mypy-lang.blogspot.com/2024/10/mypy-113-released.html>`_,
222+
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:
224+
225+
python3 -m pip install -U mypy[faster-cache]
226+
227+
Mypy may depend on orjson by default in the future.
228+
221229
Types of empty collections
222230
--------------------------
223231

@@ -505,11 +513,15 @@ to see the types of all local variables at once. Example:
505513
# b: builtins.str
506514
.. note::
507515

508-
``reveal_type`` and ``reveal_locals`` are only understood by mypy and
509-
don't exist in Python. If you try to run your program, you'll have to
510-
remove any ``reveal_type`` and ``reveal_locals`` calls before you can
511-
run your code. Both are always available and you don't need to import
512-
them.
516+
``reveal_type`` and ``reveal_locals`` are handled specially by mypy during
517+
type checking, and don't have to be defined or imported.
518+
519+
However, if you want to run your code,
520+
you'll have to remove any ``reveal_type`` and ``reveal_locals``
521+
calls from your program or else Python will give you an error at runtime.
522+
523+
Alternatively, you can import ``reveal_type`` from ``typing_extensions``
524+
or ``typing`` (on Python 3.11 and newer)
513525

514526
.. _silencing-linters:
515527

docs/source/dynamic_typing.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.. _dynamic-typing:
22

3-
43
Dynamically typed code
54
======================
65

@@ -94,6 +93,8 @@ third party libraries that mypy does not know about. This is particularly the ca
9493
when using the :option:`--ignore-missing-imports <mypy --ignore-missing-imports>`
9594
flag. See :ref:`fix-missing-imports` for more information about this.
9695

96+
.. _any-vs-object:
97+
9798
Any vs. object
9899
--------------
99100

docs/source/html_builder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,37 @@
1111
from sphinx.builders.html import StandaloneHTMLBuilder
1212
from sphinx.environment import BuildEnvironment
1313

14+
from mypy.main import define_options
15+
1416

1517
class MypyHTMLBuilder(StandaloneHTMLBuilder):
18+
strict_file: Path
19+
1620
def __init__(self, app: Sphinx, env: BuildEnvironment) -> None:
1721
super().__init__(app, env)
1822
self._ref_to_doc = {}
23+
self.strict_file = Path(self.srcdir) / "strict_list.rst"
24+
self._add_strict_list()
1925

2026
def write_doc(self, docname: str, doctree: document) -> None:
2127
super().write_doc(docname, doctree)
2228
self._ref_to_doc.update({_id: docname for _id in doctree.ids})
2329

30+
def _add_strict_list(self) -> None:
31+
strict_flags: list[str]
32+
_, strict_flags, _ = define_options()
33+
strict_part = ", ".join(f":option:`{s} <mypy {s}>`" for s in strict_flags)
34+
if (
35+
not strict_part
36+
or strict_part.isspace()
37+
or len(strict_part) < 20
38+
or len(strict_part) > 2000
39+
):
40+
raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).")
41+
self.strict_file.write_text(
42+
"For this version of mypy, the list of flags enabled by strict is: " + strict_part
43+
)
44+
2445
def _verify_error_codes(self) -> None:
2546
from mypy.errorcodes import error_codes
2647

@@ -55,6 +76,7 @@ def _write_ref_redirector(self) -> None:
5576
def finish(self) -> None:
5677
super().finish()
5778
self._write_ref_redirector()
79+
self.strict_file.unlink()
5880

5981

6082
def setup(app: Sphinx) -> dict[str, Any]:

docs/source/kinds_of_types.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ operations are permitted on the value, and the operations are only checked
4141
at runtime. You can use ``Any`` as an "escape hatch" when you can't use
4242
a more precise type for some reason.
4343

44+
This should not be confused with the
45+
:py:class:`object` type, which represents the set of all values.
46+
Unlike ``object``, ``Any`` introduces type unsafety — see
47+
:ref:`any-vs-object` for more.
48+
4449
``Any`` is compatible with every other type, and vice versa. You can freely
4550
assign a value of type ``Any`` to a variable with a more precise type:
4651

misc/typeshed_patches/0001-Partially-revert-Clean-up-argparse-hacks.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 05f351f6a37fe8b73c698c348bf6aa5108363049 Mon Sep 17 00:00:00 2001
1+
From 84a9d586544a0408d4654f57f83a93cb048070fb Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <[email protected]>
33
Date: Sat, 15 Feb 2025 20:11:06 +0100
44
Subject: [PATCH] Partially revert Clean up argparse hacks
@@ -8,15 +8,15 @@ Subject: [PATCH] Partially revert Clean up argparse hacks
88
1 file changed, 5 insertions(+), 3 deletions(-)
99

1010
diff --git a/mypy/typeshed/stdlib/argparse.pyi b/mypy/typeshed/stdlib/argparse.pyi
11-
index 95ad6c7da..79e6cfde1 100644
11+
index b9fa31139..3c3ba116a 100644
1212
--- a/mypy/typeshed/stdlib/argparse.pyi
1313
+++ b/mypy/typeshed/stdlib/argparse.pyi
1414
@@ -2,7 +2,7 @@ import sys
1515
from _typeshed import SupportsWrite, sentinel
1616
from collections.abc import Callable, Generator, Iterable, Sequence
1717
from re import Pattern
18-
-from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload
19-
+from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
18+
-from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload, type_check_only
19+
+from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload, type_check_only
2020
from typing_extensions import Self, TypeAlias, deprecated
2121

2222
__all__ = [
@@ -41,5 +41,5 @@ index 95ad6c7da..79e6cfde1 100644
4141
default: Any = ...,
4242
type: _ActionType = ...,
4343
--
44-
2.49.0
44+
2.50.1
4545

misc/typeshed_patches/0001-Revert-Remove-redundant-inheritances-from-Iterator.patch

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 363d69b366695fea117631d30c348e36b9a5a99d Mon Sep 17 00:00:00 2001
1+
From c217544146d36899d50e828d627652a0d8f63bb7 Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <[email protected]>
33
Date: Sat, 21 Dec 2024 22:36:38 +0100
44
Subject: [PATCH] Revert Remove redundant inheritances from Iterator in
@@ -15,7 +15,7 @@ Subject: [PATCH] Revert Remove redundant inheritances from Iterator in
1515
7 files changed, 34 insertions(+), 34 deletions(-)
1616

1717
diff --git a/mypy/typeshed/stdlib/_asyncio.pyi b/mypy/typeshed/stdlib/_asyncio.pyi
18-
index 4544680cc..19a2d12d8 100644
18+
index ed56f33af..5253e967e 100644
1919
--- a/mypy/typeshed/stdlib/_asyncio.pyi
2020
+++ b/mypy/typeshed/stdlib/_asyncio.pyi
2121
@@ -1,6 +1,6 @@
@@ -36,10 +36,10 @@ index 4544680cc..19a2d12d8 100644
3636
@property
3737
def _exception(self) -> BaseException | None: ...
3838
diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
39-
index ea77a730f..900c4c93f 100644
39+
index 0575be3c8..d9be595fe 100644
4040
--- a/mypy/typeshed/stdlib/builtins.pyi
4141
+++ b/mypy/typeshed/stdlib/builtins.pyi
42-
@@ -1170,7 +1170,7 @@ class frozenset(AbstractSet[_T_co]):
42+
@@ -1186,7 +1186,7 @@ class frozenset(AbstractSet[_T_co]):
4343
def __hash__(self) -> int: ...
4444
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
4545

@@ -48,7 +48,7 @@ index ea77a730f..900c4c93f 100644
4848
def __new__(cls, iterable: Iterable[_T], start: int = 0) -> Self: ...
4949
def __iter__(self) -> Self: ...
5050
def __next__(self) -> tuple[int, _T]: ...
51-
@@ -1366,7 +1366,7 @@ else:
51+
@@ -1380,7 +1380,7 @@ else:
5252

5353
exit: _sitebuiltins.Quitter
5454

@@ -57,7 +57,7 @@ index ea77a730f..900c4c93f 100644
5757
@overload
5858
def __new__(cls, function: None, iterable: Iterable[_T | None], /) -> Self: ...
5959
@overload
60-
@@ -1431,7 +1431,7 @@ license: _sitebuiltins._Printer
60+
@@ -1444,7 +1444,7 @@ license: _sitebuiltins._Printer
6161

6262
def locals() -> dict[str, Any]: ...
6363

@@ -66,7 +66,7 @@ index ea77a730f..900c4c93f 100644
6666
# 3.14 adds `strict` argument.
6767
if sys.version_info >= (3, 14):
6868
@overload
69-
@@ -1734,7 +1734,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
69+
@@ -1750,7 +1750,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
7070

7171
quit: _sitebuiltins.Quitter
7272

@@ -75,7 +75,7 @@ index ea77a730f..900c4c93f 100644
7575
@overload
7676
def __new__(cls, sequence: Reversible[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
7777
@overload
78-
@@ -1795,7 +1795,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
78+
@@ -1814,7 +1814,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
7979
@overload
8080
def vars(object: Any = ..., /) -> dict[str, Any]: ...
8181

@@ -107,7 +107,7 @@ index 2c8e7109c..4ed0ab1d8 100644
107107
restkey: _T | None
108108
restval: str | Any | None
109109
diff --git a/mypy/typeshed/stdlib/fileinput.pyi b/mypy/typeshed/stdlib/fileinput.pyi
110-
index 948b39ea1..1d5f9cf00 100644
110+
index 910d63814..eb942bc55 100644
111111
--- a/mypy/typeshed/stdlib/fileinput.pyi
112112
+++ b/mypy/typeshed/stdlib/fileinput.pyi
113113
@@ -1,8 +1,8 @@
@@ -116,12 +116,12 @@ index 948b39ea1..1d5f9cf00 100644
116116
-from collections.abc import Callable, Iterable
117117
+from collections.abc import Callable, Iterable, Iterator
118118
from types import GenericAlias, TracebackType
119-
-from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload
120-
+from typing import IO, Any, AnyStr, Literal, Protocol, overload
119+
-from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload, type_check_only
120+
+from typing import IO, Any, AnyStr, Literal, Protocol, overload, type_check_only
121121
from typing_extensions import Self, TypeAlias
122122

123123
__all__ = [
124-
@@ -104,7 +104,7 @@ def fileno() -> int: ...
124+
@@ -105,7 +105,7 @@ def fileno() -> int: ...
125125
def isfirstline() -> bool: ...
126126
def isstdin() -> bool: ...
127127

@@ -307,10 +307,10 @@ index b79f9e773..f276372d0 100644
307307
def __iter__(self) -> Self: ...
308308
def next(self, timeout: float | None = None) -> _T: ...
309309
diff --git a/mypy/typeshed/stdlib/sqlite3/__init__.pyi b/mypy/typeshed/stdlib/sqlite3/__init__.pyi
310-
index 5d3c2330b..ab783dbde 100644
310+
index bcfea3a13..5a659deac 100644
311311
--- a/mypy/typeshed/stdlib/sqlite3/__init__.pyi
312312
+++ b/mypy/typeshed/stdlib/sqlite3/__init__.pyi
313-
@@ -399,7 +399,7 @@ class Connection:
313+
@@ -405,7 +405,7 @@ class Connection:
314314
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None, /
315315
) -> Literal[False]: ...
316316

@@ -320,5 +320,5 @@ index 5d3c2330b..ab783dbde 100644
320320
@property
321321
def connection(self) -> Connection: ...
322322
--
323-
2.49.0
323+
2.50.1
324324

mypy/binder.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@
88

99
from mypy.erasetype import remove_instance_last_known_values
1010
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash, subkeys
11-
from mypy.nodes import Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, TypeInfo, Var
11+
from mypy.nodes import (
12+
LITERAL_NO,
13+
Expression,
14+
IndexExpr,
15+
MemberExpr,
16+
NameExpr,
17+
RefExpr,
18+
TypeInfo,
19+
Var,
20+
)
1221
from mypy.options import Options
1322
from mypy.subtypes import is_same_type, is_subtype
1423
from mypy.typeops import make_simplified_union
@@ -173,6 +182,15 @@ def _get(self, key: Key, index: int = -1) -> CurrentType | None:
173182
return self.frames[i].types[key]
174183
return None
175184

185+
@classmethod
186+
def can_put_directly(cls, expr: Expression) -> bool:
187+
"""Will `.put()` on this expression be successful?
188+
189+
This is inlined in `.put()` because the logic is rather hot and must be kept
190+
in sync.
191+
"""
192+
return isinstance(expr, (IndexExpr, MemberExpr, NameExpr)) and literal(expr) > LITERAL_NO
193+
176194
def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> None:
177195
"""Directly set the narrowed type of expression (if it supports it).
178196

0 commit comments

Comments
 (0)