Skip to content

Commit 4818e29

Browse files
committed
Re-implement union_items function
Since mypy v0.980 this function has been removed from `mypy.types`. This this commit we re-implement the same version present in v0.970.
1 parent 588026f commit 4818e29

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

classes/contrib/mypy/typeops/call_signatures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from mypy.typeops import get_type_vars, make_simplified_union
77
from mypy.types import CallableType, Instance, ProperType
88
from mypy.types import Type as MypyType
9-
from mypy.types import TypeVarType, union_items
9+
from mypy.types import TypeVarType
1010
from typing_extensions import Final, final
1111

1212
from classes.contrib.mypy.typeops import type_loader
13+
from classes.contrib.mypy.typeops.union_items import union_items
1314

1415
_INCOMPATIBLE_TYPEVAR_MSG: Final = (
1516
'Argument 1 to {0} has incompatible type "{1}"; expected "{2}"'

classes/contrib/mypy/typeops/mro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from mypy.subtypes import is_equivalent
55
from mypy.types import Instance
66
from mypy.types import Type as MypyType
7-
from mypy.types import UnionType, union_items
7+
from mypy.types import UnionType
88
from typing_extensions import final
99

1010
from classes.contrib.mypy.typeops import type_loader
11+
from classes.contrib.mypy.typeops.union_items import union_items
1112

1213

1314
@final
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from mypy.types import ProperType, Type, UnionType, get_proper_type
2+
3+
4+
def union_items(typ: Type) -> list[ProperType]:
5+
"""Return the flattened items of a union type.
6+
7+
For non-union types, return a list containing just the argument.
8+
9+
This is a re-implementation of the old `mypy.types.union_items`
10+
function (v0.970):
11+
Since mypy version 0.980 they removed this function from `mypy.types`.
12+
"""
13+
# https://github.com/python/mypy/blob/1f08cf44c7ec3dc4111aaf817958f7a51018ba38/mypy/types.py#L2960
14+
typ = get_proper_type(typ)
15+
if isinstance(typ, UnionType):
16+
union_types = []
17+
for a_type in typ.items:
18+
union_types.extend(union_items(a_type))
19+
return union_types
20+
21+
return [typ]

classes/contrib/mypy/validation/validate_instance/validate_runtime.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
from mypy.erasetype import erase_type
44
from mypy.plugin import MethodContext
5-
from mypy.sametypes import is_same_type
6-
from mypy.subtypes import is_subtype
5+
from mypy.subtypes import is_same_type, is_subtype
76
from mypy.types import Instance
87
from mypy.types import Type as MypyType
98
from mypy.types import TypedDictType

0 commit comments

Comments
 (0)