55import enum
66import inspect
77
8- from graphene import Field , Boolean , Dynamic , Enum , Float , Int , List , String , UUID , Union
8+ from graphene import (
9+ Field ,
10+ Boolean ,
11+ Dynamic ,
12+ Enum ,
13+ Float ,
14+ Int ,
15+ List ,
16+ String ,
17+ UUID ,
18+ Union ,
19+ )
920from graphene .types .base import BaseType
21+
1022try :
1123 from graphene .types .decimal import Decimal as GrapheneDecimal
24+
1225 DECIMAL_SUPPORTED = True
1326except ImportError :
1427 # graphene 2.1.5+ is required for Decimals
@@ -24,21 +37,21 @@ class ConversionError(TypeError):
2437 pass
2538
2639
27- # Placeholder for NoneType, so that we can easily reference it later
28- TYPE_NONE = type (None )
29-
30-
3140def get_attr_resolver (attr_name : str ) -> T .Callable :
3241 """
3342 Return a helper function that resolves a field with the given name by
3443 looking it up as an attribute of the type we're trying to resolve it on.
3544 """
45+
3646 def _get_field (root , _info ):
3747 return getattr (root , attr_name , None )
48+
3849 return _get_field
3950
4051
41- def convert_pydantic_field (field : fields .Field , registry : Registry , ** field_kwargs ) -> Field :
52+ def convert_pydantic_field (
53+ field : fields .Field , registry : Registry , ** field_kwargs
54+ ) -> Field :
4255 """
4356 Convert a Pydantic model field into a Graphene type field that we can add
4457 to the generated Graphene data model type.
@@ -58,7 +71,9 @@ def convert_pydantic_field(field: fields.Field, registry: Registry, **field_kwar
5871 return Field (resolver = get_attr_resolver (field .name ), ** field_kwargs )
5972
6073
61- def to_graphene_type (type_ : T .Type , field : fields .Field , registry : Registry = None ) -> BaseType : # noqa: C901
74+ def to_graphene_type (
75+ type_ : T .Type , field : fields .Field , registry : Registry = None
76+ ) -> BaseType : # noqa: C901
6277 """
6378 Map a native Python type to a Graphene-supported Field type, where possible.
6479 """
@@ -83,7 +98,7 @@ def to_graphene_type(type_: T.Type, field: fields.Field, registry: Registry = No
8398 elif type_ in (tuple , list , set ):
8499 # TODO: do Sets really belong here?
85100 return List
86- elif hasattr (type_ , ' __origin__' ):
101+ elif hasattr (type_ , " __origin__" ):
87102 return convert_generic_type (type_ , field , registry )
88103 elif issubclass (type_ , enum .Enum ):
89104 return Enum .from_enum (type_ )
@@ -98,7 +113,9 @@ def to_graphene_type(type_: T.Type, field: fields.Field, registry: Registry = No
98113 )
99114
100115
101- def convert_pydantic_type (type_ : T .Type , field : fields .Field , registry : Registry = None ) -> BaseType : # noqa: C901
116+ def convert_pydantic_type (
117+ type_ : T .Type , field : fields .Field , registry : Registry = None
118+ ) -> BaseType : # noqa: C901
102119 """
103120 Convert a Pydantic type to a Graphene Field type, including not just the
104121 native Python type but any additional metadata (e.g. shape) that Pydantic
@@ -107,7 +124,12 @@ def convert_pydantic_type(type_: T.Type, field: fields.Field, registry: Registry
107124 graphene_type = to_graphene_type (type_ , field , registry )
108125 if field .shape == fields .Shape .SINGLETON :
109126 return graphene_type
110- elif field .shape in (fields .Shape .LIST , fields .Shape .TUPLE , fields .Shape .SEQUENCE , fields .Shape .SET ):
127+ elif field .shape in (
128+ fields .Shape .LIST ,
129+ fields .Shape .TUPLE ,
130+ fields .Shape .SEQUENCE ,
131+ fields .Shape .SET ,
132+ ):
111133 # TODO: _should_ Sets remain here?
112134 return List (graphene_type )
113135 elif field .shape == fields .Shape .MAPPING :
@@ -142,16 +164,20 @@ def convert_union_type(type_, field, registry=None):
142164 wrapped_types = type_ .__args__
143165 # NOTE: a typing.Optional decomposes to a Union[None, T], so we can return
144166 # the Graphene type for T; Pydantic will have already parsed it as optional
145- if len (wrapped_types ) == 2 and TYPE_NONE in wrapped_types :
146- native_type = next (x for x in wrapped_types if x != TYPE_NONE )
167+ if len (wrapped_types ) == 2 and type ( None ) in wrapped_types :
168+ native_type = next (x for x in wrapped_types if x != type ( None )) # noqa: E721
147169 graphene_type = to_graphene_type (native_type , field , registry )
148170 return graphene_type
149171 else :
150172 # Otherwise, we use a little metaprogramming -- create our own unique
151173 # subclass of graphene.Union that knows its constituent Graphene types
152- graphene_types = tuple (to_graphene_type (x , field , registry ) for x in wrapped_types )
153- internal_meta = type ("Meta" , (), {'types' : graphene_types })
174+ graphene_types = tuple (
175+ to_graphene_type (x , field , registry ) for x in wrapped_types
176+ )
177+ internal_meta = type ("Meta" , (), {"types" : graphene_types })
154178
155179 union_class_name = "" .join (x .__name__ for x in wrapped_types )
156- union_class = type (f"Union_{ union_class_name } " , (Union ,), {'Meta' : internal_meta })
180+ union_class = type (
181+ f"Union_{ union_class_name } " , (Union ,), {"Meta" : internal_meta }
182+ )
157183 return union_class
0 commit comments