diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 55b10e2..eb1febf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: exclude: (migrations/|locale/|docs/) args: - --line-length=120 -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/pycqa/flake8 rev: 3.9.2 hooks: - id: flake8 diff --git a/graphene_django_cud/converter.py b/graphene_django_cud/converter.py index ed5df0b..7fa1fbc 100644 --- a/graphene_django_cud/converter.py +++ b/graphene_django_cud/converter.py @@ -8,7 +8,6 @@ # # From the last point, users of this module are expected to discard any field returning None from functools import singledispatch -from typing import Union import graphql from django.db import models @@ -31,7 +30,6 @@ Dynamic, Decimal, ) -from graphene.types import enum from graphene.types.json import JSONString from graphene_django.compat import ArrayField, HStoreField, RangeField from graphene_file_upload.scalars import Upload @@ -119,12 +117,12 @@ def description(self): def convert_django_field_with_choices( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): choices = getattr(field, "choices", None) @@ -160,9 +158,11 @@ def convert_django_field_with_choices( else: # Fetch the actual converted Choices class. We have to do this with a slightly shady usage of # the protected "_of_type" property of the NonNull type. - UnderlyingEnumCls = existing_conversion_in_registry.type._of_type if isinstance( # noqa - existing_conversion_in_registry.type, - NonNull) else existing_conversion_in_registry.type + UnderlyingEnumCls = ( + existing_conversion_in_registry.type._of_type + if isinstance(existing_conversion_in_registry.type, NonNull) # noqa + else existing_conversion_in_registry.type + ) # Return the converted field with the correct description and required value. return UnderlyingEnumCls(description=field.help_text, required=is_required(field, required)) @@ -179,12 +179,12 @@ def convert_django_field_with_choices( @singledispatch def convert_django_field_to_input( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): raise Exception("Don't know how to convert the Django field %s (%s)" % (field, field.__class__)) @@ -197,12 +197,12 @@ def convert_django_field_to_input( @convert_django_field_to_input.register(models.GenericIPAddressField) @convert_django_field_to_input.register(models.FilePathField) def convert_field_to_string_extended( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return String(description=field.help_text, required=is_required(field, required)) @@ -210,12 +210,12 @@ def convert_field_to_string_extended( @convert_django_field_to_input.register(models.OneToOneField) @convert_django_field_to_input.register(models.OneToOneRel) def convert_one_to_one_field( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): type_name = field_one_to_one_extras.get("type", "ID") if field_one_to_one_extras else "ID" if type_name == "ID": @@ -242,12 +242,12 @@ def dynamic_type(): @convert_django_field_to_input.register(models.AutoField) @convert_django_field_to_input.register(models.ForeignKey) def convert_field_to_id( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): # Call getattr here, as OneToOneRel does not carry the attribute whatsoeever. id_type = ID( @@ -274,12 +274,12 @@ def dynamic_type(): @convert_django_field_to_input.register(models.UUIDField) def convert_field_to_uuid( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return UUID(description=field.help_text, required=is_required(field, required)) @@ -290,24 +290,24 @@ def convert_field_to_uuid( @convert_django_field_to_input.register(models.BigIntegerField) @convert_django_field_to_input.register(models.IntegerField) def convert_field_to_int( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Int(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(models.BooleanField) def convert_field_to_boolean( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): if is_required(field, required): return NonNull(Boolean, description=field.help_text) @@ -318,60 +318,60 @@ def convert_field_to_boolean( @convert_django_field_to_input.register(models.NullBooleanField) def convert_field_to_nullboolean( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Boolean(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(models.FloatField) def convert_field_to_float( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Float(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(models.DecimalField) def convert_field_to_decimal( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Decimal(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(models.DurationField) def convert_field_to_time_delta( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return TimeDelta(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(models.DateTimeField) def convert_datetime_to_string( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): # We only render DateTimeFields with auto_now[_add] if they are explicitly required or not if required is None and (getattr(field, "auto_now", None) or getattr(field, "auto_now_add", None)): @@ -382,12 +382,12 @@ def convert_datetime_to_string( @convert_django_field_to_input.register(models.DateField) def convert_date_to_string( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): # We only render DateFields with auto_now[_add] if they are explicitly required or not if required is None and (getattr(field, "auto_now", None) or getattr(field, "auto_now_add", None)): @@ -398,12 +398,12 @@ def convert_date_to_string( @convert_django_field_to_input.register(models.TimeField) def convert_time_to_string( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Time(description=field.help_text, required=is_required(field, required)) @@ -412,12 +412,12 @@ def convert_time_to_string( @convert_django_field_to_input.register(models.ManyToManyRel) @convert_django_field_to_input.register(models.ManyToOneRel) def convert_many_to_many_field( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): # Use getattr on help_text here as ManyToOnRel does not possess this. list_id_type = List( @@ -448,12 +448,12 @@ def dynamic_type(): @convert_django_field_to_input.register(ArrayField) def convert_postgres_array_to_list( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): base_type = convert_django_field_to_input(field.base_field) if not isinstance(base_type, (List, NonNull)): @@ -464,24 +464,24 @@ def convert_postgres_array_to_list( @convert_django_field_to_input.register(HStoreField) @convert_django_field_to_input.register(models.JSONField) def convert_posgres_field_to_string( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return JSONString(description=field.help_text, required=is_required(field, required)) @convert_django_field_to_input.register(RangeField) def convert_postgres_range_to_string( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): inner_type = convert_django_field_to_input(field.base_field) if not isinstance(inner_type, (List, NonNull)): @@ -492,11 +492,11 @@ def convert_postgres_range_to_string( @convert_django_field_to_input.register(FileField) @convert_django_field_to_input.register(ImageField) def convert_file_field_to_upload( - field, - registry=None, - required=None, - field_many_to_many_extras=None, - field_foreign_key_extras=None, - field_one_to_one_extras=None, + field, + registry=None, + required=None, + field_many_to_many_extras=None, + field_foreign_key_extras=None, + field_one_to_one_extras=None, ): return Upload(required=is_required(field, required)) diff --git a/graphene_django_cud/mutations/batch_create.py b/graphene_django_cud/mutations/batch_create.py index 4aa7929..8424081 100644 --- a/graphene_django_cud/mutations/batch_create.py +++ b/graphene_django_cud/mutations/batch_create.py @@ -36,7 +36,7 @@ def __init_subclass_with_meta__( exclude_fields=(), # Deprecated in favor of `exclude` optional_fields=(), required_fields=(), - auto_context_fields=None, + auto_context_fields=None, return_field_name=None, many_to_many_extras=None, foreign_key_extras=None, diff --git a/graphene_django_cud/mutations/batch_delete.py b/graphene_django_cud/mutations/batch_delete.py index 8eed8c6..0294fba 100644 --- a/graphene_django_cud/mutations/batch_delete.py +++ b/graphene_django_cud/mutations/batch_delete.py @@ -109,7 +109,7 @@ def mutate(cls, root, info, ids): cls.check_permissions(root, info, ids) - Model = cls._meta.model + Model = cls._meta.model # noqa ids = cls.resolve_ids(ids) cls.validate(root, info, ids) @@ -126,7 +126,6 @@ def mutate(cls, root, info, ids): all_global_ids = [cls.get_return_id(id) for id in ids] - missed_ids = list(set(all_global_ids).difference(deleted_ids)) deletion_count, _ = qs_to_delete.delete() diff --git a/graphene_django_cud/mutations/batch_patch.py b/graphene_django_cud/mutations/batch_patch.py index f620f32..03894de 100644 --- a/graphene_django_cud/mutations/batch_patch.py +++ b/graphene_django_cud/mutations/batch_patch.py @@ -17,8 +17,8 @@ def __init_subclass_with_meta__( cls, _meta=None, model=None, - optional_fields=None, - required_fields=None, + optional_fields=None, + required_fields=None, type_name=None, **kwargs, ): diff --git a/graphene_django_cud/mutations/batch_update.py b/graphene_django_cud/mutations/batch_update.py index 3a01bef..b3b388c 100644 --- a/graphene_django_cud/mutations/batch_update.py +++ b/graphene_django_cud/mutations/batch_update.py @@ -36,7 +36,7 @@ def __init_subclass_with_meta__( exclude_fields=(), # Deprecated in favor of `exclude` optional_fields=(), required_fields=(), - auto_context_fields=None, + auto_context_fields=None, return_field_name=None, many_to_many_extras=None, foreign_key_extras=None, diff --git a/graphene_django_cud/mutations/create.py b/graphene_django_cud/mutations/create.py index 9e2f6c6..f4898bb 100644 --- a/graphene_django_cud/mutations/create.py +++ b/graphene_django_cud/mutations/create.py @@ -36,7 +36,7 @@ def __init_subclass_with_meta__( exclude_fields=(), # Deprecated in favor of `exclude` optional_fields=(), required_fields=(), - auto_context_fields=None, + auto_context_fields=None, return_field_name=None, many_to_many_extras=None, foreign_key_extras=None, diff --git a/graphene_django_cud/mutations/filter_update.py b/graphene_django_cud/mutations/filter_update.py index f5326ac..9fc7897 100644 --- a/graphene_django_cud/mutations/filter_update.py +++ b/graphene_django_cud/mutations/filter_update.py @@ -45,10 +45,10 @@ def __init_subclass_with_meta__( only_fields=(), # Deprecated in favor of `fields` exclude=(), exclude_fields=(), # Deprecated in favor of `exclude` - optional_fields=None, # Explicitly defaulted to None here and handled below. + optional_fields=None, # Explicitly defaulted to None here and handled below. required_fields=(), field_types=None, - auto_context_fields=None, + auto_context_fields=None, **kwargs, ): diff --git a/graphene_django_cud/mutations/patch.py b/graphene_django_cud/mutations/patch.py index a51db20..ccce273 100644 --- a/graphene_django_cud/mutations/patch.py +++ b/graphene_django_cud/mutations/patch.py @@ -17,8 +17,8 @@ def __init_subclass_with_meta__( cls, _meta=None, model=None, - optional_fields=None, - required_fields=None, + optional_fields=None, + required_fields=None, type_name=None, **kwargs, ): diff --git a/graphene_django_cud/tests/test_create_mutation.py b/graphene_django_cud/tests/test_create_mutation.py index 0cb5795..7a4acae 100644 --- a/graphene_django_cud/tests/test_create_mutation.py +++ b/graphene_django_cud/tests/test_create_mutation.py @@ -750,7 +750,7 @@ class Meta: class Mutations(graphene.ObjectType): create_fish = CreateFishMutation.Field() - + user = UserFactory.create() fish = FishFactory.build() @@ -770,11 +770,7 @@ class Mutations(graphene.ObjectType): result = schema.execute( mutation, - variables={ - "input": { - "name": fish.name - } - }, + variables={"input": {"name": fish.name}}, context=Dict(user=user), ) self.assertIsNone(result.errors) diff --git a/graphene_django_cud/tests/test_delete_mutation.py b/graphene_django_cud/tests/test_delete_mutation.py index 1ca2347..213d4ac 100644 --- a/graphene_django_cud/tests/test_delete_mutation.py +++ b/graphene_django_cud/tests/test_delete_mutation.py @@ -160,9 +160,7 @@ class Mutations(graphene.ObjectType): # real-world mutation scenarios. result = schema.execute( mutation, - variables={ - "id": str(fish.id) - }, + variables={"id": str(fish.id)}, context=Dict(user=user), ) self.assertIsNone(result.errors) diff --git a/graphene_django_cud/tests/test_update_mutation.py b/graphene_django_cud/tests/test_update_mutation.py index 46cc0bc..685d268 100644 --- a/graphene_django_cud/tests/test_update_mutation.py +++ b/graphene_django_cud/tests/test_update_mutation.py @@ -518,12 +518,7 @@ class Mutations(graphene.ObjectType): # real-world mutation scenarios. result = schema.execute( mutation, - variables={ - "id": str(fish.id), - "input": { - "name": "Fugu" - } - }, + variables={"id": str(fish.id), "input": {"name": "Fugu"}}, context=Dict(user=user), ) self.assertIsNone(result.errors)