Skip to content

Commit 6cbd2cc

Browse files
authored
Revert dict.__or__ typeshed change (#21186)
Fixes #21141 I am not sure how typeshed patches work. If possible, I would add some tests here, otherwise I will add tests in a follow-up PR.
1 parent 220424a commit 6cbd2cc

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
From d88a4b774fc59ecd0f2d0fdda8e0e61092adafd3 Mon Sep 17 00:00:00 2001
2+
From: Ivan Levkivskyi <levkivskyi@gmail.com>
3+
Date: Wed, 8 Apr 2026 00:01:44 +0100
4+
Subject: [PATCH] Revert dict.__or__ typeshed change
5+
6+
---
7+
mypy/typeshed/stdlib/builtins.pyi | 6 ++++++
8+
1 file changed, 6 insertions(+)
9+
10+
diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
11+
index 674142d70..25b21ba97 100644
12+
--- a/mypy/typeshed/stdlib/builtins.pyi
13+
+++ b/mypy/typeshed/stdlib/builtins.pyi
14+
@@ -1142,7 +1142,13 @@ class dict(MutableMapping[_KT, _VT]):
15+
def __reversed__(self) -> Iterator[_KT]: ...
16+
__hash__: ClassVar[None] # type: ignore[assignment]
17+
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
18+
+ @overload
19+
+ def __or__(self, value: dict[_KT, _VT], /) -> dict[_KT, _VT]: ...
20+
+ @overload
21+
def __or__(self, value: dict[_T1, _T2], /) -> dict[_KT | _T1, _VT | _T2]: ...
22+
+ @overload
23+
+ def __ror__(self, value: dict[_KT, _VT], /) -> dict[_KT, _VT]: ...
24+
+ @overload
25+
def __ror__(self, value: dict[_T1, _T2], /) -> dict[_KT | _T1, _VT | _T2]: ...
26+
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
27+
@overload # type: ignore[misc]
28+
--
29+
2.25.1
30+

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,13 @@ class dict(MutableMapping[_KT, _VT]):
11421142
def __reversed__(self) -> Iterator[_KT]: ...
11431143
__hash__: ClassVar[None] # type: ignore[assignment]
11441144
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
1145+
@overload
1146+
def __or__(self, value: dict[_KT, _VT], /) -> dict[_KT, _VT]: ...
1147+
@overload
11451148
def __or__(self, value: dict[_T1, _T2], /) -> dict[_KT | _T1, _VT | _T2]: ...
1149+
@overload
1150+
def __ror__(self, value: dict[_KT, _VT], /) -> dict[_KT, _VT]: ...
1151+
@overload
11461152
def __ror__(self, value: dict[_T1, _T2], /) -> dict[_KT | _T1, _VT | _T2]: ...
11471153
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
11481154
@overload # type: ignore[misc]

test-data/unit/pythoneval.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,3 +2229,17 @@ def f(x: int, y: list[str]):
22292229
x in y
22302230
[out]
22312231
_testStrictEqualityWithList.py:3: error: Non-overlapping container check (element type: "int", container item type: "str")
2232+
2233+
[case testDictOrUnionEdgeCases]
2234+
from typing import Mapping, Sequence, Union
2235+
2236+
d: dict[str, list[str]]
2237+
m: Mapping[str, Sequence[str]] = d | d
2238+
2239+
A = dict[str, Union[A, int]]
2240+
d1: A
2241+
d2: A
2242+
d3: A = d1 | d2
2243+
reveal_type(d1 | d2)
2244+
[out]
2245+
_testDictOrUnionEdgeCases.py:10: note: Revealed type is "dict[str, dict[str, ... | int] | int]"

0 commit comments

Comments
 (0)