Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff in pre-commit #1441

Merged
merged 15 commits into from
Aug 5, 2023
14 changes: 5 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ repos:
- --autofix
- id: trailing-whitespace
exclude: README.md
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.2
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.282
hooks:
- id: flake8
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
33 changes: 33 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
select = [
"E", # pycodestyle
"W", # pycodestyle
"F", # pyflake
"I", # isort
"B", # flake8-bugbear
kiendang marked this conversation as resolved.
Show resolved Hide resolved
"C4", # flake8-comprehensions
"UP", # pyupgrade
kiendang marked this conversation as resolved.
Show resolved Hide resolved
]

ignore = [
"E501", # line-too-long
"B017", # pytest.raises(Exception) should be considered evil
"B028", # warnings.warn called without an explicit stacklevel keyword argument
"B904", # check for raise statements in exception handlers that lack a from clause
]

exclude = [
"**/docs",
]

target-version = "py38"

[per-file-ignores]
# Ignore unused imports (F401) in these files
"__init__.py" = ["F401"]
"graphene_django/compat.py" = ["F401"]

[isort]
kiendang marked this conversation as resolved.
Show resolved Hide resolved
known-first-party = ["graphene", "graphene-django"]
known-local-folder = ["cookbook"]
force-wrap-aliases = true
combine-as-imports = true
6 changes: 3 additions & 3 deletions examples/cookbook-plain/cookbook/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cookbook.ingredients.schema
import cookbook.recipes.schema
import graphene

from graphene_django.debug import DjangoDebug

import cookbook.ingredients.schema
import cookbook.recipes.schema


class Query(
cookbook.ingredients.schema.Query,
Expand Down
3 changes: 1 addition & 2 deletions examples/cookbook-plain/cookbook/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path
from django.contrib import admin
from django.urls import path

from graphene_django.views import GraphQLView


urlpatterns = [
path("admin/", admin.site.urls),
path("graphql/", GraphQLView.as_view(graphiql=True)),
Expand Down
3 changes: 2 additions & 1 deletion examples/cookbook/cookbook/ingredients/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from cookbook.ingredients.models import Category, Ingredient
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType

from cookbook.ingredients.models import Category, Ingredient


# Graphene will automatically map the Category model's fields onto the CategoryNode.
# This is configured in the CategoryNode's Meta class (as you can see below)
Expand Down
4 changes: 3 additions & 1 deletion examples/cookbook/cookbook/recipes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
class Recipe(models.Model):
title = models.CharField(max_length=100)
instructions = models.TextField()
__unicode__ = lambda self: self.title

def __unicode__(self):
return self.title


class RecipeIngredient(models.Model):
Expand Down
3 changes: 2 additions & 1 deletion examples/cookbook/cookbook/recipes/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from cookbook.recipes.models import Recipe, RecipeIngredient
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType

from cookbook.recipes.models import Recipe, RecipeIngredient


class RecipeNode(DjangoObjectType):
class Meta:
Expand Down
6 changes: 3 additions & 3 deletions examples/cookbook/cookbook/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cookbook.ingredients.schema
import cookbook.recipes.schema
import graphene

from graphene_django.debug import DjangoDebug

import cookbook.ingredients.schema
import cookbook.recipes.schema


class Query(
cookbook.ingredients.schema.Query,
Expand Down
1 change: 0 additions & 1 deletion examples/cookbook/cookbook/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from graphene_django.views import GraphQLView


urlpatterns = [
url(r"^admin/", admin.site.urls),
url(r"^graphql$", GraphQLView.as_view(graphiql=True)),
Expand Down
2 changes: 1 addition & 1 deletion examples/django_test_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import os
import sys

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, ROOT_PATH + "/examples/")
Expand Down
8 changes: 5 additions & 3 deletions examples/starwars/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from graphene_django import DjangoConnectionField, DjangoObjectType

from .data import create_ship, get_empire, get_faction, get_rebels, get_ship, get_ships
from .models import Character as CharacterModel
from .models import Faction as FactionModel
from .models import Ship as ShipModel
from .models import (
Character as CharacterModel,
Faction as FactionModel,
Ship as ShipModel,
)


class Ship(DjangoObjectType):
Expand Down
2 changes: 1 addition & 1 deletion graphene_django/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def __init__(self, *args, **kwargs):
# Postgres fields are only available in Django with psycopg2 installed
# and we cannot have psycopg2 on PyPy
from django.contrib.postgres.fields import (
IntegerRangeField,
ArrayField,
HStoreField,
IntegerRangeField,
RangeField,
)
except ImportError:
Expand Down
14 changes: 7 additions & 7 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from django.utils.encoding import force_str
from django.utils.functional import Promise
from django.utils.module_loading import import_string
from graphql import GraphQLError

from graphene import (
ID,
UUID,
Boolean,
Date,
DateTime,
Decimal,
Dynamic,
Enum,
Field,
Expand All @@ -22,13 +24,11 @@
NonNull,
String,
Time,
Decimal,
)
from graphene.types.json import JSONString
from graphene.types.scalars import BigInt
from graphene.types.resolver import get_default_resolver
from graphene.types.scalars import BigInt
from graphene.utils.str_converters import to_camel_case
from graphql import GraphQLError

try:
from graphql import assert_name
Expand All @@ -38,7 +38,7 @@
from graphql.pyutils import register_description

from .compat import ArrayField, HStoreField, RangeField
from .fields import DjangoListField, DjangoConnectionField
from .fields import DjangoConnectionField, DjangoListField
from .settings import graphene_settings
from .utils.str_converters import to_const

Expand Down Expand Up @@ -161,9 +161,7 @@ def get_django_field_description(field):
@singledispatch
def convert_django_field(field, registry=None):
raise Exception(
"Don't know how to convert the Django field {} ({})".format(
field, field.__class__
)
f"Don't know how to convert the Django field {field} ({field.__class__})"
)


Expand Down Expand Up @@ -261,6 +259,7 @@ def convert_time_to_string(field, registry=None):
@convert_django_field.register(models.OneToOneRel)
def convert_onetoone_field_to_djangomodel(field, registry=None):
from graphene.utils.str_converters import to_snake_case

from .types import DjangoObjectType

model = field.related_model
Expand Down Expand Up @@ -364,6 +363,7 @@ def dynamic_type():
@convert_django_field.register(models.ForeignKey)
def convert_field_to_djangomodel(field, registry=None):
from graphene.utils.str_converters import to_snake_case

from .types import DjangoObjectType

model = field.related_model
Expand Down
4 changes: 1 addition & 3 deletions graphene_django/debug/middleware.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.db import connections

from promise import Promise

from .sql.tracking import unwrap_cursor, wrap_cursor
from .exception.formating import wrap_exception
from .sql.tracking import unwrap_cursor, wrap_cursor
from .types import DjangoDebug


Expand Down
3 changes: 2 additions & 1 deletion graphene_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import graphene
import pytest

import graphene
from graphene.relay import Node
from graphene_django import DjangoConnectionField, DjangoObjectType

Expand Down
2 changes: 1 addition & 1 deletion graphene_django/debug/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from graphene import List, ObjectType

from .sql.types import DjangoDebugSQL
from .exception.types import DjangoDebugException
from .sql.types import DjangoDebugSQL


class DjangoDebug(ObjectType):
Expand Down
2 changes: 0 additions & 2 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from functools import partial

from django.db.models.query import QuerySet

from graphql_relay import (
connection_from_array_slice,
cursor_to_offset,
get_offset_with_default,
offset_to_cursor,
)

from promise import Promise

from graphene import Int, NonNull
Expand Down
1 change: 1 addition & 0 deletions graphene_django/filter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings

from ..utils import DJANGO_FILTER_INSTALLED

if not DJANGO_FILTER_INSTALLED:
Expand Down
4 changes: 2 additions & 2 deletions graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from django.core.exceptions import ValidationError

from graphene.types.enum import EnumType
from graphene.types.argument import to_arguments
from graphene.types.enum import EnumType
from graphene.utils.str_converters import to_snake_case

from ..fields import DjangoConnectionField
Expand Down Expand Up @@ -58,7 +58,7 @@ def args(self, args):
def filterset_class(self):
if not self._filterset_class:
fields = self._fields or self.node_type._meta.filter_fields
meta = dict(model=self.model, fields=fields)
meta = {"model": self.model, "fields": fields}
if self._extra_filter_meta:
meta.update(self._extra_filter_meta)

Expand Down
1 change: 1 addition & 0 deletions graphene_django/filter/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings

from ...utils import DJANGO_FILTER_INSTALLED

if not DJANGO_FILTER_INSTALLED:
Expand Down
1 change: 0 additions & 1 deletion graphene_django/filter/filters/global_id_filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django_filters import Filter, MultipleChoiceFilter

from graphql_relay.node.node import from_global_id

from ...forms import GlobalIDFormField, GlobalIDMultipleChoiceField
Expand Down
8 changes: 5 additions & 3 deletions graphene_django/filter/filterset.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import itertools

from django.db import models
from django_filters.filterset import BaseFilterSet, FilterSet
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
from django_filters.filterset import (
FILTER_FOR_DBFIELD_DEFAULTS,
BaseFilterSet,
FilterSet,
)

from .filters import GlobalIDFilter, GlobalIDMultipleChoiceFilter


GRAPHENE_FILTER_SET_OVERRIDES = {
models.AutoField: {"filter_class": GlobalIDFilter},
models.OneToOneField: {"filter_class": GlobalIDFilter},
Expand Down
6 changes: 3 additions & 3 deletions graphene_django/filter/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from unittest.mock import MagicMock
import pytest

import pytest
from django.db import models
from django.db.models.query import QuerySet
from django_filters import filters
from django_filters import FilterSet

import graphene
from graphene.relay import Node
from graphene_django import DjangoObjectType
from graphene_django.filter import ArrayFilter
from graphene_django.utils import DJANGO_FILTER_INSTALLED
from graphene_django.filter import ArrayFilter, ListFilter

from ...compat import ArrayField

Expand Down
3 changes: 1 addition & 2 deletions graphene_django/filter/tests/test_enum_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import graphene
from graphene.relay import Node

from graphene_django import DjangoObjectType, DjangoConnectionField
from graphene_django import DjangoConnectionField, DjangoObjectType
from graphene_django.tests.models import Article, Reporter
from graphene_django.utils import DJANGO_FILTER_INSTALLED

Expand Down
8 changes: 4 additions & 4 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from django_filters import FilterSet, NumberFilter, OrderingFilter

from graphene_django.filter import (
GlobalIDFilter,
DjangoFilterConnectionField,
GlobalIDFilter,
GlobalIDMultipleChoiceFilter,
)
from graphene_django.filter.tests.filters import (
Expand Down Expand Up @@ -222,7 +222,7 @@ class Query(ObjectType):
reporter = Field(ReporterFilterNode)
article = Field(ArticleFilterNode)

schema = Schema(query=Query)
Schema(query=Query)
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
assert_arguments(articles_field, "headline", "reporter")
assert_not_orderable(articles_field)
Expand Down Expand Up @@ -294,7 +294,7 @@ class Query(ObjectType):
reporter = Field(ReporterFilterNode)
article = Field(ArticleFilterNode)

schema = Schema(query=Query)
Schema(query=Query)
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
assert_arguments(articles_field, "headline", "reporter")
assert_not_orderable(articles_field)
Expand Down Expand Up @@ -1186,7 +1186,7 @@ class Query(ObjectType):
first_name="Adam", last_name="Doe", email="adam@doe.com"
)

article_2 = Article.objects.create(
Article.objects.create(
headline="Good Bye",
reporter=reporter_2,
editor=reporter_2,
Expand Down