Skip to content

Commit 9a773b9

Browse files
authored
Use ruff in pre-commit (#1441)
* Use ruff in pre-commit * Add pyupgrade * Add isort * Add bugbear * Fix B015 Pointless comparison * Fix B026 * B018 false positive * Remove flake8 and isort config from setup.cfg * Remove black and flake8 from dev dependencies * Update black * Show list of fixes applied with autofix on * Fix typo * Add C4 flake8-comprehensions * Add ruff to dev dependencies * Fix up
1 parent 45a732f commit 9a773b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+218
-246
lines changed

Diff for: .pre-commit-config.yaml

+5-9
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ repos:
1515
- --autofix
1616
- id: trailing-whitespace
1717
exclude: README.md
18-
- repo: https://github.com/asottile/pyupgrade
19-
rev: v3.3.2
20-
hooks:
21-
- id: pyupgrade
22-
args: [--py38-plus]
2318
- repo: https://github.com/psf/black
24-
rev: 23.3.0
19+
rev: 23.7.0
2520
hooks:
2621
- id: black
27-
- repo: https://github.com/PyCQA/flake8
28-
rev: 6.0.0
22+
- repo: https://github.com/astral-sh/ruff-pre-commit
23+
rev: v0.0.282
2924
hooks:
30-
- id: flake8
25+
- id: ruff
26+
args: [--fix, --exit-non-zero-on-fix, --show-fixes]

Diff for: .ruff.toml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
select = [
2+
"E", # pycodestyle
3+
"W", # pycodestyle
4+
"F", # pyflake
5+
"I", # isort
6+
"B", # flake8-bugbear
7+
"C4", # flake8-comprehensions
8+
"UP", # pyupgrade
9+
]
10+
11+
ignore = [
12+
"E501", # line-too-long
13+
"B017", # pytest.raises(Exception) should be considered evil
14+
"B028", # warnings.warn called without an explicit stacklevel keyword argument
15+
"B904", # check for raise statements in exception handlers that lack a from clause
16+
]
17+
18+
exclude = [
19+
"**/docs",
20+
]
21+
22+
target-version = "py38"
23+
24+
[per-file-ignores]
25+
# Ignore unused imports (F401) in these files
26+
"__init__.py" = ["F401"]
27+
"graphene_django/compat.py" = ["F401"]
28+
29+
[isort]
30+
known-first-party = ["graphene", "graphene-django"]
31+
known-local-folder = ["cookbook"]
32+
force-wrap-aliases = true
33+
combine-as-imports = true

Diff for: examples/cookbook-plain/cookbook/schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import cookbook.ingredients.schema
2-
import cookbook.recipes.schema
31
import graphene
4-
52
from graphene_django.debug import DjangoDebug
63

4+
import cookbook.ingredients.schema
5+
import cookbook.recipes.schema
6+
77

88
class Query(
99
cookbook.ingredients.schema.Query,

Diff for: examples/cookbook-plain/cookbook/urls.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from django.urls import path
21
from django.contrib import admin
2+
from django.urls import path
33

44
from graphene_django.views import GraphQLView
55

6-
76
urlpatterns = [
87
path("admin/", admin.site.urls),
98
path("graphql/", GraphQLView.as_view(graphiql=True)),

Diff for: examples/cookbook/cookbook/ingredients/schema.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from cookbook.ingredients.models import Category, Ingredient
21
from graphene import Node
32
from graphene_django.filter import DjangoFilterConnectionField
43
from graphene_django.types import DjangoObjectType
54

5+
from cookbook.ingredients.models import Category, Ingredient
6+
67

78
# Graphene will automatically map the Category model's fields onto the CategoryNode.
89
# This is configured in the CategoryNode's Meta class (as you can see below)

Diff for: examples/cookbook/cookbook/recipes/models.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
class Recipe(models.Model):
77
title = models.CharField(max_length=100)
88
instructions = models.TextField()
9-
__unicode__ = lambda self: self.title
9+
10+
def __unicode__(self):
11+
return self.title
1012

1113

1214
class RecipeIngredient(models.Model):

Diff for: examples/cookbook/cookbook/recipes/schema.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from cookbook.recipes.models import Recipe, RecipeIngredient
21
from graphene import Node
32
from graphene_django.filter import DjangoFilterConnectionField
43
from graphene_django.types import DjangoObjectType
54

5+
from cookbook.recipes.models import Recipe, RecipeIngredient
6+
67

78
class RecipeNode(DjangoObjectType):
89
class Meta:

Diff for: examples/cookbook/cookbook/schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import cookbook.ingredients.schema
2-
import cookbook.recipes.schema
31
import graphene
4-
52
from graphene_django.debug import DjangoDebug
63

4+
import cookbook.ingredients.schema
5+
import cookbook.recipes.schema
6+
77

88
class Query(
99
cookbook.ingredients.schema.Query,

Diff for: examples/cookbook/cookbook/urls.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from graphene_django.views import GraphQLView
55

6-
76
urlpatterns = [
87
url(r"^admin/", admin.site.urls),
98
url(r"^graphql$", GraphQLView.as_view(graphiql=True)),

Diff for: examples/django_test_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import sys
21
import os
2+
import sys
33

44
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
55
sys.path.insert(0, ROOT_PATH + "/examples/")

Diff for: examples/starwars/schema.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from graphene_django import DjangoConnectionField, DjangoObjectType
44

55
from .data import create_ship, get_empire, get_faction, get_rebels, get_ship, get_ships
6-
from .models import Character as CharacterModel
7-
from .models import Faction as FactionModel
8-
from .models import Ship as ShipModel
6+
from .models import (
7+
Character as CharacterModel,
8+
Faction as FactionModel,
9+
Ship as ShipModel,
10+
)
911

1012

1113
class Ship(DjangoObjectType):

Diff for: graphene_django/compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def __init__(self, *args, **kwargs):
1313
# Postgres fields are only available in Django with psycopg2 installed
1414
# and we cannot have psycopg2 on PyPy
1515
from django.contrib.postgres.fields import (
16-
IntegerRangeField,
1716
ArrayField,
1817
HStoreField,
18+
IntegerRangeField,
1919
RangeField,
2020
)
2121
except ImportError:

Diff for: graphene_django/converter.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
from django.utils.encoding import force_str
77
from django.utils.functional import Promise
88
from django.utils.module_loading import import_string
9+
from graphql import GraphQLError
910

1011
from graphene import (
1112
ID,
1213
UUID,
1314
Boolean,
1415
Date,
1516
DateTime,
17+
Decimal,
1618
Dynamic,
1719
Enum,
1820
Field,
@@ -22,13 +24,11 @@
2224
NonNull,
2325
String,
2426
Time,
25-
Decimal,
2627
)
2728
from graphene.types.json import JSONString
28-
from graphene.types.scalars import BigInt
2929
from graphene.types.resolver import get_default_resolver
30+
from graphene.types.scalars import BigInt
3031
from graphene.utils.str_converters import to_camel_case
31-
from graphql import GraphQLError
3232

3333
try:
3434
from graphql import assert_name
@@ -38,7 +38,7 @@
3838
from graphql.pyutils import register_description
3939

4040
from .compat import ArrayField, HStoreField, RangeField
41-
from .fields import DjangoListField, DjangoConnectionField
41+
from .fields import DjangoConnectionField, DjangoListField
4242
from .settings import graphene_settings
4343
from .utils.str_converters import to_const
4444

@@ -161,9 +161,7 @@ def get_django_field_description(field):
161161
@singledispatch
162162
def convert_django_field(field, registry=None):
163163
raise Exception(
164-
"Don't know how to convert the Django field {} ({})".format(
165-
field, field.__class__
166-
)
164+
f"Don't know how to convert the Django field {field} ({field.__class__})"
167165
)
168166

169167

@@ -261,6 +259,7 @@ def convert_time_to_string(field, registry=None):
261259
@convert_django_field.register(models.OneToOneRel)
262260
def convert_onetoone_field_to_djangomodel(field, registry=None):
263261
from graphene.utils.str_converters import to_snake_case
262+
264263
from .types import DjangoObjectType
265264

266265
model = field.related_model
@@ -364,6 +363,7 @@ def dynamic_type():
364363
@convert_django_field.register(models.ForeignKey)
365364
def convert_field_to_djangomodel(field, registry=None):
366365
from graphene.utils.str_converters import to_snake_case
366+
367367
from .types import DjangoObjectType
368368

369369
model = field.related_model

Diff for: graphene_django/debug/middleware.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from django.db import connections
22

3-
from promise import Promise
4-
5-
from .sql.tracking import unwrap_cursor, wrap_cursor
63
from .exception.formating import wrap_exception
4+
from .sql.tracking import unwrap_cursor, wrap_cursor
75
from .types import DjangoDebug
86

97

Diff for: graphene_django/debug/tests/test_query.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import graphene
21
import pytest
2+
3+
import graphene
34
from graphene.relay import Node
45
from graphene_django import DjangoConnectionField, DjangoObjectType
56

Diff for: graphene_django/debug/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from graphene import List, ObjectType
22

3-
from .sql.types import DjangoDebugSQL
43
from .exception.types import DjangoDebugException
4+
from .sql.types import DjangoDebugSQL
55

66

77
class DjangoDebug(ObjectType):

Diff for: graphene_django/fields.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from functools import partial
22

33
from django.db.models.query import QuerySet
4-
54
from graphql_relay import (
65
connection_from_array_slice,
76
cursor_to_offset,
87
get_offset_with_default,
98
offset_to_cursor,
109
)
11-
1210
from promise import Promise
1311

1412
from graphene import Int, NonNull

Diff for: graphene_django/filter/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
23
from ..utils import DJANGO_FILTER_INSTALLED
34

45
if not DJANGO_FILTER_INSTALLED:

Diff for: graphene_django/filter/fields.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from django.core.exceptions import ValidationError
55

6-
from graphene.types.enum import EnumType
76
from graphene.types.argument import to_arguments
7+
from graphene.types.enum import EnumType
88
from graphene.utils.str_converters import to_snake_case
99

1010
from ..fields import DjangoConnectionField
@@ -58,7 +58,7 @@ def args(self, args):
5858
def filterset_class(self):
5959
if not self._filterset_class:
6060
fields = self._fields or self.node_type._meta.filter_fields
61-
meta = dict(model=self.model, fields=fields)
61+
meta = {"model": self.model, "fields": fields}
6262
if self._extra_filter_meta:
6363
meta.update(self._extra_filter_meta)
6464

Diff for: graphene_django/filter/filters/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
23
from ...utils import DJANGO_FILTER_INSTALLED
34

45
if not DJANGO_FILTER_INSTALLED:

Diff for: graphene_django/filter/filters/global_id_filter.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django_filters import Filter, MultipleChoiceFilter
2-
32
from graphql_relay.node.node import from_global_id
43

54
from ...forms import GlobalIDFormField, GlobalIDMultipleChoiceField

Diff for: graphene_django/filter/filterset.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import itertools
22

33
from django.db import models
4-
from django_filters.filterset import BaseFilterSet, FilterSet
5-
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
4+
from django_filters.filterset import (
5+
FILTER_FOR_DBFIELD_DEFAULTS,
6+
BaseFilterSet,
7+
FilterSet,
8+
)
69

710
from .filters import GlobalIDFilter, GlobalIDMultipleChoiceFilter
811

9-
1012
GRAPHENE_FILTER_SET_OVERRIDES = {
1113
models.AutoField: {"filter_class": GlobalIDFilter},
1214
models.OneToOneField: {"filter_class": GlobalIDFilter},

Diff for: graphene_django/filter/tests/conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from unittest.mock import MagicMock
2-
import pytest
32

3+
import pytest
44
from django.db import models
55
from django.db.models.query import QuerySet
6-
from django_filters import filters
76
from django_filters import FilterSet
7+
88
import graphene
99
from graphene.relay import Node
1010
from graphene_django import DjangoObjectType
11+
from graphene_django.filter import ArrayFilter
1112
from graphene_django.utils import DJANGO_FILTER_INSTALLED
12-
from graphene_django.filter import ArrayFilter, ListFilter
1313

1414
from ...compat import ArrayField
1515

Diff for: graphene_django/filter/tests/test_enum_filtering.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import graphene
44
from graphene.relay import Node
5-
6-
from graphene_django import DjangoObjectType, DjangoConnectionField
5+
from graphene_django import DjangoConnectionField, DjangoObjectType
76
from graphene_django.tests.models import Article, Reporter
87
from graphene_django.utils import DJANGO_FILTER_INSTALLED
98

Diff for: graphene_django/filter/tests/test_fields.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from django_filters import FilterSet, NumberFilter, OrderingFilter
2020

2121
from graphene_django.filter import (
22-
GlobalIDFilter,
2322
DjangoFilterConnectionField,
23+
GlobalIDFilter,
2424
GlobalIDMultipleChoiceFilter,
2525
)
2626
from graphene_django.filter.tests.filters import (
@@ -222,7 +222,7 @@ class Query(ObjectType):
222222
reporter = Field(ReporterFilterNode)
223223
article = Field(ArticleFilterNode)
224224

225-
schema = Schema(query=Query)
225+
Schema(query=Query)
226226
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
227227
assert_arguments(articles_field, "headline", "reporter")
228228
assert_not_orderable(articles_field)
@@ -294,7 +294,7 @@ class Query(ObjectType):
294294
reporter = Field(ReporterFilterNode)
295295
article = Field(ArticleFilterNode)
296296

297-
schema = Schema(query=Query)
297+
Schema(query=Query)
298298
articles_field = ReporterFilterNode._meta.fields["articles"].get_type()
299299
assert_arguments(articles_field, "headline", "reporter")
300300
assert_not_orderable(articles_field)
@@ -1186,7 +1186,7 @@ class Query(ObjectType):
11861186
first_name="Adam", last_name="Doe", email="[email protected]"
11871187
)
11881188

1189-
article_2 = Article.objects.create(
1189+
Article.objects.create(
11901190
headline="Good Bye",
11911191
reporter=reporter_2,
11921192
editor=reporter_2,

0 commit comments

Comments
 (0)