Skip to content

Commit a3505f7

Browse files
Merge branch 'master' into pr-19672
2 parents ee700a3 + dd55b66 commit a3505f7

97 files changed

Lines changed: 1402 additions & 420 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.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 84a9d586544a0408d4654f57f83a93cb048070fb Mon Sep 17 00:00:00 2001
1+
From b6d495c10e79fb56ff64f8fb90c87894090f9cbe Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
33
Date: Sat, 15 Feb 2025 20:11:06 +0100
44
Subject: [PATCH] Partially revert Clean up argparse hacks
@@ -8,7 +8,7 @@ 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 b9fa31139..3c3ba116a 100644
11+
index ae99eb036..c87b8f4fc 100644
1212
--- a/mypy/typeshed/stdlib/argparse.pyi
1313
+++ b/mypy/typeshed/stdlib/argparse.pyi
1414
@@ -2,7 +2,7 @@ import sys
@@ -41,5 +41,5 @@ index b9fa31139..3c3ba116a 100644
4141
default: Any = ...,
4242
type: _ActionType = ...,
4343
--
44-
2.50.1
44+
2.52.0
4545

misc/typeshed_patches/0001-Remove-use-of-LiteralString-in-builtins-13743.patch

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 3229a6066cff3d80d6cb923322c2d42a300d0be3 Mon Sep 17 00:00:00 2001
1+
From ed11d7e0204868ef1a43c17ed950372b1d78f497 Mon Sep 17 00:00:00 2001
22
From: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
33
Date: Mon, 26 Sep 2022 12:55:07 -0700
44
Subject: [PATCH] Remove use of LiteralString in builtins (#13743)
@@ -8,18 +8,18 @@ Subject: [PATCH] Remove use of LiteralString in builtins (#13743)
88
1 file changed, 1 insertion(+), 99 deletions(-)
99

1010
diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
11-
index 969d16876..044e264d2 100644
11+
index 693dd0b77..8acdeadff 100644
1212
--- a/mypy/typeshed/stdlib/builtins.pyi
1313
+++ b/mypy/typeshed/stdlib/builtins.pyi
14-
@@ -63,7 +63,6 @@ from typing import ( # noqa: Y022,UP035
14+
@@ -64,7 +64,6 @@ from typing import ( # noqa: Y022,UP035
1515
from typing_extensions import ( # noqa: Y023
1616
Concatenate,
1717
Literal,
1818
- LiteralString,
1919
ParamSpec,
2020
Self,
2121
TypeAlias,
22-
@@ -480,31 +479,16 @@ class str(Sequence[str]):
22+
@@ -482,31 +481,16 @@ class str(Sequence[str]):
2323
def __new__(cls, object: object = "") -> Self: ...
2424
@overload
2525
def __new__(cls, object: ReadableBuffer, encoding: str = "utf-8", errors: str = "strict") -> Self: ...
@@ -51,7 +51,7 @@ index 969d16876..044e264d2 100644
5151
def format(self, *args: object, **kwargs: object) -> str: ...
5252
def format_map(self, mapping: _FormatMapMapping, /) -> str: ...
5353
def index(self, sub: str, start: SupportsIndex | None = None, end: SupportsIndex | None = None, /) -> int: ...
54-
@@ -520,98 +504,34 @@ class str(Sequence[str]):
54+
@@ -522,98 +506,34 @@ class str(Sequence[str]):
5555
def isspace(self) -> bool: ...
5656
def istitle(self) -> bool: ...
5757
def isupper(self) -> bool: ...
@@ -150,7 +150,7 @@ index 969d16876..044e264d2 100644
150150
def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc]
151151
@staticmethod
152152
@overload
153-
@@ -622,39 +542,21 @@ class str(Sequence[str]):
153+
@@ -624,39 +544,21 @@ class str(Sequence[str]):
154154
@staticmethod
155155
@overload
156156
def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ...
@@ -192,5 +192,5 @@ index 969d16876..044e264d2 100644
192192
def __getnewargs__(self) -> tuple[str]: ...
193193
def __format__(self, format_spec: str, /) -> str: ...
194194
--
195-
2.51.1
195+
2.52.0
196196

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 7678bc3f80e4d3f04a0ff0ee3a7d51f49ae4c465 Mon Sep 17 00:00:00 2001
1+
From 69791281c2c5e919cea9a77c4a771f79d9e70384 Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
33
Date: Sat, 21 Dec 2024 22:36:38 +0100
44
Subject: [PATCH] Revert Remove redundant inheritances from Iterator in
@@ -36,10 +36,10 @@ index d663f5d93..f43178e4d 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 044e264d2..6d813f172 100644
39+
index 8acdeadff..5a3bb5908 100644
4040
--- a/mypy/typeshed/stdlib/builtins.pyi
4141
+++ b/mypy/typeshed/stdlib/builtins.pyi
42-
@@ -1210,7 +1210,7 @@ class frozenset(AbstractSet[_T_co]):
42+
@@ -1226,7 +1226,7 @@ class frozenset(AbstractSet[_T_co]):
4343
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
4444

4545
@disjoint_base
@@ -48,7 +48,7 @@ index 044e264d2..6d813f172 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-
@@ -1404,7 +1404,7 @@ else:
51+
@@ -1413,7 +1413,7 @@ else:
5252
exit: _sitebuiltins.Quitter
5353

5454
@disjoint_base
@@ -57,7 +57,7 @@ index 044e264d2..6d813f172 100644
5757
@overload
5858
def __new__(cls, function: None, iterable: Iterable[_T | None], /) -> Self: ...
5959
@overload
60-
@@ -1468,7 +1468,7 @@ license: _sitebuiltins._Printer
60+
@@ -1477,7 +1477,7 @@ license: _sitebuiltins._Printer
6161

6262
def locals() -> dict[str, Any]: ...
6363
@disjoint_base
@@ -66,7 +66,7 @@ index 044e264d2..6d813f172 100644
6666
# 3.14 adds `strict` argument.
6767
if sys.version_info >= (3, 14):
6868
@overload
69-
@@ -1775,7 +1775,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
69+
@@ -1784,7 +1784,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
7070
quit: _sitebuiltins.Quitter
7171

7272
@disjoint_base
@@ -75,7 +75,7 @@ index 044e264d2..6d813f172 100644
7575
@overload
7676
def __new__(cls, sequence: Reversible[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
7777
@overload
78-
@@ -1839,7 +1839,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
78+
@@ -1848,7 +1848,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
7979
@overload
8080
def vars(object: Any = ..., /) -> dict[str, Any]: ...
8181
@disjoint_base
@@ -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 910d63814..eb942bc55 100644
110+
index 6778b7648..95164de2f 100644
111111
--- a/mypy/typeshed/stdlib/fileinput.pyi
112112
+++ b/mypy/typeshed/stdlib/fileinput.pyi
113113
@@ -1,8 +1,8 @@
@@ -118,7 +118,7 @@ index 910d63814..eb942bc55 100644
118118
from types import GenericAlias, TracebackType
119119
-from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload, type_check_only
120120
+from typing import IO, Any, AnyStr, Literal, Protocol, overload, type_check_only
121-
from typing_extensions import Self, TypeAlias
121+
from typing_extensions import Self, TypeAlias, deprecated
122122

123123
__all__ = [
124124
@@ -105,7 +105,7 @@ def fileno() -> int: ...
@@ -131,7 +131,7 @@ index 910d63814..eb942bc55 100644
131131
# encoding and errors are added
132132
@overload
133133
diff --git a/mypy/typeshed/stdlib/itertools.pyi b/mypy/typeshed/stdlib/itertools.pyi
134-
index fe4ccbdf8..73745fe92 100644
134+
index 8a924ad8b..5c2bf7f83 100644
135135
--- a/mypy/typeshed/stdlib/itertools.pyi
136136
+++ b/mypy/typeshed/stdlib/itertools.pyi
137137
@@ -28,7 +28,7 @@ _Predicate: TypeAlias = Callable[[_T], object]
@@ -166,7 +166,7 @@ index fe4ccbdf8..73745fe92 100644
166166
-class accumulate(Generic[_T]):
167167
+class accumulate(Iterator[_T]):
168168
@overload
169-
def __new__(cls, iterable: Iterable[_T], func: None = None, *, initial: _T | None = ...) -> Self: ...
169+
def __new__(cls, iterable: Iterable[_T], func: None = None, *, initial: _T | None = None) -> Self: ...
170170
@overload
171171
@@ -64,7 +64,7 @@ class accumulate(Generic[_T]):
172172
def __next__(self) -> _T: ...
@@ -239,7 +239,7 @@ index fe4ccbdf8..73745fe92 100644
239239
+class zip_longest(Iterator[_T_co]):
240240
# one iterable (fillvalue doesn't matter)
241241
@overload
242-
def __new__(cls, iter1: Iterable[_T1], /, *, fillvalue: object = ...) -> zip_longest[tuple[_T1]]: ...
242+
def __new__(cls, iter1: Iterable[_T1], /, *, fillvalue: object = None) -> zip_longest[tuple[_T1]]: ...
243243
@@ -202,7 +202,7 @@ class zip_longest(Generic[_T_co]):
244244
def __next__(self) -> _T_co: ...
245245

@@ -291,8 +291,8 @@ index fe4ccbdf8..73745fe92 100644
291291
- class batched(Generic[_T_co]):
292292
+ class batched(Iterator[tuple[_T_co, ...]], Generic[_T_co]):
293293
if sys.version_info >= (3, 13):
294-
def __new__(cls, iterable: Iterable[_T_co], n: int, *, strict: bool = False) -> Self: ...
295-
else:
294+
@overload
295+
def __new__(cls, iterable: Iterable[_T], n: Literal[1], *, strict: Literal[True]) -> batched[tuple[_T]]: ...
296296
diff --git a/mypy/typeshed/stdlib/multiprocessing/pool.pyi b/mypy/typeshed/stdlib/multiprocessing/pool.pyi
297297
index b79f9e773..f276372d0 100644
298298
--- a/mypy/typeshed/stdlib/multiprocessing/pool.pyi
@@ -313,10 +313,10 @@ index b79f9e773..f276372d0 100644
313313
def __iter__(self) -> Self: ...
314314
def next(self, timeout: float | None = None) -> _T: ...
315315
diff --git a/mypy/typeshed/stdlib/sqlite3/__init__.pyi b/mypy/typeshed/stdlib/sqlite3/__init__.pyi
316-
index 6b0f1ba94..882cd143c 100644
316+
index 04b978b1b..e4604144f 100644
317317
--- a/mypy/typeshed/stdlib/sqlite3/__init__.pyi
318318
+++ b/mypy/typeshed/stdlib/sqlite3/__init__.pyi
319-
@@ -407,7 +407,7 @@ class Connection:
319+
@@ -408,7 +408,7 @@ class Connection:
320320
) -> Literal[False]: ...
321321

322322
@disjoint_base
@@ -326,5 +326,5 @@ index 6b0f1ba94..882cd143c 100644
326326
@property
327327
def connection(self) -> Connection: ...
328328
--
329-
2.51.1
329+
2.52.0
330330

misc/typeshed_patches/0001-Revert-sum-literal-integer-change-13961.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 16b0b50ec77e470f24145071acde5274a1de53a0 Mon Sep 17 00:00:00 2001
1+
From 0e30b762e8335f02e19977c055ac7b98e707991c Mon Sep 17 00:00:00 2001
22
From: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
33
Date: Sat, 29 Oct 2022 12:47:21 -0700
44
Subject: [PATCH] Revert sum literal integer change (#13961)
@@ -19,10 +19,10 @@ within mypy, I might pursue upstreaming this in typeshed.
1919
1 file changed, 1 insertion(+), 1 deletion(-)
2020

2121
diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
22-
index 900c4c93f..d874edd8f 100644
22+
index 5a3bb5908..0f2196070 100644
2323
--- a/mypy/typeshed/stdlib/builtins.pyi
2424
+++ b/mypy/typeshed/stdlib/builtins.pyi
25-
@@ -1782,7 +1782,7 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
25+
@@ -1835,7 +1835,7 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
2626
# without creating many false-positive errors (see #7578).
2727
# Instead, we special-case the most common examples of this: bool and literal integers.
2828
@overload
@@ -32,5 +32,5 @@ index 900c4c93f..d874edd8f 100644
3232
def sum(iterable: Iterable[_SupportsSumNoDefaultT], /) -> _SupportsSumNoDefaultT | Literal[0]: ...
3333
@overload
3434
--
35-
2.49.0
35+
2.52.0
3636

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
From 85c0cfb55c6211c2a47c3f45d2ff28fa76f8204b Mon Sep 17 00:00:00 2001
1+
From 80a710e0e2c09612c6fc30c644f2052450e69ccd Mon Sep 17 00:00:00 2001
22
From: AlexWaygood <alex.waygood@gmail.com>
33
Date: Mon, 1 May 2023 20:34:55 +0100
4-
Subject: [PATCH] Revert typeshed ctypes change Since the plugin provides
5-
superior type checking:
6-
https://github.com/python/mypy/pull/13987#issuecomment-1310863427 A manual
7-
cherry-pick of e437cdf.
4+
Subject: [PATCH] Revert typeshed ctypes change
85

6+
The plugin provides superior type checking:
7+
https://github.com/python/mypy/pull/13987#issuecomment-1310863427
8+
A manual cherry-pick of e437cdf.
99
---
1010
mypy/typeshed/stdlib/_ctypes.pyi | 6 +-----
1111
1 file changed, 1 insertion(+), 5 deletions(-)
1212

1313
diff --git a/mypy/typeshed/stdlib/_ctypes.pyi b/mypy/typeshed/stdlib/_ctypes.pyi
14-
index 944685646..dc8c7b2ca 100644
14+
index be7792818..b7a3fb104 100644
1515
--- a/mypy/typeshed/stdlib/_ctypes.pyi
1616
+++ b/mypy/typeshed/stdlib/_ctypes.pyi
17-
@@ -289,11 +289,7 @@ class Array(_CData, Generic[_CT], metaclass=_PyCArrayType):
17+
@@ -320,11 +320,7 @@ class Array(_CData, Generic[_CT], metaclass=_PyCArrayType):
1818
def _type_(self) -> type[_CT]: ...
1919
@_type_.setter
2020
def _type_(self, value: type[_CT]) -> None: ...
@@ -28,5 +28,5 @@ index 944685646..dc8c7b2ca 100644
2828
# TODO: These methods cannot be annotated correctly at the moment.
2929
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT
3030
--
31-
2.49.0
31+
2.52.0
3232

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5070,6 +5070,8 @@ def visit_if_stmt(self, s: IfStmt) -> None:
50705070

50715071
if_map, else_map = self.find_isinstance_check(e)
50725072

5073+
s.unreachable_else = else_map is None
5074+
50735075
# XXX Issue a warning if condition is always False?
50745076
with self.binder.frame_context(can_skip=True, fall_through=2):
50755077
self.push_type_map(if_map, from_assignment=False)

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,7 @@ def infer_overload_return_type(
29182918

29192919
for typ in plausible_targets:
29202920
assert self.msg is self.chk.msg
2921-
with self.msg.filter_errors() as w:
2921+
with self.msg.filter_errors(filter_revealed_type=True) as w:
29222922
with self.chk.local_type_map as m:
29232923
ret_type, infer_type = self.check_call(
29242924
callee=typ,

mypy/checkmember.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,9 +1349,6 @@ def analyze_enum_class_attribute_access(
13491349
# Skip these since Enum will remove it
13501350
if name in EXCLUDED_ENUM_ATTRIBUTES:
13511351
return report_missing_attribute(mx.original_type, itype, name, mx)
1352-
# Dunders and private names are not Enum members
1353-
if name.startswith("__") and name.replace("_", "") != "":
1354-
return None
13551352

13561353
node = itype.type.get(name)
13571354
if node and node.type:
@@ -1364,6 +1361,9 @@ def analyze_enum_class_attribute_access(
13641361
):
13651362
return proper.args[0]
13661363

1364+
if name not in itype.type.enum_members:
1365+
return None
1366+
13671367
enum_literal = LiteralType(name, fallback=itype)
13681368
return itype.copy_modified(last_known_value=enum_literal)
13691369

mypy/join.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,34 @@ def visit_instance(self, t: Instance) -> ProperType:
377377
return self.default(self.s)
378378

379379
def visit_callable_type(self, t: CallableType) -> ProperType:
380-
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
381-
if is_equivalent(t, self.s):
382-
return combine_similar_callables(t, self.s)
383-
result = join_similar_callables(t, self.s)
384-
# We set the from_type_type flag to suppress error when a collection of
385-
# concrete class objects gets inferred as their common abstract superclass.
386-
if not (
387-
(t.is_type_obj() and t.type_object().is_abstract)
388-
or (self.s.is_type_obj() and self.s.type_object().is_abstract)
389-
):
390-
result.from_type_type = True
391-
if any(
392-
isinstance(tp, (NoneType, UninhabitedType))
393-
for tp in get_proper_types(result.arg_types)
394-
):
395-
# We don't want to return unusable Callable, attempt fallback instead.
380+
if isinstance(self.s, CallableType):
381+
if is_similar_callables(t, self.s):
382+
if is_equivalent(t, self.s):
383+
return combine_similar_callables(t, self.s)
384+
result = join_similar_callables(t, self.s)
385+
if any(
386+
isinstance(tp, (NoneType, UninhabitedType))
387+
for tp in get_proper_types(result.arg_types)
388+
):
389+
# We don't want to return unusable Callable, attempt fallback instead.
390+
return join_types(t.fallback, self.s)
391+
# We set the from_type_type flag to suppress error when a collection of
392+
# concrete class objects gets inferred as their common abstract superclass.
393+
if not (
394+
(t.is_type_obj() and t.type_object().is_abstract)
395+
or (self.s.is_type_obj() and self.s.type_object().is_abstract)
396+
):
397+
result.from_type_type = True
398+
return result
399+
else:
400+
s2, t2 = self.s, t
401+
if t2.is_var_arg:
402+
s2, t2 = t2, s2
403+
if is_subtype(s2, t2):
404+
return t2.copy_modified()
405+
elif is_subtype(t2, s2):
406+
return s2.copy_modified()
396407
return join_types(t.fallback, self.s)
397-
return result
398408
elif isinstance(self.s, Overloaded):
399409
# Switch the order of arguments to that we'll get to visit_overloaded.
400410
return join_types(t, self.s)

mypy/nodes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,19 +1832,26 @@ def accept(self, visitor: StatementVisitor[T]) -> T:
18321832

18331833

18341834
class IfStmt(Statement):
1835-
__slots__ = ("expr", "body", "else_body")
1835+
__slots__ = ("expr", "body", "else_body", "unreachable_else")
18361836

1837-
__match_args__ = ("expr", "body", "else_body")
1837+
__match_args__ = ("expr", "body", "else_body", "unreachable_else")
18381838

18391839
expr: list[Expression]
18401840
body: list[Block]
18411841
else_body: Block | None
1842+
# (If there is actually no else statement, semantic analysis may nevertheless create an
1843+
# empty else block and mark it permanently as unreachable to tell that the control flow
1844+
# must always go through the if block.)
1845+
unreachable_else: bool
1846+
# (Type checking may modify this flag repeatedly to indicate whether an actually available
1847+
# or unavailable else block is unreachable, considering the current type information.)
18421848

18431849
def __init__(self, expr: list[Expression], body: list[Block], else_body: Block | None) -> None:
18441850
super().__init__()
18451851
self.expr = expr
18461852
self.body = body
18471853
self.else_body = else_body
1854+
self.unreachable_else = False
18481855

18491856
def accept(self, visitor: StatementVisitor[T]) -> T:
18501857
return visitor.visit_if_stmt(self)

0 commit comments

Comments
 (0)