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

stac_fastapi/api/stac_fastapi/api/openapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def __init__(self, *args, **kwargs):
2828
def update_openapi(app: FastAPI) -> FastAPI:
2929
"""Update OpenAPI response content-type.
3030
31-
This function modifies the openapi route to comply with the STAC API spec's
32-
required content-type response header.
31+
This function modifies the openapi route to comply with the STAC API spec's required
32+
content-type response header.
3333
"""
3434
# Find the route for the openapi_url in the app
3535
openapi_route: Route = next(

stac_fastapi/api/stac_fastapi/api/routes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""route factories."""
1+
"""Route factories."""
22
import functools
33
import inspect
44
from typing import Any, Callable, Dict, List, Optional, Type, TypedDict, Union
@@ -104,7 +104,6 @@ def add_route_dependencies(
104104
"""
105105
for scope in scopes:
106106
for route in routes:
107-
108107
match, _ = route.matches({"type": "http", **scope})
109108
if match != Match.FULL:
110109
continue
@@ -120,7 +119,8 @@ def add_route_dependencies(
120119
)
121120

122121
# Register dependencies directly on route so that they aren't ignored if
123-
# the routes are later associated with an app (e.g. app.include_router(router))
122+
# the routes are later associated with an app (e.g.
123+
# app.include_router(router))
124124
# https://github.com/tiangolo/fastapi/blob/58ab733f19846b4875c5b79bfb1f4d1cb7f4823f/fastapi/applications.py#L337-L360
125125
# https://github.com/tiangolo/fastapi/blob/58ab733f19846b4875c5b79bfb1f4d1cb7f4823f/fastapi/routing.py#L677-L678
126126
route.dependencies.extend(dependencies)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
"""library version."""
1+
"""Library version."""
22
__version__ = "2.4.6"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""extensions submodule."""
1+
"""Extensions submodule."""

stac_fastapi/extensions/stac_fastapi/extensions/core/context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""context extension."""
1+
"""Context extension."""
22
from typing import List, Optional
33

44
import attr
@@ -11,9 +11,9 @@
1111
class ContextExtension(ApiExtension):
1212
"""Context Extension.
1313
14-
The Context extension adds a JSON object to ItemCollection responses (`/search`, `/collections/{collection_id}/items`)
15-
which includes the number of items matched, returned, and the limit requested.
16-
14+
The Context extension adds a JSON object to ItemCollection responses (`/search`,
15+
`/collections/{collection_id}/items`) which includes the number of items matched,
16+
returned, and the limit requested.
1717
https://github.com/radiantearth/stac-api-spec/blob/master/item-search/README.md#context
1818
"""
1919

stac_fastapi/extensions/stac_fastapi/extensions/core/fields/fields.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""fields extension."""
1+
"""Fields extension."""
22
from typing import List, Optional, Set
33

44
import attr
@@ -13,17 +13,19 @@
1313
class FieldsExtension(ApiExtension):
1414
"""Fields Extension.
1515
16-
The Fields extension adds functionality to the `/search` endpoint which allows the caller to include or exclude
17-
specific from the API response. Registering this extension with the application has the added effect of removing
18-
the `ItemCollection` response model from the `/search` endpoint, as the Fields extension allows the API to return
19-
potentially invalid responses by excluding fields which are required by the STAC spec, such as geometry.
16+
The Fields extension adds functionality to the `/search` endpoint which
17+
allows the caller to include or exclude specific from the API response.
18+
Registering this extension with the application has the added effect of
19+
removing the `ItemCollection` response model from the `/search` endpoint, as
20+
the Fields extension allows the API to return potentially invalid responses
21+
by excluding fields which are required by the STAC spec, such as geometry.
2022
2123
https://github.com/radiantearth/stac-api-spec/blob/master/item-search/README.md#fields
2224
2325
Attributes:
2426
default_includes (set): defines the default set of included fields.
25-
conformance_classes (list): Defines the list of conformance classes for the extension
26-
27+
conformance_classes (list): Defines the list of conformance classes for
28+
the extension
2729
"""
2830

2931
GET = FieldsExtensionGetRequest

stac_fastapi/extensions/stac_fastapi/extensions/core/fields/request.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class PostFieldsExtension(BaseModel):
2424
def _get_field_dict(fields: Optional[Set[str]]) -> Dict:
2525
"""Pydantic include/excludes notation.
2626
27-
Internal method to create a dictionary for advanced include or exclude of pydantic fields on model export
27+
Internal method to create a dictionary for advanced include or exclude
28+
of pydantic fields on model export
2829
Ref: https://pydantic-docs.helpmanual.io/usage/exporting_models/#advanced-include-and-exclude
2930
"""
3031
field_dict = {}
@@ -44,8 +45,8 @@ def _get_field_dict(fields: Optional[Set[str]]) -> Dict:
4445
def filter_fields(self) -> Dict:
4546
"""Create pydantic include/exclude expression.
4647
47-
Create dictionary of fields to include/exclude on model export based on the included and excluded fields passed
48-
to the API
48+
Create dictionary of fields to include/exclude on model export based on
49+
the included and excluded fields passed to the API
4950
Ref: https://pydantic-docs.helpmanual.io/usage/exporting_models/#advanced-include-and-exclude
5051
"""
5152
# Always include default_includes, even if they

stac_fastapi/extensions/stac_fastapi/extensions/core/filter/filter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
class FilterConformanceClasses(str, Enum):
1919
"""Conformance classes for the Filter extension.
2020
21-
See https://github.com/radiantearth/stac-api-spec/tree/v1.0.0-rc.1/fragments/filter
21+
See
22+
https://github.com/radiantearth/stac-api-spec/tree/v1.0.0-rc.1/fragments/filter
2223
"""
2324

2425
FILTER = "http://www.opengis.net/spec/ogcapi-features-3/1.0/conf/filter"
@@ -50,8 +51,9 @@ class FilterConformanceClasses(str, Enum):
5051
class FilterExtension(ApiExtension):
5152
"""Filter Extension.
5253
53-
The filter extension adds several endpoints which allow the retrieval of queryables and
54-
provides an expressive mechanism for searching based on Item Attributes:
54+
The filter extension adds several endpoints which allow the retrieval of
55+
queryables and provides an expressive mechanism for searching based on Item
56+
Attributes:
5557
GET /queryables
5658
GET /collections/{collection_id}/queryables
5759
@@ -60,7 +62,6 @@ class FilterExtension(ApiExtension):
6062
Attributes:
6163
client: Queryables endpoint logic
6264
conformance_classes: Conformance classes provided by the extension
63-
6465
"""
6566

6667
GET = FilterExtensionGetRequest

0 commit comments

Comments
 (0)