diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b6bb6ee3..669da376 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,7 @@ - id: pretty-format-json args: - --autofix +- repo: https://github.com/asottile/pyupgrade + sha: v1.1.1 + hooks: + - id: pyupgrade diff --git a/UPGRADE-v2.0.md b/UPGRADE-v2.0.md index 96aac143..5dc76154 100644 --- a/UPGRADE-v2.0.md +++ b/UPGRADE-v2.0.md @@ -207,7 +207,7 @@ Before: ```python class SomeMutation(Mutation): ... - + @classmethod def mutate(cls, instance, args, context, info): ... @@ -218,7 +218,7 @@ With 2.0: ```python class SomeMutation(Mutation): ... - + def mutate(self, info, **args): ... ``` @@ -231,7 +231,7 @@ class SomeMutation(Mutation): first_name = String(required=True) last_name = String(required=True) ... - + def mutate(self, info, first_name, last_name): ... ``` @@ -250,7 +250,7 @@ If you are using Middelwares, you need to some adjustments: Before: ```python -class MyGrapheneMiddleware(object): +class MyGrapheneMiddleware(object): def resolve(self, next_mw, root, args, context, info): ## Middleware code @@ -261,7 +261,7 @@ class MyGrapheneMiddleware(object): With 2.0: ```python -class MyGrapheneMiddleware(object): +class MyGrapheneMiddleware(object): def resolve(self, next_mw, root, info, **args): context = info.context diff --git a/graphene/pyutils/enum.py b/graphene/pyutils/enum.py index 076b6a9d..f3421d9f 100644 --- a/graphene/pyutils/enum.py +++ b/graphene/pyutils/enum.py @@ -170,7 +170,7 @@ class EnumMeta(type): first_enum) # save enum items into separate mapping so they don't get baked into # the new class - members = dict((k, classdict[k]) for k in classdict._member_names) + members = {k: classdict[k] for k in classdict._member_names} for name in classdict._member_names: del classdict[name] @@ -192,14 +192,14 @@ class EnumMeta(type): _order_ += aliases # check for illegal enum names (any others?) - invalid_names = set(members) & set(['mro']) + invalid_names = set(members) & {'mro'} if invalid_names: raise ValueError('Invalid enum member name(s): %s' % ( ', '.join(invalid_names), )) # save attributes from super classes so we know if we can take # the shortcut of storing members in the class dict - base_attributes = set([a for b in bases for a in b.__dict__]) + base_attributes = {a for b in bases for a in b.__dict__} # create our new Enum type enum_class = super(EnumMeta, metacls).__new__(metacls, cls, bases, classdict) enum_class._member_names_ = [] # names in random order @@ -831,7 +831,7 @@ def _convert(cls, name, module, filter, source=None): source = vars(source) else: source = module_globals - members = dict((name, value) for name, value in source.items() if filter(name)) + members = {name: value for name, value in source.items() if filter(name)} cls = cls(name, members, module=module) cls.__reduce_ex__ = _reduce_ex_by_name module_globals.update(cls.__members__) diff --git a/graphene/pyutils/signature.py b/graphene/pyutils/signature.py index 9d94a6ef..41e06624 100644 --- a/graphene/pyutils/signature.py +++ b/graphene/pyutils/signature.py @@ -52,7 +52,7 @@ def signature(obj): '''Get a signature object for the passed callable.''' if not callable(obj): - raise TypeError('{0!r} is not a callable object'.format(obj)) + raise TypeError('{!r} is not a callable object'.format(obj)) if isinstance(obj, types.MethodType): sig = signature(obj.__func__) @@ -99,7 +99,7 @@ def signature(obj): try: ba = sig.bind_partial(*partial_args, **partial_keywords) except TypeError as ex: - msg = 'partial object {0!r} has incorrect arguments'.format(obj) + msg = 'partial object {!r} has incorrect arguments'.format(obj) raise ValueError(msg) for arg_name, arg_value in ba.arguments.items(): @@ -166,10 +166,10 @@ def signature(obj): if isinstance(obj, types.BuiltinFunctionType): # Raise a nicer error message for builtins - msg = 'no signature found for builtin function {0!r}'.format(obj) + msg = 'no signature found for builtin function {!r}'.format(obj) raise ValueError(msg) - raise ValueError('callable {0!r} is not supported by signature'.format(obj)) + raise ValueError('callable {!r} is not supported by signature'.format(obj)) class _void(object): @@ -190,7 +190,7 @@ class _ParameterKind(int): return self._name def __repr__(self): - return '<_ParameterKind: {0!r}>'.format(self._name) + return '<_ParameterKind: {!r}>'.format(self._name) _POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY') @@ -238,7 +238,7 @@ class Parameter(object): if default is not _empty: if kind in (_VAR_POSITIONAL, _VAR_KEYWORD): - msg = '{0} parameters cannot have default values'.format(kind) + msg = '{} parameters cannot have default values'.format(kind) raise ValueError(msg) self._default = default self._annotation = annotation @@ -251,7 +251,7 @@ class Parameter(object): else: name = str(name) if kind != _POSITIONAL_ONLY and not re.match(r'[a-z_]\w*$', name, re.I): - msg = '{0!r} is not a valid parameter name'.format(name) + msg = '{!r} is not a valid parameter name'.format(name) raise ValueError(msg) self._name = name @@ -302,15 +302,15 @@ class Parameter(object): if kind == _POSITIONAL_ONLY: if formatted is None: formatted = '' - formatted = '<{0}>'.format(formatted) + formatted = '<{}>'.format(formatted) # Add annotation and default value if self._annotation is not _empty: - formatted = '{0}:{1}'.format(formatted, + formatted = '{}:{}'.format(formatted, formatannotation(self._annotation)) if self._default is not _empty: - formatted = '{0}={1}'.format(formatted, repr(self._default)) + formatted = '{}={}'.format(formatted, repr(self._default)) if kind == _VAR_POSITIONAL: formatted = '*' + formatted @@ -320,11 +320,11 @@ class Parameter(object): return formatted def __repr__(self): - return '<{0} at {1:#x} {2!r}>'.format(self.__class__.__name__, + return '<{} at {:#x} {!r}>'.format(self.__class__.__name__, id(self), self.name) def __hash__(self): - msg = "unhashable type: '{0}'".format(self.__class__.__name__) + msg = "unhashable type: '{}'".format(self.__class__.__name__) raise TypeError(msg) def __eq__(self, other): @@ -421,7 +421,7 @@ class BoundArguments(object): return kwargs def __hash__(self): - msg = "unhashable type: '{0}'".format(self.__class__.__name__) + msg = "unhashable type: '{}'".format(self.__class__.__name__) raise TypeError(msg) def __eq__(self, other): @@ -489,7 +489,7 @@ class Signature(object): param = param.replace(name=name) if name in params: - msg = 'duplicate parameter name: {0!r}'.format(name) + msg = 'duplicate parameter name: {!r}'.format(name) raise ValueError(msg) params[name] = param else: @@ -504,7 +504,7 @@ class Signature(object): '''Constructs Signature for the given python function''' if not isinstance(func, types.FunctionType): - raise TypeError('{0!r} is not a Python function'.format(func)) + raise TypeError('{!r} is not a Python function'.format(func)) Parameter = cls._parameter_cls @@ -599,7 +599,7 @@ class Signature(object): return_annotation=return_annotation) def __hash__(self): - msg = "unhashable type: '{0}'".format(self.__class__.__name__) + msg = "unhashable type: '{}'".format(self.__class__.__name__) raise TypeError(msg) def __eq__(self, other): @@ -608,8 +608,8 @@ class Signature(object): len(self.parameters) != len(other.parameters)): return False - other_positions = dict((param, idx) - for idx, param in enumerate(other.parameters.keys())) + other_positions = {param: idx + for idx, param in enumerate(other.parameters.keys())} for idx, (param_name, param) in enumerate(self.parameters.items()): if param.kind == _KEYWORD_ONLY: @@ -799,10 +799,10 @@ class Signature(object): result.append(formatted) - rendered = '({0})'.format(', '.join(result)) + rendered = '({})'.format(', '.join(result)) if self.return_annotation is not _empty: anno = formatannotation(self.return_annotation) - rendered += ' -> {0}'.format(anno) + rendered += ' -> {}'.format(anno) return rendered diff --git a/graphene/types/base.py b/graphene/types/base.py index 50242674..3bbd42d1 100644 --- a/graphene/types/base.py +++ b/graphene/types/base.py @@ -18,7 +18,7 @@ class BaseOptions(object): if not self._frozen: super(BaseOptions, self).__setattr__(name, value) else: - raise Exception("Can't modify frozen Options {0}".format(self)) + raise Exception("Can't modify frozen Options {}".format(self)) def __repr__(self): return "<{} name={}>".format(self.__class__.__name__, repr(self.name))