Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update pre-commit and use ruff for linting/formating #105

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 13 additions & 47 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,57 +1,23 @@
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
language_version: python

- repo: https://github.com/psf/black
rev: 22.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: black
args: ["--safe"]
language_version: python

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
language_version: python
args: [
# E501 let black handle all line length decisions
# W503 black conflicts with "line break before operator" rule
# E203 black conflicts with "whitespace before ':'" rule
"--ignore=E501,W503,E203,C901",
]
- repo: https://github.com/chewse/pre-commit-mirrors-pydocstyle
# 2.1.1
rev: v2.1.1
hooks:
- id: pydocstyle
language_version: python
exclude: ".*(test|scripts).*"
args:
[
# Check for docstring presence only
"--select=D1",
]
# Don't require docstrings for tests
# '--match=(?!test).*\.py']
# -
# repo: https://github.com/pre-commit/mirrors-mypy
# rev: v0.770
# hooks:
# - id: mypy
# language_version: python3.8
# args: [--no-strict-optional, --ignore-missing-imports]
- id: ruff
args: ["--fix"]
- id: ruff-format

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.3.0
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
- id: pydocstyle
- id: mypy
language_version: python
exclude: ".*(test|scripts).*"
#args: [
# Don't require docstrings for tests
#'--match=(?!test|scripts).*\.py',
#]
exclude: tests/.*
additional_dependencies:
- types-requests
- types-attrs
29 changes: 23 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
[flake8]
ignore = "D203"
exclude = [".git", "__pycache__", "docs/source/conf.py", "build", "dist"]
max-complexity = 12
max-line-length = 90

[tool.isort]
profile = "black"
known_first_party = "stac_fastapi.pgstac"
known_third_party = ["rasterio", "stac-pydantic", "sqlalchemy", "geoalchemy2", "fastapi", "stac_fastapi"]
sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]

[tool.mypy]
ignore_missing_imports = true
namespace_packages = true
explicit_package_bases = true
exclude = ["tests", ".venv"]

[tool.ruff]
line-length = 90

[tool.ruff.lint]
select = [
"C",
"E",
"F",
"W",
"B",
]
ignore = [
"E203", # line too long, handled by black
"E501", # do not perform function calls in argument defaults
"B028", # No explicit `stacklevel` keyword argument found
]
1 change: 1 addition & 0 deletions scripts/ingest_joplin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Ingest sample data during docker-compose"""

import json
import sys
from pathlib import Path
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,5 @@
install_requires=install_requires,
tests_require=extra_reqs["dev"],
extras_require=extra_reqs,
entry_points={
"console_scripts": ["stac-fastapi-pgstac=stac_fastapi.pgstac.app:run"]
},
entry_points={"console_scripts": ["stac-fastapi-pgstac=stac_fastapi.pgstac.app:run"]},
)
9 changes: 4 additions & 5 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@

if enabled_extensions := os.getenv("ENABLED_EXTENSIONS"):
extensions = [
extensions_map[extension_name]
for extension_name in enabled_extensions.split(",")
extensions_map[extension_name] for extension_name in enabled_extensions.split(",")
]
else:
extensions = list(extensions_map.values())
Expand All @@ -57,7 +56,7 @@
api = StacApi(
settings=settings,
extensions=extensions,
client=CoreCrudClient(post_request_model=post_request_model),
client=CoreCrudClient(post_request_model=post_request_model), # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why I was getting a Mypy error here 🤷

response_class=ORJSONResponse,
search_get_request_model=create_get_request_model(extensions),
search_post_request_model=post_request_model,
Expand Down Expand Up @@ -90,8 +89,8 @@ def run():
reload=settings.reload,
root_path=os.getenv("UVICORN_ROOT_PATH", ""),
)
except ImportError:
raise RuntimeError("Uvicorn must be installed in order to use command")
except ImportError as e:
raise RuntimeError("Uvicorn must be installed in order to use command") from e


if __name__ == "__main__":
Expand Down
16 changes: 9 additions & 7 deletions stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Item crud client."""

import re
from datetime import datetime
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -168,10 +169,10 @@ async def _search_base(
req=search_request_json,
)
items = await conn.fetchval(q, *p)
except InvalidDatetimeFormatError:
except InvalidDatetimeFormatError as e:
raise InvalidQueryParameter(
f"Datetime parameter {search_request.datetime} is invalid."
)
) from e

next: Optional[str] = items.pop("next", None)
prev: Optional[str] = items.pop("prev", None)
Expand Down Expand Up @@ -203,8 +204,8 @@ async def _add_item_links(
and all([collection_id, item_id])
):
feature["links"] = await ItemLinks(
collection_id=collection_id,
item_id=item_id,
collection_id=collection_id, # type: ignore
item_id=item_id, # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collection_id and item_id should always be define but for some reason mypy thinks it can be None

request=request,
).get_links(extra_links=feature.get("links"))

Expand Down Expand Up @@ -251,7 +252,7 @@ async def item_collection(
bbox: Optional[List[NumType]] = None,
datetime: Optional[Union[str, datetime]] = None,
limit: Optional[int] = None,
token: str = None,
token: Optional[str] = None,
**kwargs,
) -> ItemCollection:
"""Get all items from a specific collection.
Expand Down Expand Up @@ -336,7 +337,7 @@ async def post_search(
item_collection = await self._search_base(search_request, request=request)
return ItemCollection(**item_collection)

async def get_search(
async def get_search( # noqa: C901
self,
request: Request,
collections: Optional[List[str]] = None,
Expand Down Expand Up @@ -428,5 +429,6 @@ async def get_search(
except ValidationError as e:
raise HTTPException(
status_code=400, detail=f"Invalid parameters provided {e}"
)
) from e

return await self.post_search(search_request, request=request)
22 changes: 16 additions & 6 deletions stac_fastapi/pgstac/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

import json
from contextlib import asynccontextmanager, contextmanager
from typing import AsyncIterator, Callable, Dict, Generator, Literal, Union
from typing import (
AsyncIterator,
Callable,
Dict,
Generator,
List,
Literal,
Optional,
Union,
)

import attr
import orjson
Expand Down Expand Up @@ -36,14 +45,17 @@ async def con_init(conn):
ConnectionGetter = Callable[[Request, Literal["r", "w"]], AsyncIterator[Connection]]


async def connect_to_db(app: FastAPI, get_conn: ConnectionGetter = None) -> None:
async def connect_to_db(
app: FastAPI, get_conn: Optional[ConnectionGetter] = None
) -> None:
"""Create connection pools & connection retriever on application."""
settings = app.state.settings
if app.state.settings.testing:
readpool = writepool = settings.testing_connection_string
else:
readpool = settings.reader_connection_string
writepool = settings.writer_connection_string

db = DB()
app.state.readpool = await db.create_pool(readpool, settings)
app.state.writepool = await db.create_pool(writepool, settings)
Expand All @@ -62,15 +74,13 @@ async def get_connection(
readwrite: Literal["r", "w"] = "r",
) -> AsyncIterator[Connection]:
"""Retrieve connection from database conection pool."""
pool = (
request.app.state.writepool if readwrite == "w" else request.app.state.readpool
)
pool = request.app.state.writepool if readwrite == "w" else request.app.state.readpool
with translate_pgstac_errors():
async with pool.acquire() as conn:
yield conn


async def dbfunc(conn: Connection, func: str, arg: Union[str, Dict]):
async def dbfunc(conn: Connection, func: str, arg: Union[str, Dict, List]):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

"""Wrap PLPGSQL Functions.

Keyword arguments:
Expand Down
1 change: 1 addition & 0 deletions stac_fastapi/pgstac/extensions/filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Get Queryables."""

from typing import Any, Optional

from buildpg import render
Expand Down
Loading
Loading