Skip to content

Commit

Permalink
chore: use pre-commit and ruff (#116)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kiendang authored Oct 16, 2023
1 parent 8af13c0 commit 9180278
Show file tree
Hide file tree
Showing 25 changed files with 270 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
- name: Run lint and static type checks
run: tox
env:
TOXENV: flake8,black,import-order,mypy,manifest
TOXENV: pre-commit,mypy
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
default_language_version:
python: python3.10
exclude: LICENSE
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-merge-conflict
- id: check-json
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
exclude: ^docs/.*$
- id: pretty-format-json
args:
- --autofix
- id: trailing-whitespace
- repo: https://github.com/mgedmin/check-manifest
rev: "0.49"
hooks:
- id: check-manifest
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.283
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
35 changes: 35 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
select = [
"E", # pycodestyle
"W", # pycodestyle
"F", # pyflake
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
]

ignore = [
"E501", # line-too-long
"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"]

[isort]
known-first-party = ["graphql_server"]
force-wrap-aliases = true
combine-as-imports = true

[pyupgrade]
# this keeps annotation syntaxes like Union[X, Y] instead of X | Y
# to not break Python 3.8
# https://beta.ruff.rs/docs/settings/#pyupgrade-keep-runtime-typing
keep-runtime-typing = true
12 changes: 4 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ Activate the virtualenv and install dependencies by running:
python pip install -e ".[test]"
```

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.
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.

### Development on Conda

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

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.
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.

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

Expand All @@ -73,8 +71,7 @@ Install tox:
pip install tox
```

Run `tox` on your virtualenv (do not forget to activate it!)
and that's it!
Run `tox` on your virtualenv (do not forget to activate it!) and that's it!

### Running tox on Conda

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

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

Run `tox` and you will see all the environments being created
and all passing tests. :rocket:
Run `tox` and you will see all the environments being created and all passing tests. :rocket:
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include CONTRIBUTING.md

include codecov.yml
include tox.ini
include .pre-commit-config.yaml
include .ruff.toml

recursive-include docs *.md *.svg

Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ coverage:
status:
project:
default:
target: auto
target: auto
14 changes: 7 additions & 7 deletions graphql_server/aiohttp/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GraphQLView:
encode = staticmethod(json_encode)

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
super().__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
Expand Down Expand Up @@ -177,7 +177,7 @@ async def __call__(self, request):
*(
ex
if ex is not None and is_awaitable(ex)
else wrap_in_async(lambda: ex)()
else wrap_in_async(lambda x: x)(ex)
for ex in execution_results
)
)
Expand All @@ -188,15 +188,15 @@ async def __call__(self, request):
exec_res,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=is_pretty), # noqa: ignore
encode=partial(self.encode, pretty=is_pretty),
)

if is_graphiql:
graphiql_data = GraphiQLData(
result=result,
query=getattr(all_params[0], "query"),
variables=getattr(all_params[0], "variables"),
operation_name=getattr(all_params[0], "operation_name"),
query=all_params[0].query,
variables=all_params[0].variables,
operation_name=all_params[0].operation_name,
subscription_url=self.subscriptions,
headers=self.headers,
)
Expand Down Expand Up @@ -225,7 +225,7 @@ async def __call__(self, request):
except HttpQueryError as err:
parsed_error = GraphQLError(err.message)
return web.Response(
body=self.encode(dict(errors=[self.format_error(parsed_error)])),
body=self.encode({"errors": [self.format_error(parsed_error)]}),
status=err.status_code,
headers=err.headers,
content_type="application/json",
Expand Down
2 changes: 1 addition & 1 deletion graphql_server/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, status_code, message=None, is_graphql_error=False, headers=No
self.message = message
self.is_graphql_error = is_graphql_error
self.headers = headers
super(HttpQueryError, self).__init__(message)
super().__init__(message)

def __eq__(self, other):
"""Check whether this HTTP query error is equal to another one."""
Expand Down
12 changes: 6 additions & 6 deletions graphql_server/flask/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GraphQLView(View):
encode = staticmethod(json_encode)

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
super().__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
Expand Down Expand Up @@ -120,15 +120,15 @@ def dispatch_request(self):
execution_results,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty), # noqa
encode=partial(self.encode, pretty=pretty),
)

if show_graphiql:
graphiql_data = GraphiQLData(
result=result,
query=getattr(all_params[0], "query"),
variables=getattr(all_params[0], "variables"),
operation_name=getattr(all_params[0], "operation_name"),
query=all_params[0].query,
variables=all_params[0].variables,
operation_name=all_params[0].operation_name,
subscription_url=self.subscriptions,
headers=self.headers,
)
Expand All @@ -153,7 +153,7 @@ def dispatch_request(self):
except HttpQueryError as e:
parsed_error = GraphQLError(e.message)
return Response(
self.encode(dict(errors=[self.format_error(parsed_error)])),
self.encode({"errors": [self.format_error(parsed_error)]}),
status=e.status_code,
headers=e.headers,
content_type="application/json",
Expand Down
14 changes: 7 additions & 7 deletions graphql_server/quart/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class GraphQLView(View):
encode = staticmethod(json_encode)

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
super().__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
Expand Down Expand Up @@ -125,7 +125,7 @@ async def dispatch_request(self):
*(
ex
if ex is not None and is_awaitable(ex)
else wrap_in_async(lambda: ex)()
else wrap_in_async(lambda x: x)(ex)
for ex in execution_results
)
)
Expand All @@ -136,15 +136,15 @@ async def dispatch_request(self):
exec_res,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty), # noqa
encode=partial(self.encode, pretty=pretty),
)

if show_graphiql:
graphiql_data = GraphiQLData(
result=result,
query=getattr(all_params[0], "query"),
variables=getattr(all_params[0], "variables"),
operation_name=getattr(all_params[0], "operation_name"),
query=all_params[0].query,
variables=all_params[0].variables,
operation_name=all_params[0].operation_name,
subscription_url=self.subscriptions,
headers=self.headers,
)
Expand All @@ -169,7 +169,7 @@ async def dispatch_request(self):
except HttpQueryError as e:
parsed_error = GraphQLError(e.message)
return Response(
self.encode(dict(errors=[self.format_error(parsed_error)])),
self.encode({"errors": [self.format_error(parsed_error)]}),
status=e.status_code,
headers=e.headers,
content_type="application/json",
Expand Down
14 changes: 7 additions & 7 deletions graphql_server/sanic/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GraphQLView(HTTPMethodView):
encode = staticmethod(json_encode)

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
super().__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
Expand Down Expand Up @@ -130,7 +130,7 @@ async def __handle_request(self, request, *args, **kwargs):
*(
ex
if ex is not None and is_awaitable(ex)
else wrap_in_async(lambda: ex)()
else wrap_in_async(lambda x: x)(ex)
for ex in execution_results
)
)
Expand All @@ -141,15 +141,15 @@ async def __handle_request(self, request, *args, **kwargs):
exec_res,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty), # noqa: ignore
encode=partial(self.encode, pretty=pretty),
)

if show_graphiql:
graphiql_data = GraphiQLData(
result=result,
query=getattr(all_params[0], "query"),
variables=getattr(all_params[0], "variables"),
operation_name=getattr(all_params[0], "operation_name"),
query=all_params[0].query,
variables=all_params[0].variables,
operation_name=all_params[0].operation_name,
subscription_url=self.subscriptions,
headers=self.headers,
)
Expand Down Expand Up @@ -181,7 +181,7 @@ async def __handle_request(self, request, *args, **kwargs):
except HttpQueryError as e:
parsed_error = GraphQLError(e.message)
return HTTPResponse(
self.encode(dict(errors=[self.format_error(parsed_error)])),
self.encode({"errors": [self.format_error(parsed_error)]}),
status=e.status_code,
headers=e.headers,
content_type="application/json",
Expand Down
12 changes: 6 additions & 6 deletions graphql_server/webob/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GraphQLView:
encode = staticmethod(json_encode)

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
super().__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
Expand Down Expand Up @@ -122,15 +122,15 @@ def dispatch_request(self, request):
execution_results,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty), # noqa
encode=partial(self.encode, pretty=pretty),
)

if show_graphiql:
graphiql_data = GraphiQLData(
result=result,
query=getattr(all_params[0], "query"),
variables=getattr(all_params[0], "variables"),
operation_name=getattr(all_params[0], "operation_name"),
query=all_params[0].query,
variables=all_params[0].variables,
operation_name=all_params[0].operation_name,
subscription_url=self.subscriptions,
headers=self.headers,
)
Expand Down Expand Up @@ -165,7 +165,7 @@ def dispatch_request(self, request):
except HttpQueryError as e:
parsed_error = GraphQLError(e.message)
return Response(
self.encode(dict(errors=[self.format_error(parsed_error)])),
self.encode({"errors": [self.format_error(parsed_error)]}),
status=e.status_code,
charset=self.charset,
headers=e.headers or {},
Expand Down
13 changes: 0 additions & 13 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
[flake8]
exclude = docs
max-line-length = 88
ignore = E203, E501, W503

[isort]
known_first_party=graphql_server
profile=black
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True

[tool:pytest]
norecursedirs = venv .venv .tox .git .cache .mypy_cache .pytest_cache
markers = asyncio
Expand Down
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
]

dev_requires = [
"flake8>=6,<7",
"isort>=5,<6",
"black>=23.9,<23.10",
"mypy>=1.6,<1.7",
"check-manifest>=0.47,<1",
] + tests_requires

install_flask_requires = [
Expand Down
Loading

0 comments on commit 9180278

Please sign in to comment.