Skip to content

Commit aec04c7

Browse files
committed
Fix PEP 604 isinstance caching (#17563)
Mentioned by ngnpope
1 parent cb44e4d commit aec04c7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

mypy/types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,12 +2898,19 @@ def relevant_items(self) -> list[Type]:
28982898
return [i for i in self.items if not isinstance(get_proper_type(i), NoneType)]
28992899

29002900
def serialize(self) -> JsonDict:
2901-
return {".class": "UnionType", "items": [t.serialize() for t in self.items]}
2901+
return {
2902+
".class": "UnionType",
2903+
"items": [t.serialize() for t in self.items],
2904+
"uses_pep604_syntax": self.uses_pep604_syntax,
2905+
}
29022906

29032907
@classmethod
29042908
def deserialize(cls, data: JsonDict) -> UnionType:
29052909
assert data[".class"] == "UnionType"
2906-
return UnionType([deserialize_type(t) for t in data["items"]])
2910+
return UnionType(
2911+
[deserialize_type(t) for t in data["items"]],
2912+
uses_pep604_syntax=data["uses_pep604_syntax"],
2913+
)
29072914

29082915

29092916
class PartialType(ProperType):

test-data/unit/check-incremental.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6726,3 +6726,20 @@ from typing_extensions import TypeIs
67266726
def guard(x: object) -> TypeIs[int]:
67276727
pass
67286728
[builtins fixtures/tuple.pyi]
6729+
6730+
[case testStartUsingPEP604Union]
6731+
# flags: --python-version 3.10
6732+
import a
6733+
[file a.py]
6734+
import lib
6735+
6736+
[file a.py.2]
6737+
from lib import IntOrStr
6738+
assert isinstance(1, IntOrStr)
6739+
6740+
[file lib.py]
6741+
from typing_extensions import TypeAlias
6742+
6743+
IntOrStr: TypeAlias = int | str
6744+
assert isinstance(1, IntOrStr)
6745+
[builtins fixtures/type.pyi]

0 commit comments

Comments
 (0)