diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 4431de53e..9f79690e5 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -9,14 +9,14 @@ class Argument(MountedType): - def __init__(self, type, default_value=None, description=None, name=None, required=False, _creation_counter=None): + def __init__(self, type_, default_value=None, description=None, name=None, required=False, _creation_counter=None): super(Argument, self).__init__(_creation_counter=_creation_counter) if required: - type = NonNull(type) + type_ = NonNull(type_) self.name = name - self._type = type + self._type = type_ self.default_value = default_value self.description = description diff --git a/graphene/types/dynamic.py b/graphene/types/dynamic.py index 0efbd081c..736ceb43e 100644 --- a/graphene/types/dynamic.py +++ b/graphene/types/dynamic.py @@ -10,10 +10,10 @@ class Dynamic(MountedType): the schema. So we can have lazy fields. ''' - def __init__(self, type, with_schema=False, _creation_counter=None): + def __init__(self, type_, with_schema=False, _creation_counter=None): super(Dynamic, self).__init__(_creation_counter=_creation_counter) - assert inspect.isfunction(type) or isinstance(type, partial) - self.type = type + assert inspect.isfunction(type_) or isinstance(type_, partial) + self.type = type_ self.with_schema = with_schema def get_type(self, schema=None): diff --git a/graphene/types/field.py b/graphene/types/field.py index 4a79fcac6..f7a723981 100644 --- a/graphene/types/field.py +++ b/graphene/types/field.py @@ -20,7 +20,7 @@ def source_resolver(source, root, info, **args): class Field(MountedType): - def __init__(self, type, args=None, resolver=None, source=None, + def __init__(self, type_, args=None, resolver=None, source=None, deprecation_reason=None, name=None, description=None, required=False, _creation_counter=None, default_value=None, **extra_args): @@ -36,7 +36,7 @@ def __init__(self, type, args=None, resolver=None, source=None, ).format(base_type(default_value)) if required: - type = NonNull(type) + type_ = NonNull(type_) # Check if name is actually an argument of the field if isinstance(name, (Argument, UnmountedType)): @@ -49,7 +49,7 @@ def __init__(self, type, args=None, resolver=None, source=None, source = None self.name = name - self._type = type + self._type = type_ self.args = to_arguments(args or OrderedDict(), extra_args) if source: resolver = partial(source_resolver, source) diff --git a/graphene/types/inputfield.py b/graphene/types/inputfield.py index 0510ab4a0..9dc69d880 100644 --- a/graphene/types/inputfield.py +++ b/graphene/types/inputfield.py @@ -5,14 +5,14 @@ class InputField(MountedType): - def __init__(self, type, name=None, default_value=None, + def __init__(self, type_, name=None, default_value=None, deprecation_reason=None, description=None, required=False, _creation_counter=None, **extra_args): super(InputField, self).__init__(_creation_counter=_creation_counter) self.name = name if required: - type = NonNull(type) - self._type = type + type_ = NonNull(type_) + self._type = type_ self.deprecation_reason = deprecation_reason self.default_value = default_value self.description = description diff --git a/graphene/types/schema.py b/graphene/types/schema.py index 72bae81f8..69f4e6b6c 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -12,15 +12,15 @@ from .typemap import TypeMap, is_graphene_type -def assert_valid_root_type(_type): - if _type is None: +def assert_valid_root_type(type_): + if type_ is None: return is_graphene_objecttype = inspect.isclass( - _type) and issubclass(_type, ObjectType) - is_graphql_objecttype = isinstance(_type, GraphQLObjectType) + type_) and issubclass(type_, ObjectType) + is_graphql_objecttype = isinstance(type_, GraphQLObjectType) assert is_graphene_objecttype or is_graphql_objecttype, ( "Type {} is not a valid ObjectType." - ).format(_type) + ).format(type_) class Schema(GraphQLSchema): @@ -72,26 +72,26 @@ def __getattr__(self, type_name): Example: using schema.Query for accessing the "Query" type in the Schema ''' - _type = super(Schema, self).get_type(type_name) - if _type is None: + type_ = super(Schema, self).get_type(type_name) + if type_ is None: raise AttributeError( 'Type "{}" not found in the Schema'.format(type_name)) - if isinstance(_type, GrapheneGraphQLType): - return _type.graphene_type - return _type - - def get_graphql_type(self, _type): - if not _type: - return _type - if is_type(_type): - return _type - if is_graphene_type(_type): - graphql_type = self.get_type(_type._meta.name) + if isinstance(type_, GrapheneGraphQLType): + return type_.graphene_type + return type_ + + def get_graphql_type(self, type_): + if not type_: + return type_ + if is_type(type_): + return type_ + if is_graphene_type(type_): + graphql_type = self.get_type(type_._meta.name) assert graphql_type, "Type {} not found in this schema.".format( - _type._meta.name) - assert graphql_type.graphene_type == _type + type_._meta.name) + assert graphql_type.graphene_type == type_ return graphql_type - raise Exception("{} is not a valid GraphQL type.".format(_type)) + raise Exception("{} is not a valid GraphQL type.".format(type_)) def execute(self, *args, **kwargs): return graphql(self, *args, **kwargs) @@ -105,8 +105,8 @@ def introspect(self): def __str__(self): return print_schema(self) - def lazy(self, _type): - return lambda: self.get_type(_type) + def lazy(self, type_): + return lambda: self.get_type(type_) def build_typemap(self): initial_types = [ diff --git a/graphene/types/typemap.py b/graphene/types/typemap.py index b2bc4a0e9..ce419d1a2 100644 --- a/graphene/types/typemap.py +++ b/graphene/types/typemap.py @@ -28,33 +28,33 @@ from .utils import get_field_as -def is_graphene_type(_type): - if isinstance(_type, (List, NonNull)): +def is_graphene_type(type_): + if isinstance(type_, (List, NonNull)): return True - if inspect.isclass(_type) and issubclass(_type, + if inspect.isclass(type_) and issubclass(type_, (ObjectType, InputObjectType, Scalar, Interface, Union, Enum)): return True def resolve_type(resolve_type_func, map, type_name, root, info): - _type = resolve_type_func(root, info) + type_ = resolve_type_func(root, info) - if not _type: + if not type_: return_type = map[type_name] return get_default_resolve_type_fn(root, info, return_type) - if inspect.isclass(_type) and issubclass(_type, ObjectType): - graphql_type = map.get(_type._meta.name) + if inspect.isclass(type_) and issubclass(type_, ObjectType): + graphql_type = map.get(type_._meta.name) assert graphql_type, "Can't find type {} in schema".format( - _type._meta.name + type_._meta.name ) - assert graphql_type.graphene_type == _type, ( + assert graphql_type.graphene_type == type_, ( 'The type {} does not match with the associated graphene type {}.' - ).format(_type, graphql_type.graphene_type) + ).format(type_, graphql_type.graphene_type) return graphql_type - return _type + return type_ def is_type_of_from_possible_types(possible_types, root, info): @@ -68,45 +68,45 @@ def __init__(self, types, auto_camelcase=True, schema=None): self.schema = schema super(TypeMap, self).__init__(types) - def reducer(self, map, type): - if not type: + def reducer(self, map, type_): + if not type_: return map - if inspect.isfunction(type): - type = type() - if is_graphene_type(type): - return self.graphene_reducer(map, type) - return GraphQLTypeMap.reducer(map, type) - - def graphene_reducer(self, map, type): - if isinstance(type, (List, NonNull)): - return self.reducer(map, type.of_type) - if type._meta.name in map: - _type = map[type._meta.name] - if isinstance(_type, GrapheneGraphQLType): - assert _type.graphene_type == type, ( + if inspect.isfunction(type_): + type_ = type_() + if is_graphene_type(type_): + return self.graphene_reducer(map, type_) + return GraphQLTypeMap.reducer(map, type_) + + def graphene_reducer(self, map, type_): + if isinstance(type_, (List, NonNull)): + return self.reducer(map, type_.of_type) + if type_._meta.name in map: + type_from_map = map[type_._meta.name] + if isinstance(type_from_map, GrapheneGraphQLType): + assert type_from_map.graphene_type == type_, ( 'Found different types with the same name in the schema: {}, {}.' - ).format(_type.graphene_type, type) + ).format(type_from_map.graphene_type, type_) return map - if issubclass(type, ObjectType): - internal_type = self.construct_objecttype(map, type) - elif issubclass(type, InputObjectType): - internal_type = self.construct_inputobjecttype(map, type) - elif issubclass(type, Interface): - internal_type = self.construct_interface(map, type) - elif issubclass(type, Scalar): - internal_type = self.construct_scalar(map, type) - elif issubclass(type, Enum): - internal_type = self.construct_enum(map, type) - elif issubclass(type, Union): - internal_type = self.construct_union(map, type) + if issubclass(type_, ObjectType): + internal_type = self.construct_objecttype(map, type_) + elif issubclass(type_, InputObjectType): + internal_type = self.construct_inputobjecttype(map, type_) + elif issubclass(type_, Interface): + internal_type = self.construct_interface(map, type_) + elif issubclass(type_, Scalar): + internal_type = self.construct_scalar(map, type_) + elif issubclass(type_, Enum): + internal_type = self.construct_enum(map, type_) + elif issubclass(type_, Union): + internal_type = self.construct_union(map, type_) else: raise Exception( - "Expected Graphene type, but received: {}.".format(type)) + "Expected Graphene type, but received: {}.".format(type_)) return GraphQLTypeMap.reducer(map, internal_type) - def construct_scalar(self, map, type): + def construct_scalar(self, map, type_): # We have a mapping to the original GraphQL types # so there are no collisions. _scalars = { @@ -116,27 +116,27 @@ def construct_scalar(self, map, type): Boolean: GraphQLBoolean, ID: GraphQLID } - if type in _scalars: - return _scalars[type] + if type_ in _scalars: + return _scalars[type_] return GrapheneScalarType( - graphene_type=type, - name=type._meta.name, - description=type._meta.description, - serialize=getattr(type, 'serialize', None), - parse_value=getattr(type, 'parse_value', None), - parse_literal=getattr(type, 'parse_literal', None), ) - - def construct_enum(self, map, type): + graphene_type=type_, + name=type_._meta.name, + description=type_._meta.description, + serialize=getattr(type_, 'serialize', None), + parse_value=getattr(type_, 'parse_value', None), + parse_literal=getattr(type_, 'parse_literal', None), ) + + def construct_enum(self, map, type_): values = OrderedDict() - for name, value in type._meta.enum.__members__.items(): + for name, value in type_._meta.enum.__members__.items(): description = getattr(value, 'description', None) deprecation_reason = getattr(value, 'deprecation_reason', None) - if not description and callable(type._meta.description): - description = type._meta.description(value) + if not description and callable(type_._meta.description): + description = type_._meta.description(value) - if not deprecation_reason and callable(type._meta.deprecation_reason): - deprecation_reason = type._meta.deprecation_reason(value) + if not deprecation_reason and callable(type_._meta.deprecation_reason): + deprecation_reason = type_._meta.deprecation_reason(value) values[name] = GraphQLEnumValue( name=name, @@ -144,85 +144,86 @@ def construct_enum(self, map, type): description=description, deprecation_reason=deprecation_reason) - type_description = type._meta.description(None) if callable(type._meta.description) else type._meta.description + type_description = type_._meta.description(None)\ + if callable(type_._meta.description) else type_._meta.description return GrapheneEnumType( - graphene_type=type, + graphene_type=type_, values=values, - name=type._meta.name, + name=type_._meta.name, description=type_description, ) - def construct_objecttype(self, map, type): - if type._meta.name in map: - _type = map[type._meta.name] - if isinstance(_type, GrapheneGraphQLType): - assert _type.graphene_type == type, ( + def construct_objecttype(self, map, type_): + if type_._meta.name in map: + type_from_map = map[type_._meta.name] + if isinstance(type_from_map, GrapheneGraphQLType): + assert type_from_map.graphene_type == type_, ( 'Found different types with the same name in the schema: {}, {}.' - ).format(_type.graphene_type, type) - return _type + ).format(type_from_map.graphene_type, type_) + return type_from_map def interfaces(): interfaces = [] - for interface in type._meta.interfaces: + for interface in type_._meta.interfaces: self.graphene_reducer(map, interface) internal_type = map[interface._meta.name] assert internal_type.graphene_type == interface interfaces.append(internal_type) return interfaces - if type._meta.possible_types: + if type_._meta.possible_types: is_type_of = partial(is_type_of_from_possible_types, - type._meta.possible_types) + type_._meta.possible_types) else: - is_type_of = type.is_type_of + is_type_of = type_.is_type_of return GrapheneObjectType( - graphene_type=type, - name=type._meta.name, - description=type._meta.description, - fields=partial(self.construct_fields_for_type, map, type), + graphene_type=type_, + name=type_._meta.name, + description=type_._meta.description, + fields=partial(self.construct_fields_for_type, map, type_), is_type_of=is_type_of, interfaces=interfaces) - def construct_interface(self, map, type): - if type._meta.name in map: - _type = map[type._meta.name] - if isinstance(_type, GrapheneInterfaceType): - assert _type.graphene_type == type, ( + def construct_interface(self, map, type_): + if type_._meta.name in map: + type_from_map = map[type_._meta.name] + if isinstance(type_from_map, GrapheneInterfaceType): + assert type_from_map.graphene_type == type_, ( 'Found different types with the same name in the schema: {}, {}.' - ).format(_type.graphene_type, type) - return _type + ).format(type_from_map.graphene_type, type_) + return type_from_map _resolve_type = None - if type.resolve_type: - _resolve_type = partial(resolve_type, type.resolve_type, map, - type._meta.name) + if type_.resolve_type: + _resolve_type = partial(resolve_type, type_.resolve_type, map, + type_._meta.name) return GrapheneInterfaceType( - graphene_type=type, - name=type._meta.name, - description=type._meta.description, - fields=partial(self.construct_fields_for_type, map, type), + graphene_type=type_, + name=type_._meta.name, + description=type_._meta.description, + fields=partial(self.construct_fields_for_type, map, type_), resolve_type=_resolve_type, ) - def construct_inputobjecttype(self, map, type): + def construct_inputobjecttype(self, map, type_): return GrapheneInputObjectType( - graphene_type=type, - name=type._meta.name, - description=type._meta.description, - container_type=type._meta.container, + graphene_type=type_, + name=type_._meta.name, + description=type_._meta.description, + container_type=type_._meta.container, fields=partial( - self.construct_fields_for_type, map, type, is_input_type=True), + self.construct_fields_for_type, map, type_, is_input_type=True), ) - def construct_union(self, map, type): + def construct_union(self, map, type_): _resolve_type = None - if type.resolve_type: - _resolve_type = partial(resolve_type, type.resolve_type, map, - type._meta.name) + if type_.resolve_type: + _resolve_type = partial(resolve_type, type_.resolve_type, map, + type_._meta.name) def types(): union_types = [] - for objecttype in type._meta.types: + for objecttype in type_._meta.types: self.graphene_reducer(map, objecttype) internal_type = map[objecttype._meta.name] assert internal_type.graphene_type == objecttype @@ -230,8 +231,8 @@ def types(): return union_types return GrapheneUnionType( - graphene_type=type, - name=type._meta.name, + graphene_type=type_, + name=type_._meta.name, types=types, resolve_type=_resolve_type, ) @@ -240,9 +241,9 @@ def get_name(self, name): return to_camel_case(name) return name - def construct_fields_for_type(self, map, type, is_input_type=False): + def construct_fields_for_type(self, map, type_, is_input_type=False): fields = OrderedDict() - for name, field in type._meta.fields.items(): + for name, field in type_._meta.fields.items(): if isinstance(field, Dynamic): field = get_field_as(field.get_type(self.schema), _as=Field) if not field: @@ -271,7 +272,7 @@ def construct_fields_for_type(self, map, type, is_input_type=False): args=args, resolver=field.get_resolver( self.get_resolver_for_type( - type, + type_, name, field.default_value ) @@ -282,15 +283,15 @@ def construct_fields_for_type(self, map, type, is_input_type=False): fields[field_name] = _field return fields - def get_resolver_for_type(self, type, name, default_value): - if not issubclass(type, ObjectType): + def get_resolver_for_type(self, type_, name, default_value): + if not issubclass(type_, ObjectType): return - resolver = getattr(type, 'resolve_{}'.format(name), None) + resolver = getattr(type_, 'resolve_{}'.format(name), None) if not resolver: # If we don't find the resolver in the ObjectType class, then try to # find it in each of the interfaces interface_resolver = None - for interface in type._meta.interfaces: + for interface in type_._meta.interfaces: if name not in interface._meta.fields: continue interface_resolver = getattr(interface, @@ -303,13 +304,13 @@ def get_resolver_for_type(self, type, name, default_value): if resolver: return get_unbound_function(resolver) - default_resolver = type._meta.default_resolver or get_default_resolver( + default_resolver = type_._meta.default_resolver or get_default_resolver( ) return partial(default_resolver, name, default_value) - def get_field_type(self, map, type): - if isinstance(type, List): - return GraphQLList(self.get_field_type(map, type.of_type)) - if isinstance(type, NonNull): - return GraphQLNonNull(self.get_field_type(map, type.of_type)) - return map.get(type._meta.name) + def get_field_type(self, map, type_): + if isinstance(type_, List): + return GraphQLList(self.get_field_type(map, type_.of_type)) + if isinstance(type_, NonNull): + return GraphQLNonNull(self.get_field_type(map, type_.of_type)) + return map.get(type_._meta.name)