Skip to content

Commit

Permalink
chore: migrate from black to ruff-formatter (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 authored Oct 29, 2023
1 parent 8f82b84 commit 7c42e28
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 348 deletions.
7 changes: 2 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ repos:
- id: check-xml
- id: check-symlinks
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.1
rev: v0.1.3
hooks:
- id: ruff-format
- id: ruff
args:
- --fix
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.10.0
hooks:
- id: black
- repo: https://github.com/patrick91/pre-commit-alex
rev: aa5da9e54b92ab7284feddeaf52edf14b1690de3
hooks:
Expand Down
523 changes: 264 additions & 259 deletions poetry.lock

Large diffs are not rendered by default.

40 changes: 19 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mkdocs-material = "^9.0.4"
mkdocs-minify-plugin = "^0.7.1"
pymdown-extensions = "^10.0.1"
mkdocstrings = { version = "^0.23.0", extras = ["python"] }
ruff = "^0.0.292"
ruff = "^0.1.3"
django-types = "^0.18.0"
Markdown = "^3.3.7"
Pygments = "^2.15.1"
Expand All @@ -74,6 +74,8 @@ requires = ["poetry-core>=1.0.0", "setuptools"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
target-version = "py38"
preview = true
select = [
"A",
"ASYNC100",
Expand Down Expand Up @@ -132,11 +134,24 @@ ignore = [
"PLR09",
"SLF001",
"TRY003",

"PLR6301",
# ruff formatter recommends to disable those
"COM812",
"COM819",
"D206",
"E111",
"E114",
"E117",
"E501",
"ISC001",
"Q000",
"Q001",
"Q002",
"Q003",
"W191",
]
# FIXME: Enable autofix for UP006 when we don't need to support django 3.8 anymore
unfixable = ["UP006"]
target-version = "py38"
exclude = [
".eggs",
".git",
Expand All @@ -158,24 +173,7 @@ exclude = [

[tool.ruff.isort]

[tool.black]
target-version = ['py38', 'py39', 'py310', 'py311']
preview = true
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| __pycached__
| _build
| buck-out
| build
| dist
)/
'''
[tool.ruff.format]

[tool.pyright]
pythonVersion = "3.8"
Expand Down
4 changes: 1 addition & 3 deletions strawberry_django/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ def resolve_type(
*,
type_definition: StrawberryObjectDefinition | None = None,
) -> (
StrawberryType
| type[WithStrawberryObjectDefinition]
| Literal[UNRESOLVED] # type: ignore
StrawberryType | type[WithStrawberryObjectDefinition] | Literal[UNRESOLVED] # type: ignore
):
resolved = super().resolve_type(type_definition=type_definition)
if resolved is UNRESOLVED:
Expand Down
4 changes: 2 additions & 2 deletions strawberry_django/fields/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ def apply(self, field: StrawberryDjangoField) -> None:

if "filters" not in args:
filters = field.get_filters()
if filters not in (None, UNSET):
if filters not in (None, UNSET): # noqa: PLR6201
args["filters"] = argument("filters", filters, is_optional=True)
if "order" not in args:
order = field.get_order()
if order not in (None, UNSET):
if order not in (None, UNSET): # noqa: PLR6201
args["order"] = argument("order", order, is_optional=True)

field.arguments = list(args.values())
Expand Down
6 changes: 3 additions & 3 deletions strawberry_django/fields/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ def __hash__(self):
Point = strawberry.scalar(
NewType("Point", Tuple[float, float, Optional[float]]),
serialize=lambda v: v.tuple if isinstance(v, geos.Point) else v,
parse_value=lambda v: geos.Point(v),
parse_value=geos.Point,
description="Represents a point as `(x, y, z)` or `(x, y)`.",
)

LineString = strawberry.scalar(
NewType("LineString", Tuple[Point]),
serialize=lambda v: v.tuple if isinstance(v, geos.LineString) else v,
parse_value=lambda v: geos.LineString(v),
parse_value=geos.LineString,
description=(
"A geographical line that gets multiple 'x, y' or 'x, y, z'"
" tuples to form a line."
Expand All @@ -302,7 +302,7 @@ def __hash__(self):
LinearRing = strawberry.scalar(
NewType("LinearRing", Tuple[Point]),
serialize=lambda v: v.tuple if isinstance(v, geos.LinearRing) else v,
parse_value=lambda v: geos.LinearRing(v),
parse_value=geos.LinearRing,
description=(
"A geographical line that gets multiple 'x, y' or 'x, y, z' "
"tuples to form a line. It must be a circle. "
Expand Down
6 changes: 3 additions & 3 deletions strawberry_django/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def build_filter_kwargs(
continue

if django_model:
if field_name in ("AND", "OR", "NOT"):
if field_name in ("AND", "OR", "NOT"): # noqa: PLR6201
if has_object_definition(field_value):
(
subfield_filter_kwargs,
Expand Down Expand Up @@ -267,10 +267,10 @@ def apply(
info: Optional[Info] = None,
pk: Optional[Any] = None,
) -> _QS:
if pk not in (None, strawberry.UNSET):
if pk not in (None, strawberry.UNSET): # noqa: PLR6201
queryset = queryset.filter(pk=pk)

if filters in (None, strawberry.UNSET) or not has_django_definition(filters):
if filters in (None, strawberry.UNSET) or not has_django_definition(filters): # noqa: PLR6201
return queryset

# Custom filter function in the filters object
Expand Down
2 changes: 1 addition & 1 deletion strawberry_django/middlewares/debug_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __call__(self, request: HttpRequest):

return self.process_request(request)

async def __acall__(self, request: HttpRequest):
async def __acall__(self, request: HttpRequest): # noqa: PLW3201
if _is_websocket(request):
return await self._original_get_response(request)

Expand Down
8 changes: 3 additions & 5 deletions strawberry_django/mutations/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def resolve_type(
*,
type_definition: StrawberryObjectDefinition | None = None,
) -> (
StrawberryType
| type[WithStrawberryObjectDefinition]
| Literal[UNRESOLVED] # type: ignore
StrawberryType | type[WithStrawberryObjectDefinition] | Literal[UNRESOLVED] # type: ignore
):
resolved = super().resolve_type(type_definition=type_definition)
if resolved is UNRESOLVED:
Expand Down Expand Up @@ -286,7 +284,7 @@ def resolver(
vdata = vars(data).copy() if data is not None else {}

pk = get_pk(vdata, key_attr=self.key_attr)
if pk not in (None, UNSET):
if pk not in (None, UNSET): # noqa: PLR6201
instance = get_with_perms(
pk,
info,
Expand Down Expand Up @@ -343,7 +341,7 @@ def resolver(
vdata = vars(data).copy() if data is not None else {}

pk = get_pk(vdata, key_attr=self.key_attr)
if pk not in (None, UNSET):
if pk not in (None, UNSET): # noqa: PLR6201
instance = get_with_perms(
pk,
info,
Expand Down
17 changes: 8 additions & 9 deletions strawberry_django/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ def _get_model_hints(
# because when field_store was created on __init__,
# the field name wasn't available.
# This allows for annotate expressions to be declared as:
# total: int = gql.django.field(annotate=Sum("price")) # noqa: ERA001, E501
# total: int = gql.django.field(annotate=Sum("price")) # noqa: ERA001
# Instead of the more redundant:
# total: int = gql.django.field(annotate={"total": Sum("price")}) # noqa: ERA001, E501
# total: int = gql.django.field(annotate={"total": Sum("price")}) # noqa: ERA001
field_store.annotate = {
field.name: field_store.annotate[_annotate_placeholder],
}
Expand Down Expand Up @@ -653,8 +653,7 @@ def optimize(

# Avoid optimizing twice and also modify an already resolved queryset
if (
getattr(qs, "_gql_optimized", False)
or qs._result_cache is not None # type: ignore
getattr(qs, "_gql_optimized", False) or qs._result_cache is not None # type: ignore
):
return qs

Expand Down Expand Up @@ -714,11 +713,11 @@ def optimize(
return qs


optimizer: contextvars.ContextVar[DjangoOptimizerExtension | None] = (
contextvars.ContextVar(
"optimizer_ctx",
default=None,
)
optimizer: contextvars.ContextVar[
DjangoOptimizerExtension | None
] = contextvars.ContextVar(
"optimizer_ctx",
default=None,
)


Expand Down
2 changes: 1 addition & 1 deletion strawberry_django/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def generate_order_args(order: WithStrawberryObjectDefinition, prefix: str = "")


def apply(order: Optional[WithStrawberryObjectDefinition], queryset: _QS) -> _QS:
if order in (None, strawberry.UNSET):
if order in (None, strawberry.UNSET): # noqa: PLR6201
return queryset

args = generate_order_args(order)
Expand Down
2 changes: 1 addition & 1 deletion strawberry_django/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OffsetPaginationInput:


def apply(pagination: Optional[object], queryset: _QS) -> _QS:
if pagination in (None, strawberry.UNSET):
if pagination in (None, strawberry.UNSET): # noqa: PLR6201
return queryset

if not isinstance(pagination, OffsetPaginationInput):
Expand Down
6 changes: 3 additions & 3 deletions strawberry_django/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,9 @@ class HasPerm(DjangoPermissionExtension):
"""

DEFAULT_TARGET: ClassVar[PermTarget] = PermTarget.GLOBAL
DEFAULT_ERROR_MESSAGE: ClassVar[str] = (
"You don't have permission to access this app."
)
DEFAULT_ERROR_MESSAGE: ClassVar[
str
] = "You don't have permission to access this app."
SCHEMA_DIRECTIVE_DESCRIPTION: ClassVar[str] = _desc(
"Will check if the user has any/all permissions to resolve this.",
)
Expand Down
2 changes: 1 addition & 1 deletion strawberry_django/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_django_definition(

def is_auto(obj: Any) -> bool:
if isinstance(obj, str):
return obj in ["auto", "strawberry.auto"]
return obj in {"auto", "strawberry.auto"}

return isinstance(obj, StrawberryAuto)

Expand Down
6 changes: 4 additions & 2 deletions tests/filters/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def test_deprecated_not(query, fruits):


def test_not(query, fruits):
result = query("""{
result = query(
"""{
fruits(
filters: {
NOT: {
Expand All @@ -168,7 +169,8 @@ def test_not(query, fruits):
id
name
}
}""")
}"""
)
assert not result.errors
assert result.data["fruits"] == [
{"id": "3", "name": "banana"},
Expand Down
4 changes: 2 additions & 2 deletions tests/mutations/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Mutation:

@strawberry_django.mutation
def update_lazy_fruit(self, info, data: FruitPartialInput) -> Fruit:
fruit = SimpleLazyObject(lambda: models.Fruit.objects.get())
fruit = SimpleLazyObject(models.Fruit.objects.get)
return cast(
Fruit,
resolvers.update(
Expand All @@ -55,7 +55,7 @@ def update_lazy_fruit(self, info, data: FruitPartialInput) -> Fruit:

@strawberry_django.mutation
def delete_lazy_fruit(self, info) -> Fruit:
fruit = SimpleLazyObject(lambda: models.Fruit.objects.get())
fruit = SimpleLazyObject(models.Fruit.objects.get)
return cast(
Fruit,
resolvers.delete(
Expand Down
34 changes: 17 additions & 17 deletions tests/projects/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ class IssueType(relay.Node):
name_with_kind: str = strawberry_django.field(only=["kind", "name"])
tags: List["TagType"]
issue_assignees: List["AssigneeType"]
favorite_set: ListConnectionWithTotalCount["FavoriteType"] = (
strawberry_django.connection()
)
favorite_set: ListConnectionWithTotalCount[
"FavoriteType"
] = strawberry_django.connection()


@strawberry_django.type(Tag)
Expand Down Expand Up @@ -318,18 +318,18 @@ class Query:
project_list: List[ProjectType] = strawberry_django.field()
tag_list: List[TagType] = strawberry_django.field()

favorite_conn: ListConnectionWithTotalCount[FavoriteType] = (
strawberry_django.connection()
)
favorite_conn: ListConnectionWithTotalCount[
FavoriteType
] = strawberry_django.connection()
issue_conn: ListConnectionWithTotalCount[
strawberry.LazyType[
"IssueType",
"tests.projects.schema", # type: ignore # noqa: F821
]
] = strawberry_django.connection()
milestone_conn: ListConnectionWithTotalCount[MilestoneType] = (
strawberry_django.connection()
)
milestone_conn: ListConnectionWithTotalCount[
MilestoneType
] = strawberry_django.connection()

project_conn: ProjectConnection = strawberry_django.connection()
tag_conn: ListConnectionWithTotalCount[TagType] = strawberry_django.connection()
Expand Down Expand Up @@ -364,10 +364,10 @@ class Query:
issue_list_perm_required: List[IssueType] = strawberry_django.field(
extensions=[HasPerm(perms=["projects.view_issue"])],
)
issue_conn_perm_required: ListConnectionWithTotalCount[IssueType] = (
strawberry_django.connection(
extensions=[HasPerm(perms=["projects.view_issue"])],
)
issue_conn_perm_required: ListConnectionWithTotalCount[
IssueType
] = strawberry_django.connection(
extensions=[HasPerm(perms=["projects.view_issue"])],
)
# User permission on the resolved object for "projects.view_issue"
issue_obj_perm_required: IssueType = strawberry_django.node(
Expand All @@ -379,10 +379,10 @@ class Query:
issue_list_obj_perm_required: List[IssueType] = strawberry_django.field(
extensions=[HasRetvalPerm(perms=["projects.view_issue"])],
)
issue_conn_obj_perm_required: ListConnectionWithTotalCount[IssueType] = (
strawberry_django.connection(
extensions=[HasRetvalPerm(perms=["projects.view_issue"])],
)
issue_conn_obj_perm_required: ListConnectionWithTotalCount[
IssueType
] = strawberry_django.connection(
extensions=[HasRetvalPerm(perms=["projects.view_issue"])],
)

@strawberry_django.field
Expand Down
Loading

0 comments on commit 7c42e28

Please sign in to comment.