From 9e17044ddca17bc5d93f8c670d85030613008fcc Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Fri, 5 Nov 2021 02:21:14 +0100 Subject: [PATCH 1/2] Chore: Refactor Multi Expression Code --- graphene/pyutils/version.py | 5 +--- graphene/relay/node.py | 10 ++------ graphene/types/mutation.py | 5 +--- graphene/types/resolver.py | 4 +-- graphene/types/tests/test_subscribe_async.py | 4 +-- graphene/utils/module_loading.py | 25 +++++++++---------- graphene/utils/tests/test_orderedtype.py | 2 +- .../tests/test_disable_introspection.py | 4 +-- 8 files changed, 20 insertions(+), 39 deletions(-) diff --git a/graphene/pyutils/version.py b/graphene/pyutils/version.py index f2005442..8a3be07a 100644 --- a/graphene/pyutils/version.py +++ b/graphene/pyutils/version.py @@ -19,10 +19,7 @@ def get_version(version=None): sub = "" if version[3] == "alpha" and version[4] == 0: git_changeset = get_git_changeset() - if git_changeset: - sub = ".dev%s" % git_changeset - else: - sub = ".dev" + sub = ".dev%s" % git_changeset if git_changeset else ".dev" elif version[3] != "final": mapping = {"alpha": "a", "beta": "b", "rc": "rc"} sub = mapping[version[3]] + str(version[4]) diff --git a/graphene/relay/node.py b/graphene/relay/node.py index 8defefff..2a506aa2 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -18,11 +18,7 @@ def is_node(objecttype): if not issubclass(objecttype, ObjectType): return False - for i in objecttype._meta.interfaces: - if issubclass(i, Node): - return True - - return False + return any(issubclass(i, Node) for i in objecttype._meta.interfaces) class GlobalID(Field): @@ -92,9 +88,7 @@ class Node(AbstractNode): _type, _id = cls.from_global_id(global_id) except Exception as e: raise Exception( - f'Unable to parse global ID "{global_id}". ' - 'Make sure it is a base64 encoded string in the format: "TypeName:id". ' - f"Exception message: {str(e)}" + f'Unable to parse global ID "{global_id}". Make sure it is a base64 encoded string in the format: "TypeName:id". Exception message: {e}' ) graphene_type = info.schema.get_type(_type) diff --git a/graphene/types/mutation.py b/graphene/types/mutation.py index ca87775a..06fc3567 100644 --- a/graphene/types/mutation.py +++ b/graphene/types/mutation.py @@ -101,10 +101,7 @@ class Mutation(ObjectType): "Read more:" " https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#mutation-input" ) - if input_class: - arguments = props(input_class) - else: - arguments = {} + arguments = props(input_class) if input_class else {} if not resolver: mutate = getattr(cls, "mutate", None) assert mutate, "All mutations must define a mutate method in it" diff --git a/graphene/types/resolver.py b/graphene/types/resolver.py index 6a8ea02b..72d2edb8 100644 --- a/graphene/types/resolver.py +++ b/graphene/types/resolver.py @@ -7,9 +7,7 @@ def dict_resolver(attname, default_value, root, info, **args): def dict_or_attr_resolver(attname, default_value, root, info, **args): - resolver = attr_resolver - if isinstance(root, dict): - resolver = dict_resolver + resolver = dict_resolver if isinstance(root, dict) else attr_resolver return resolver(attname, default_value, root, info, **args) diff --git a/graphene/types/tests/test_subscribe_async.py b/graphene/types/tests/test_subscribe_async.py index 9b7a1f13..50e5ba68 100644 --- a/graphene/types/tests/test_subscribe_async.py +++ b/graphene/types/tests/test_subscribe_async.py @@ -14,9 +14,7 @@ class Subscription(ObjectType): count_to_ten = Field(Int) async def subscribe_count_to_ten(root, info): - count = 0 - while count < 10: - count += 1 + for count in range(1, 11): yield count diff --git a/graphene/utils/module_loading.py b/graphene/utils/module_loading.py index 25dc86ca..d9095d0a 100644 --- a/graphene/utils/module_loading.py +++ b/graphene/utils/module_loading.py @@ -27,19 +27,18 @@ def import_string(dotted_path, dotted_attributes=None): if not dotted_attributes: return result - else: - attributes = dotted_attributes.split(".") - traveled_attributes = [] - try: - for attribute in attributes: - traveled_attributes.append(attribute) - result = getattr(result, attribute) - return result - except AttributeError: - raise ImportError( - 'Module "%s" does not define a "%s" attribute inside attribute/class "%s"' - % (module_path, ".".join(traveled_attributes), class_name) - ) + attributes = dotted_attributes.split(".") + traveled_attributes = [] + try: + for attribute in attributes: + traveled_attributes.append(attribute) + result = getattr(result, attribute) + return result + except AttributeError: + raise ImportError( + 'Module "%s" does not define a "%s" attribute inside attribute/class "%s"' + % (module_path, ".".join(traveled_attributes), class_name) + ) def lazy_import(dotted_path, dotted_attributes=None): diff --git a/graphene/utils/tests/test_orderedtype.py b/graphene/utils/tests/test_orderedtype.py index ea6c7cc0..ad5bd77a 100644 --- a/graphene/utils/tests/test_orderedtype.py +++ b/graphene/utils/tests/test_orderedtype.py @@ -38,4 +38,4 @@ def test_orderedtype_non_orderabletypes(): assert one.__lt__(1) == NotImplemented assert one.__gt__(1) == NotImplemented - assert not one == 1 + assert one != 1 diff --git a/graphene/validation/tests/test_disable_introspection.py b/graphene/validation/tests/test_disable_introspection.py index 958a1afa..149ac628 100644 --- a/graphene/validation/tests/test_disable_introspection.py +++ b/graphene/validation/tests/test_disable_introspection.py @@ -18,14 +18,12 @@ schema = Schema(query=Query) def run_query(query: str): document = parse(query) - errors = validate( + return validate( schema=schema.graphql_schema, document_ast=document, rules=(DisableIntrospection,), ) - return errors - def test_disallows_introspection_queries(): errors = run_query("{ __schema { queryType { name } } }") From 7108bc857736dc85f9b4db3fec5d675fd094b61f Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Thu, 2 Dec 2021 12:04:07 +0100 Subject: [PATCH 2/2] Update node.py --- graphene/relay/node.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphene/relay/node.py b/graphene/relay/node.py index 2a506aa2..c1316d22 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -88,7 +88,9 @@ class Node(AbstractNode): _type, _id = cls.from_global_id(global_id) except Exception as e: raise Exception( - f'Unable to parse global ID "{global_id}". Make sure it is a base64 encoded string in the format: "TypeName:id". Exception message: {e}' + f'Unable to parse global ID "{global_id}". ' + 'Make sure it is a base64 encoded string in the format: "TypeName:id". ' + f"Exception message: {e}" ) graphene_type = info.schema.get_type(_type)