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

⬆️ Upgrade and fully migrate to Ruff, remove isort, includes a couple of tweaks suggested by the new version of Ruff #9660

Merged
merged 8 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,11 @@ repos:
- --py3-plus
- --keep-runtime-typing
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.254
rev: v0.0.272
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- id: isort
name: isort (cython)
types: [cython]
- id: isort
name: isort (pyi)
types: [pyi]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
Expand Down
8 changes: 3 additions & 5 deletions fastapi/openapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def get_openapi_operation_metadata(
file_name = getattr(route.endpoint, "__globals__", {}).get("__file__")
if file_name:
message += f" at {file_name}"
warnings.warn(message)
warnings.warn(message, stacklevel=1)
operation_ids.add(operation_id)
operation["operationId"] = operation_id
if route.deprecated:
Expand Down Expand Up @@ -332,10 +332,8 @@ def get_openapi_path(
openapi_response["description"] = description
http422 = str(HTTP_422_UNPROCESSABLE_ENTITY)
if (all_route_params or route.body_field) and not any(
[
status in operation["responses"]
for status in [http422, "4XX", "default"]
]
status in operation["responses"]
for status in [http422, "4XX", "default"]
):
operation["responses"][http422] = {
"description": "Validation Error",
Expand Down
13 changes: 9 additions & 4 deletions fastapi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
solve_dependencies,
)
from fastapi.encoders import DictIntStrAny, SetIntStr, jsonable_encoder
from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError
from fastapi.exceptions import (
FastAPIError,
RequestValidationError,
WebSocketRequestValidationError,
)
from fastapi.types import DecoratedCallable
from fastapi.utils import (
create_cloned_field,
Expand All @@ -48,14 +52,15 @@
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.routing import BaseRoute, Match
from starlette.routing import Mount as Mount # noqa
from starlette.routing import (
BaseRoute,
Match,
compile_path,
get_name,
request_response,
websocket_session,
)
from starlette.routing import Mount as Mount # noqa
from starlette.types import ASGIApp, Lifespan, Scope
from starlette.websockets import WebSocket

Expand Down Expand Up @@ -763,7 +768,7 @@ def include_router(
path = getattr(r, "path") # noqa: B009
name = getattr(r, "name", "unknown")
if path is not None and not path:
raise Exception(
raise FastAPIError(
f"Prefix and path cannot be both empty (path operation: {name})"
)
if responses is None:
Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ all = [
[tool.hatch.version]
path = "fastapi/__init__.py"

[tool.isort]
profile = "black"
known_third_party = ["fastapi", "pydantic", "starlette"]

[tool.mypy]
strict = true

Expand Down Expand Up @@ -125,7 +121,7 @@ select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
# "I", # isort
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
Expand Down
3 changes: 1 addition & 2 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
pytest >=7.1.3,<8.0.0
coverage[toml] >= 6.5.0,< 8.0
mypy ==1.3.0
ruff ==0.0.138
ruff ==0.0.272
black == 23.1.0
isort >=5.0.6,<6.0.0
httpx >=0.23.0,<0.24.0
email_validator >=1.1.1,<2.0.0
# TODO: once removing databases from tutorial, upgrade SQLAlchemy
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-e .[all]
-r requirements-tests.txt
-r requirements-docs.txt
ruff ==0.0.138
uvicorn[standard] >=0.12.0,<0.21.0
pre-commit >=2.17.0,<3.0.0
1 change: 0 additions & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ set -x

ruff fastapi tests docs_src scripts --fix
black fastapi tests docs_src scripts
isort fastapi tests docs_src scripts
1 change: 0 additions & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ set -x
mypy fastapi
ruff fastapi tests docs_src scripts
black fastapi tests --check
isort fastapi tests docs_src scripts --check-only
3 changes: 2 additions & 1 deletion tests/test_empty_router.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from fastapi import APIRouter, FastAPI
from fastapi.exceptions import FastAPIError
from fastapi.testclient import TestClient

app = FastAPI()
Expand Down Expand Up @@ -31,5 +32,5 @@ def test_use_empty():

def test_include_empty():
# if both include and router.path are empty - it should raise exception
with pytest.raises(Exception):
with pytest.raises(FastAPIError):
app.include_router(router)