Skip to content

Commit 9180278

Browse files
authored
chore: use pre-commit and ruff (#116)
* chore: use pre-commit with ruff * chore: fix warning: Invalid # noqa directive * fix: B023 Function definition does not bind loop variable `ex`. * chore: add check-manifest pre-commit * chore: remove flake8 and isort config from setup.cfg * chore: remove black, flake8, ... from dependencies and tox * chore: add flake8-comprehensions * chore: remove pointless noqa * chore: keep type annotation syntax compatible with python 3.8
1 parent 8af13c0 commit 9180278

25 files changed

+270
-244
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
- name: Run lint and static type checks
2020
run: tox
2121
env:
22-
TOXENV: flake8,black,import-order,mypy,manifest
22+
TOXENV: pre-commit,mypy

.pre-commit-config.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
default_language_version:
2+
python: python3.10
3+
exclude: LICENSE
4+
repos:
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v4.4.0
7+
hooks:
8+
- id: check-merge-conflict
9+
- id: check-json
10+
- id: check-yaml
11+
- id: debug-statements
12+
- id: end-of-file-fixer
13+
exclude: ^docs/.*$
14+
- id: pretty-format-json
15+
args:
16+
- --autofix
17+
- id: trailing-whitespace
18+
- repo: https://github.com/mgedmin/check-manifest
19+
rev: "0.49"
20+
hooks:
21+
- id: check-manifest
22+
- repo: https://github.com/psf/black
23+
rev: 23.7.0
24+
hooks:
25+
- id: black
26+
- repo: https://github.com/astral-sh/ruff-pre-commit
27+
rev: v0.0.283
28+
hooks:
29+
- id: ruff
30+
args: [--fix, --exit-non-zero-on-fix, --show-fixes]

.ruff.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
"B904", # check for raise statements in exception handlers that lack a from clause
14+
]
15+
16+
exclude = [
17+
"**/docs",
18+
]
19+
20+
target-version = "py38"
21+
22+
[per-file-ignores]
23+
# Ignore unused imports (F401) in these files
24+
"__init__.py" = ["F401"]
25+
26+
[isort]
27+
known-first-party = ["graphql_server"]
28+
force-wrap-aliases = true
29+
combine-as-imports = true
30+
31+
[pyupgrade]
32+
# this keeps annotation syntaxes like Union[X, Y] instead of X | Y
33+
# to not break Python 3.8
34+
# https://beta.ruff.rs/docs/settings/#pyupgrade-keep-runtime-typing
35+
keep-runtime-typing = true

CONTRIBUTING.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ Activate the virtualenv and install dependencies by running:
3131
python pip install -e ".[test]"
3232
```
3333

34-
If you are using Linux or MacOS, you can make use of Makefile command
35-
`make dev-setup`, which is a shortcut for the above python command.
34+
If you are using Linux or MacOS, you can make use of Makefile command `make dev-setup`, which is a shortcut for the above python command.
3635

3736
### Development on Conda
3837

@@ -60,8 +59,7 @@ After developing, the full test suite can be evaluated by running:
6059
pytest tests --cov=graphql-server -vv
6160
```
6261

63-
If you are using Linux or MacOS, you can make use of Makefile command
64-
`make tests`, which is a shortcut for the above python command.
62+
If you are using Linux or MacOS, you can make use of Makefile command `make tests`, which is a shortcut for the above python command.
6563

6664
You can also test on several python environments by using tox.
6765

@@ -73,8 +71,7 @@ Install tox:
7371
pip install tox
7472
```
7573

76-
Run `tox` on your virtualenv (do not forget to activate it!)
77-
and that's it!
74+
Run `tox` on your virtualenv (do not forget to activate it!) and that's it!
7875

7976
### Running tox on Conda
8077

@@ -89,5 +86,4 @@ This install tox underneath so no need to install it before.
8986

9087
Then uncomment the `requires = tox-conda` line on `tox.ini` file.
9188

92-
Run `tox` and you will see all the environments being created
93-
and all passing tests. :rocket:
89+
Run `tox` and you will see all the environments being created and all passing tests. :rocket:

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ include CONTRIBUTING.md
66

77
include codecov.yml
88
include tox.ini
9+
include .pre-commit-config.yaml
10+
include .ruff.toml
911

1012
recursive-include docs *.md *.svg
1113

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ coverage:
77
status:
88
project:
99
default:
10-
target: auto
10+
target: auto

graphql_server/aiohttp/graphqlview.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class GraphQLView:
5656
encode = staticmethod(json_encode)
5757

5858
def __init__(self, **kwargs):
59-
super(GraphQLView, self).__init__()
59+
super().__init__()
6060
for key, value in kwargs.items():
6161
if hasattr(self, key):
6262
setattr(self, key, value)
@@ -177,7 +177,7 @@ async def __call__(self, request):
177177
*(
178178
ex
179179
if ex is not None and is_awaitable(ex)
180-
else wrap_in_async(lambda: ex)()
180+
else wrap_in_async(lambda x: x)(ex)
181181
for ex in execution_results
182182
)
183183
)
@@ -188,15 +188,15 @@ async def __call__(self, request):
188188
exec_res,
189189
is_batch=isinstance(data, list),
190190
format_error=self.format_error,
191-
encode=partial(self.encode, pretty=is_pretty), # noqa: ignore
191+
encode=partial(self.encode, pretty=is_pretty),
192192
)
193193

194194
if is_graphiql:
195195
graphiql_data = GraphiQLData(
196196
result=result,
197-
query=getattr(all_params[0], "query"),
198-
variables=getattr(all_params[0], "variables"),
199-
operation_name=getattr(all_params[0], "operation_name"),
197+
query=all_params[0].query,
198+
variables=all_params[0].variables,
199+
operation_name=all_params[0].operation_name,
200200
subscription_url=self.subscriptions,
201201
headers=self.headers,
202202
)
@@ -225,7 +225,7 @@ async def __call__(self, request):
225225
except HttpQueryError as err:
226226
parsed_error = GraphQLError(err.message)
227227
return web.Response(
228-
body=self.encode(dict(errors=[self.format_error(parsed_error)])),
228+
body=self.encode({"errors": [self.format_error(parsed_error)]}),
229229
status=err.status_code,
230230
headers=err.headers,
231231
content_type="application/json",

graphql_server/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, status_code, message=None, is_graphql_error=False, headers=No
1616
self.message = message
1717
self.is_graphql_error = is_graphql_error
1818
self.headers = headers
19-
super(HttpQueryError, self).__init__(message)
19+
super().__init__(message)
2020

2121
def __eq__(self, other):
2222
"""Check whether this HTTP query error is equal to another one."""

graphql_server/flask/graphqlview.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class GraphQLView(View):
5353
encode = staticmethod(json_encode)
5454

5555
def __init__(self, **kwargs):
56-
super(GraphQLView, self).__init__()
56+
super().__init__()
5757
for key, value in kwargs.items():
5858
if hasattr(self, key):
5959
setattr(self, key, value)
@@ -120,15 +120,15 @@ def dispatch_request(self):
120120
execution_results,
121121
is_batch=isinstance(data, list),
122122
format_error=self.format_error,
123-
encode=partial(self.encode, pretty=pretty), # noqa
123+
encode=partial(self.encode, pretty=pretty),
124124
)
125125

126126
if show_graphiql:
127127
graphiql_data = GraphiQLData(
128128
result=result,
129-
query=getattr(all_params[0], "query"),
130-
variables=getattr(all_params[0], "variables"),
131-
operation_name=getattr(all_params[0], "operation_name"),
129+
query=all_params[0].query,
130+
variables=all_params[0].variables,
131+
operation_name=all_params[0].operation_name,
132132
subscription_url=self.subscriptions,
133133
headers=self.headers,
134134
)
@@ -153,7 +153,7 @@ def dispatch_request(self):
153153
except HttpQueryError as e:
154154
parsed_error = GraphQLError(e.message)
155155
return Response(
156-
self.encode(dict(errors=[self.format_error(parsed_error)])),
156+
self.encode({"errors": [self.format_error(parsed_error)]}),
157157
status=e.status_code,
158158
headers=e.headers,
159159
content_type="application/json",

graphql_server/quart/graphqlview.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class GraphQLView(View):
5757
encode = staticmethod(json_encode)
5858

5959
def __init__(self, **kwargs):
60-
super(GraphQLView, self).__init__()
60+
super().__init__()
6161
for key, value in kwargs.items():
6262
if hasattr(self, key):
6363
setattr(self, key, value)
@@ -125,7 +125,7 @@ async def dispatch_request(self):
125125
*(
126126
ex
127127
if ex is not None and is_awaitable(ex)
128-
else wrap_in_async(lambda: ex)()
128+
else wrap_in_async(lambda x: x)(ex)
129129
for ex in execution_results
130130
)
131131
)
@@ -136,15 +136,15 @@ async def dispatch_request(self):
136136
exec_res,
137137
is_batch=isinstance(data, list),
138138
format_error=self.format_error,
139-
encode=partial(self.encode, pretty=pretty), # noqa
139+
encode=partial(self.encode, pretty=pretty),
140140
)
141141

142142
if show_graphiql:
143143
graphiql_data = GraphiQLData(
144144
result=result,
145-
query=getattr(all_params[0], "query"),
146-
variables=getattr(all_params[0], "variables"),
147-
operation_name=getattr(all_params[0], "operation_name"),
145+
query=all_params[0].query,
146+
variables=all_params[0].variables,
147+
operation_name=all_params[0].operation_name,
148148
subscription_url=self.subscriptions,
149149
headers=self.headers,
150150
)
@@ -169,7 +169,7 @@ async def dispatch_request(self):
169169
except HttpQueryError as e:
170170
parsed_error = GraphQLError(e.message)
171171
return Response(
172-
self.encode(dict(errors=[self.format_error(parsed_error)])),
172+
self.encode({"errors": [self.format_error(parsed_error)]}),
173173
status=e.status_code,
174174
headers=e.headers,
175175
content_type="application/json",

graphql_server/sanic/graphqlview.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class GraphQLView(HTTPMethodView):
5858
encode = staticmethod(json_encode)
5959

6060
def __init__(self, **kwargs):
61-
super(GraphQLView, self).__init__()
61+
super().__init__()
6262
for key, value in kwargs.items():
6363
if hasattr(self, key):
6464
setattr(self, key, value)
@@ -130,7 +130,7 @@ async def __handle_request(self, request, *args, **kwargs):
130130
*(
131131
ex
132132
if ex is not None and is_awaitable(ex)
133-
else wrap_in_async(lambda: ex)()
133+
else wrap_in_async(lambda x: x)(ex)
134134
for ex in execution_results
135135
)
136136
)
@@ -141,15 +141,15 @@ async def __handle_request(self, request, *args, **kwargs):
141141
exec_res,
142142
is_batch=isinstance(data, list),
143143
format_error=self.format_error,
144-
encode=partial(self.encode, pretty=pretty), # noqa: ignore
144+
encode=partial(self.encode, pretty=pretty),
145145
)
146146

147147
if show_graphiql:
148148
graphiql_data = GraphiQLData(
149149
result=result,
150-
query=getattr(all_params[0], "query"),
151-
variables=getattr(all_params[0], "variables"),
152-
operation_name=getattr(all_params[0], "operation_name"),
150+
query=all_params[0].query,
151+
variables=all_params[0].variables,
152+
operation_name=all_params[0].operation_name,
153153
subscription_url=self.subscriptions,
154154
headers=self.headers,
155155
)
@@ -181,7 +181,7 @@ async def __handle_request(self, request, *args, **kwargs):
181181
except HttpQueryError as e:
182182
parsed_error = GraphQLError(e.message)
183183
return HTTPResponse(
184-
self.encode(dict(errors=[self.format_error(parsed_error)])),
184+
self.encode({"errors": [self.format_error(parsed_error)]}),
185185
status=e.status_code,
186186
headers=e.headers,
187187
content_type="application/json",

graphql_server/webob/graphqlview.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class GraphQLView:
5252
encode = staticmethod(json_encode)
5353

5454
def __init__(self, **kwargs):
55-
super(GraphQLView, self).__init__()
55+
super().__init__()
5656
for key, value in kwargs.items():
5757
if hasattr(self, key):
5858
setattr(self, key, value)
@@ -122,15 +122,15 @@ def dispatch_request(self, request):
122122
execution_results,
123123
is_batch=isinstance(data, list),
124124
format_error=self.format_error,
125-
encode=partial(self.encode, pretty=pretty), # noqa
125+
encode=partial(self.encode, pretty=pretty),
126126
)
127127

128128
if show_graphiql:
129129
graphiql_data = GraphiQLData(
130130
result=result,
131-
query=getattr(all_params[0], "query"),
132-
variables=getattr(all_params[0], "variables"),
133-
operation_name=getattr(all_params[0], "operation_name"),
131+
query=all_params[0].query,
132+
variables=all_params[0].variables,
133+
operation_name=all_params[0].operation_name,
134134
subscription_url=self.subscriptions,
135135
headers=self.headers,
136136
)
@@ -165,7 +165,7 @@ def dispatch_request(self, request):
165165
except HttpQueryError as e:
166166
parsed_error = GraphQLError(e.message)
167167
return Response(
168-
self.encode(dict(errors=[self.format_error(parsed_error)])),
168+
self.encode({"errors": [self.format_error(parsed_error)]}),
169169
status=e.status_code,
170170
charset=self.charset,
171171
headers=e.headers or {},

setup.cfg

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
[flake8]
2-
exclude = docs
3-
max-line-length = 88
4-
ignore = E203, E501, W503
5-
6-
[isort]
7-
known_first_party=graphql_server
8-
profile=black
9-
multi_line_output=3
10-
include_trailing_comma=True
11-
force_grid_wrap=0
12-
use_parentheses=True
13-
141
[tool:pytest]
152
norecursedirs = venv .venv .tox .git .cache .mypy_cache .pytest_cache
163
markers = asyncio

setup.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
]
1717

1818
dev_requires = [
19-
"flake8>=6,<7",
20-
"isort>=5,<6",
21-
"black>=23.9,<23.10",
2219
"mypy>=1.6,<1.7",
23-
"check-manifest>=0.47,<1",
2420
] + tests_requires
2521

2622
install_flask_requires = [

0 commit comments

Comments
 (0)