2424from graphene .types .datetime import Date , DateTime , Time
2525from pydantic import BaseModel
2626from pydantic .fields import ModelField
27+ from pydantic .typing import evaluate_forwardref
2728
2829from .registry import Registry
2930from .util import construct_union_class_name
3031
31- try :
32- # Pydantic pre-1.0
33- from pydantic .fields import Shape
34-
35- SHAPE_SINGLETON = (Shape .SINGLETON ,)
36- SHAPE_SEQUENTIAL = (
37- Shape .LIST ,
38- Shape .TUPLE ,
39- Shape .TUPLE_ELLIPS ,
40- Shape .SEQUENCE ,
41- Shape .SET ,
42- )
43- SHAPE_MAPPING = (Shape .MAPPING ,)
44- except ImportError :
45- # Pydantic 1.0+
46- from pydantic import fields
47-
48- SHAPE_SINGLETON = (fields .SHAPE_SINGLETON ,)
49- SHAPE_SEQUENTIAL = (
50- fields .SHAPE_LIST ,
51- fields .SHAPE_TUPLE ,
52- fields .SHAPE_TUPLE_ELLIPSIS ,
53- fields .SHAPE_SEQUENCE ,
54- fields .SHAPE_SET ,
32+ from pydantic import fields
33+
34+ SHAPE_SINGLETON = (fields .SHAPE_SINGLETON ,)
35+ SHAPE_SEQUENTIAL = (
36+ fields .SHAPE_LIST ,
37+ fields .SHAPE_TUPLE ,
38+ fields .SHAPE_TUPLE_ELLIPSIS ,
39+ fields .SHAPE_SEQUENCE ,
40+ fields .SHAPE_SET ,
41+ )
42+
43+ if hasattr (fields , "SHAPE_DICT" ):
44+ SHAPE_MAPPING = T .cast (
45+ T .Tuple , (fields .SHAPE_MAPPING , fields .SHAPE_DICT , fields .SHAPE_DEFAULTDICT )
5546 )
56- SHAPE_MAPPING = (fields .SHAPE_MAPPING ,)
47+ else :
48+ SHAPE_MAPPING = T .cast (T .Tuple , (fields .SHAPE_MAPPING ,))
5749
5850
5951try :
@@ -139,6 +131,10 @@ def convert_pydantic_field(
139131 # - maybe even (Sphinx-style) parse attribute documentation
140132 field_kwargs .setdefault ("description" , field .field_info .description )
141133
134+ # Somehow, this happens
135+ if "type_" not in field_kwargs and "type" in field_kwargs :
136+ field_kwargs ["type_" ] = field_kwargs .pop ("type" )
137+
142138 return Field (resolver = get_attr_resolver (field .name ), ** field_kwargs )
143139
144140
@@ -228,7 +224,7 @@ def find_graphene_type(
228224 "See the README for more on forward references."
229225 )
230226 module_ns = sys .modules [sibling .__module__ ].__dict__
231- resolved = type_ . _evaluate ( module_ns , None )
227+ resolved = evaluate_forwardref ( type_ , module_ns , None )
232228 # TODO: make this behavior optional. maybe this is a place for the TypeOptions to play a role?
233229 if registry :
234230 registry .add_placeholder_for_model (resolved )
@@ -267,7 +263,7 @@ def convert_generic_python_type(
267263 return convert_union_type (
268264 type_ , field , registry , parent_type = parent_type , model = model
269265 )
270- elif origin == T .Literal :
266+ elif hasattr ( T , "Literal" ) and origin == T .Literal :
271267 return convert_literal_type (
272268 type_ , field , registry , parent_type = parent_type , model = model
273269 )
@@ -338,6 +334,7 @@ def convert_union_type(
338334 )
339335 return union_cls
340336
337+
341338def convert_literal_type (
342339 type_ : T .Type ,
343340 field : ModelField ,
@@ -351,11 +348,7 @@ def convert_literal_type(
351348 inner_types = type_ .__args__
352349 # Here we'll expand the subtypes of this Literal into a corresponding more
353350 # general scalar type.
354- scalar_types = {
355- type (x )
356- for x in inner_types
357- if x != NONE_TYPE
358- }
351+ scalar_types = {type (x ) for x in inner_types if x != NONE_TYPE }
359352 graphene_scalar_types = [
360353 convert_pydantic_type (x , field , registry , parent_type = parent_type , model = model )
361354 for x in scalar_types
@@ -368,6 +361,10 @@ def convert_literal_type(
368361 internal_meta_cls = type ("Meta" , (), {"types" : graphene_scalar_types })
369362
370363 union_cls = type (
371- construct_union_class_name (scalar_types ), (Union ,), {"Meta" : internal_meta_cls }
364+ construct_union_class_name (
365+ sorted (scalar_types , key = lambda x : x .__class__ .__name__ )
366+ ),
367+ (Union ,),
368+ {"Meta" : internal_meta_cls },
372369 )
373370 return union_cls
0 commit comments