Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff1d781

Browse filesBrowse files
committed
fix: forward ref
1 parent 6f5000c commit ff1d781
Copy full SHA for ff1d781

File tree

Expand file treeCollapse file tree

2 files changed

+26
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-7
lines changed

‎graphene_pydantic/converters.py

Copy file name to clipboardExpand all lines: graphene_pydantic/converters.py
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import typing as T
99
import uuid
1010
from types import UnionType
11-
from typing import get_origin
11+
from typing import Type, get_origin
1212

1313
import graphene
1414
from graphene import (
@@ -25,14 +25,13 @@
2525
UUID,
2626
Union,
2727
)
28-
from graphene.types.base import BaseType
2928
from graphene.types.datetime import Date, DateTime, Time
3029
from pydantic import BaseModel
3130
from pydantic.fields import FieldInfo
3231
from pydantic_core import PydanticUndefined
3332

3433
from .registry import Registry
35-
from .util import construct_union_class_name
34+
from .util import construct_union_class_name, evaluate_forward_ref
3635

3736
GRAPHENE2 = graphene.VERSION[0] < 3
3837

@@ -168,7 +167,7 @@ def convert_pydantic_type(
168167
registry: Registry,
169168
parent_type: T.Type = None,
170169
model: T.Type[BaseModel] = None,
171-
) -> BaseType: # noqa: C901
170+
) -> Type: # noqa: C901
172171
"""
173172
Convert a Pydantic type to a Graphene Field type, including not just the
174173
native Python type but any additional metadata (e.g. shape) that Pydantic
@@ -190,7 +189,7 @@ def find_graphene_type(
190189
registry: Registry,
191190
parent_type: T.Type = None,
192191
model: T.Type[BaseModel] = None,
193-
) -> BaseType: # noqa: C901
192+
) -> Type: # noqa: C901
194193
"""
195194
Map a native Python type to a Graphene-supported Field type, where possible,
196195
throwing an error if we don't know what to map it to.
@@ -254,8 +253,9 @@ def find_graphene_type(
254253
"the forward reference. Did you call `resolve_placeholders()`? "
255254
"See the README for more on forward references."
256255
)
256+
257257
module_ns = sys.modules[sibling.__module__].__dict__
258-
resolved = T.cast(T.Any, type_)._evaluate(type_, module_ns, None)
258+
resolved = evaluate_forward_ref(type_, module_ns, None)
259259
# TODO: make this behavior optional. maybe this is a place for the TypeOptions to play a role?
260260
if registry:
261261
registry.add_placeholder_for_model(resolved)
@@ -294,7 +294,7 @@ def convert_generic_python_type(
294294
registry: Registry,
295295
parent_type: T.Type = None,
296296
model: T.Type[BaseModel] = None,
297-
) -> BaseType: # noqa: C901
297+
) -> Type: # noqa: C901
298298
"""
299299
Convert annotated Python generic types into the most appropriate Graphene
300300
Field type -- e.g. turn `typing.Union` into a Graphene Union.

‎graphene_pydantic/util.py

Copy file name to clipboardExpand all lines: graphene_pydantic/util.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import sys
12
import typing as T
3+
from typing import (
4+
Any,
5+
ForwardRef,
6+
cast,
7+
) # type: ignore
28

39

410
def construct_union_class_name(inner_types: T.Sequence[T.Type]) -> str:
@@ -10,3 +16,16 @@ def construct_union_class_name(inner_types: T.Sequence[T.Type]) -> str:
1016
caps_cased_names = "".join(n[0].upper() + n[1:] for n in type_names)
1117

1218
return f"UnionOf{caps_cased_names}"
19+
20+
21+
if sys.version_info < (3, 9):
22+
23+
def evaluate_forward_ref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
24+
return type_._evaluate(globalns, localns)
25+
26+
else:
27+
28+
def evaluate_forward_ref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
29+
# Even though it is the right signature for python 3.9, mypy complains with
30+
# `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
31+
return cast(Any, type_)._evaluate(globalns, localns, set())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.