|
7 | 7 | import sys |
8 | 8 | import typing as T |
9 | 9 | import uuid |
10 | | -from types import UnionType |
11 | 10 | from typing import Type, get_origin |
12 | 11 |
|
13 | 12 | import graphene |
|
33 | 32 | from .registry import Registry |
34 | 33 | from .util import construct_union_class_name, evaluate_forward_ref |
35 | 34 |
|
| 35 | +PYTHON10 = sys.version_info >= (3, 10) |
| 36 | +if PYTHON10: |
| 37 | + from types import UnionType |
| 38 | + |
36 | 39 | GRAPHENE2 = graphene.VERSION[0] < 3 |
37 | 40 |
|
38 | 41 | try: |
@@ -114,10 +117,15 @@ def convert_pydantic_field( |
114 | 117 | """ |
115 | 118 | declared_type = getattr(field, "annotation", None) |
116 | 119 |
|
117 | | - # Convert Python 11 UnionType to T.Union |
118 | | - is_union_type = ( |
119 | | - get_origin(declared_type) is T.Union or get_origin(declared_type) is UnionType |
120 | | - ) |
| 120 | + # Convert Python 10 UnionType to T.Union |
| 121 | + if PYTHON10: |
| 122 | + is_union_type = ( |
| 123 | + get_origin(declared_type) is T.Union |
| 124 | + or get_origin(declared_type) is UnionType |
| 125 | + ) |
| 126 | + else: |
| 127 | + is_union_type = get_origin(declared_type) is T.Union |
| 128 | + |
121 | 129 | if is_union_type: |
122 | 130 | declared_type = T.Union[declared_type.__args__] |
123 | 131 |
|
@@ -195,9 +203,10 @@ def find_graphene_type( |
195 | 203 | throwing an error if we don't know what to map it to. |
196 | 204 | """ |
197 | 205 |
|
198 | | - # Convert Python 11 UnionType to T.Union |
199 | | - if isinstance(type_, UnionType): |
200 | | - type_ = T.Union[type_.__args__] |
| 206 | + # Convert Python 10 UnionType to T.Union |
| 207 | + if PYTHON10: |
| 208 | + if isinstance(type_, UnionType): |
| 209 | + type_ = T.Union[type_.__args__] |
201 | 210 |
|
202 | 211 | if type_ == uuid.UUID: |
203 | 212 | return UUID |
|
0 commit comments