Skip to content

Commit ced2e43

Browse files
jpolchlogadomski
andauthored
Use ruff for pre-commit checks; replaces isort and flake8 (#549)
* Use ruff for pre-commit checks; replaces isort and flake8 * Update changelog to include link to this PR * feat: switch to ruff * feat: apply formatting changes * deps: pre-commit autoupdate * fixup some black formatting * fixup, docs: changelog * fix: add I to ruff rules --------- Co-authored-by: Pete Gadomski <[email protected]>
1 parent 74a7909 commit ced2e43

37 files changed

+185
-182
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,10 @@
11
repos:
2-
- repo: https://github.com/PyCQA/isort
3-
rev: 5.12.0
2+
- repo: https://github.com/charliermarsh/ruff-pre-commit
3+
rev: "v0.0.267"
44
hooks:
5-
- id: isort
6-
language_version: python3.8
5+
- id: ruff
6+
args: [--fix, --exit-non-zero-on-fix]
77
- repo: https://github.com/psf/black
8-
rev: 22.12.0
8+
rev: 23.3.0
99
hooks:
1010
- id: black
11-
args: ["--safe"]
12-
language_version: python3.8
13-
- repo: https://github.com/pycqa/flake8
14-
rev: 6.0.0
15-
hooks:
16-
- id: flake8
17-
language_version: python3.8
18-
args: [
19-
# E501 let black handle all line length decisions
20-
# W503 black conflicts with "line break before operator" rule
21-
# E203 black conflicts with "whitespace before ':'" rule
22-
"--ignore=E501,W503,E203,C901",
23-
]
24-
- repo: https://github.com/chewse/pre-commit-mirrors-pydocstyle
25-
# 2.1.1
26-
rev: v2.1.1
27-
hooks:
28-
- id: pydocstyle
29-
language_version: python3.8
30-
exclude: ".*(test|alembic|scripts).*"
31-
args:
32-
[
33-
# Check for docstring presence only
34-
"--select=D1",
35-
]
36-
# Don't require docstrings for tests
37-
# '--match=(?!test).*\.py']
38-
# -
39-
# repo: https://github.com/pre-commit/mirrors-mypy
40-
# rev: v0.770
41-
# hooks:
42-
# - id: mypy
43-
# language_version: python3.8
44-
# args: [--no-strict-optional, --ignore-missing-imports]
45-
- repo: https://github.com/PyCQA/pydocstyle
46-
rev: 6.3.0
47-
hooks:
48-
- id: pydocstyle
49-
language_version: python3.8
50-
exclude: ".*(test|alembic|scripts).*"
51-
#args: [
52-
# Don't require docstrings for tests
53-
#'--match=(?!test|alembic|scripts).*\.py',
54-
#]

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
### Added
66

7-
* Add support for POSTing ItemCollections to the /items endpoint of the Transaction Extension ([#547](https://github.com/stac-utils/stac-fastapi/pull/574)
7+
* Add support for POSTing ItemCollections to the /items endpoint of the Transaction Extension ([#547](https://github.com/stac-utils/stac-fastapi/pull/574))
8+
9+
### Changed
10+
11+
* flake8, isort, and pydocstyle replaced by ruff for pre-commit checks ([#549](https://github.com/stac-utils/stac-fastapi/pull/549))
812

913
## [2.4.6] - 2023-05-09
1014

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $ pip install -e stac_fastapi/api[dev]
1212

1313
**Python3.8 only**
1414

15-
This repo is set to use `pre-commit` to run *isort*, *flake8*, *pydocstring*, *black* ("uncompromising Python code formatter") and mypy when committing new code.
15+
This repo is set to use `pre-commit` to run *ruff*, *pydocstring*, *black* ("uncompromising Python code formatter") and mypy when committing new code.
1616

1717
```bash
1818
$ pre-commit install

pyproject.toml

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
[flake8]
2-
ignore = "D203"
3-
exclude = [".git", "__pycache__", "docs/source/conf.py", "build", "dist"]
4-
max-complexity = 12
5-
max-line-length = 90
1+
[tool.ruff]
2+
line-length = 90
3+
select = [
4+
"C9",
5+
"D1",
6+
"E",
7+
"F",
8+
"I",
9+
"W",
10+
]
611

7-
[tool.isort]
8-
profile = "black"
9-
known_first_party = "stac_fastapi"
10-
known_third_party = ["stac-pydantic", "fastapi"]
11-
sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
12+
[tool.ruff.per-file-ignores]
13+
"**/tests/**/*.py" = ["D1"]
14+
15+
[tool.ruff.isort]
16+
known-first-party = ["stac_fastapi"]
17+
known-third-party = ["stac_pydantic", "fastapi"]
18+
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]
19+
20+
[tool.black]
21+
target-version = ["py38", "py39", "py310", "py311"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""api submodule."""
1+
"""Api submodule."""

stac_fastapi/api/stac_fastapi/api/app.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""fastapi app creation."""
1+
"""Fastapi app creation."""
22
from typing import Any, Dict, List, Optional, Tuple, Type, Union
33

44
import attr
@@ -37,22 +37,32 @@
3737
class StacApi:
3838
"""StacApi factory.
3939
40-
Factory for creating a STAC-compliant FastAPI application. After instantation, the application is accessible from
41-
the `StacApi.app` attribute.
40+
Factory for creating a STAC-compliant FastAPI application. After
41+
instantation, the application is accessible from the `StacApi.app` attribute.
4242
4343
Attributes:
4444
settings:
45-
API settings and configuration, potentially using environment variables. See https://pydantic-docs.helpmanual.io/usage/settings/.
45+
API settings and configuration, potentially using environment
46+
variables. See https://pydantic-docs.helpmanual.io/usage/settings/.
4647
client:
47-
A subclass of `stac_api.clients.BaseCoreClient`. Defines the application logic which is injected into the API.
48+
A subclass of `stac_api.clients.BaseCoreClient`. Defines the
49+
application logic which is injected into the API.
4850
extensions:
49-
API extensions to include with the application. This may include official STAC extensions as well as third-party add ons.
51+
API extensions to include with the application. This may include
52+
official STAC extensions as well as third-party add ons.
5053
exceptions:
51-
Defines a global mapping between exceptions and status codes, allowing configuration of response behavior on certain exceptions (https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers).
54+
Defines a global mapping between exceptions and status codes,
55+
allowing configuration of response behavior on certain exceptions
56+
(https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers).
5257
app:
5358
The FastAPI application, defaults to a fresh application.
54-
route_dependencies (list of tuples of route scope dicts (eg `{'path': '/collections', 'method': 'POST'}`) and list of dependencies (e.g. `[Depends(oauth2_scheme)]`)):
55-
Applies specified dependencies to specified routes. This is useful for applying custom auth requirements to routes defined elsewhere in the application.
59+
route_dependencies:
60+
List of tuples of route scope dicts (eg `{'path':
61+
'/collections', 'method': 'POST'}`) and list of dependencies (e.g.
62+
`[Depends(oauth2_scheme)]`)). Applies specified dependencies to
63+
specified routes. This is useful
64+
for applying custom auth requirements to routes defined elsewhere in
65+
the application.
5666
"""
5767

5868
settings: ApiSettings = attr.ib()
@@ -341,8 +351,11 @@ def add_route_dependencies(
341351
"""Add custom dependencies to routes.
342352
343353
Args:
344-
scopes: list of scopes. Each scope should be a dict with a `path` and `method` property.
345-
dependencies: list of [FastAPI dependencies](https://fastapi.tiangolo.com/tutorial/dependencies/) to apply to each scope.
354+
scopes: list of scopes. Each scope should be a dict with a `path`
355+
and `method` property.
356+
dependencies: list of [FastAPI
357+
dependencies](https://fastapi.tiangolo.com/tutorial/dependencies/)
358+
to apply to each scope.
346359
347360
Returns:
348361
None

stac_fastapi/api/stac_fastapi/api/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44

55
# TODO: Move to stac-pydantic
6-
# Does that make sense now? The shift to json schema rather than a well-known enumeration makes that less obvious.
6+
# Does that make sense now? The shift to json schema rather than a well-known
7+
# enumeration makes that less obvious.
78
class ApiExtensions(enum.Enum):
89
"""Enumeration of available stac api extensions.
910

stac_fastapi/api/stac_fastapi/api/errors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
class ErrorResponse(TypedDict):
3434
"""A JSON error response returned by the API.
3535
36-
The STAC API spec expects that `code` and `description` are both present in the payload.
36+
The STAC API spec expects that `code` and `description` are both present in
37+
the payload.
3738
3839
Attributes:
3940
code: A code representing the error, semantics are up to implementor.
@@ -77,7 +78,7 @@ def add_exception_handlers(
7778
Returns:
7879
None
7980
"""
80-
for (exc, code) in status_codes.items():
81+
for exc, code in status_codes.items():
8182
app.add_exception_handler(exc, exception_handler_factory(code))
8283

8384
# By default FastAPI will return 422 status codes for invalid requests

stac_fastapi/api/stac_fastapi/api/middleware.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""api middleware."""
1+
"""Api middleware."""
22
import re
33
import typing
44
from http.client import HTTP_PORT, HTTPS_PORT
@@ -9,8 +9,8 @@
99

1010

1111
class CORSMiddleware(_CORSMiddleware):
12-
"""
13-
Subclass of Starlette's standard CORS middleware with default values set to those reccomended by the STAC API spec.
12+
"""Subclass of Starlette's standard CORS middleware with default values set to those
13+
reccomended by the STAC API spec.
1414
1515
https://github.com/radiantearth/stac-api-spec/blob/914cf8108302e2ec734340080a45aaae4859bb63/implementation.md#cors
1616
"""
@@ -44,12 +44,11 @@ def __init__(
4444

4545

4646
class ProxyHeaderMiddleware:
47-
"""
48-
Account for forwarding headers when deriving base URL.
47+
"""Account for forwarding headers when deriving base URL.
4948
5049
Prioritise standard Forwarded header, look for non-standard X-Forwarded-* if missing.
51-
Default to what can be derived from the URL if no headers provided.
52-
Middleware updates the host header that is interpreted by starlette when deriving Request.base_url.
50+
Default to what can be derived from the URL if no headers provided. Middleware updates
51+
the host header that is interpreted by starlette when deriving Request.base_url.
5352
"""
5453

5554
def __init__(self, app: ASGIApp):

stac_fastapi/api/stac_fastapi/api/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""api request/response models."""
1+
"""Api request/response models."""
22

33
import importlib.util
44
from typing import Optional, Type, Union
@@ -44,7 +44,7 @@ def create_request_model(
4444
# Handle POST requests
4545
elif all([issubclass(m, BaseModel) for m in models]):
4646
for model in models:
47-
for (k, v) in model.__fields__.items():
47+
for k, v in model.__fields__.items():
4848
field_info = v.field_info
4949
body = Body(
5050
None

0 commit comments

Comments
 (0)