Skip to content

Commit

Permalink
Fix PEP 604 isinstance caching (python#17563)
Browse files Browse the repository at this point in the history
Mentioned by ngnpope
  • Loading branch information
hauntsaninja authored Jul 24, 2024
1 parent ed0cd4a commit 312694f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2900,12 +2900,19 @@ def relevant_items(self) -> list[Type]:
return [i for i in self.items if not isinstance(get_proper_type(i), NoneType)]

def serialize(self) -> JsonDict:
return {".class": "UnionType", "items": [t.serialize() for t in self.items]}
return {
".class": "UnionType",
"items": [t.serialize() for t in self.items],
"uses_pep604_syntax": self.uses_pep604_syntax,
}

@classmethod
def deserialize(cls, data: JsonDict) -> UnionType:
assert data[".class"] == "UnionType"
return UnionType([deserialize_type(t) for t in data["items"]])
return UnionType(
[deserialize_type(t) for t in data["items"]],
uses_pep604_syntax=data["uses_pep604_syntax"],
)


class PartialType(ProperType):
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -6726,3 +6726,20 @@ from typing_extensions import TypeIs
def guard(x: object) -> TypeIs[int]:
pass
[builtins fixtures/tuple.pyi]

[case testStartUsingPEP604Union]
# flags: --python-version 3.10
import a
[file a.py]
import lib

[file a.py.2]
from lib import IntOrStr
assert isinstance(1, IntOrStr)

[file lib.py]
from typing_extensions import TypeAlias

IntOrStr: TypeAlias = int | str
assert isinstance(1, IntOrStr)
[builtins fixtures/type.pyi]

0 comments on commit 312694f

Please sign in to comment.