mirror of https://github.com/tiangolo/fastapi.git
⬆️ Upgrade and fully migrate to Ruff, remove isort, includes a couple of tweaks suggested by the new version of Ruff (#9660)
This commit is contained in:
parent
ba882c10fe
commit
7167c77a18
|
|
@ -21,22 +21,11 @@ repos:
|
||||||
- --py3-plus
|
- --py3-plus
|
||||||
- --keep-runtime-typing
|
- --keep-runtime-typing
|
||||||
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
||||||
rev: v0.0.254
|
rev: v0.0.272
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
args:
|
args:
|
||||||
- --fix
|
- --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
|
- repo: https://github.com/psf/black
|
||||||
rev: 23.1.0
|
rev: 23.1.0
|
||||||
hooks:
|
hooks:
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ def get_openapi_operation_metadata(
|
||||||
file_name = getattr(route.endpoint, "__globals__", {}).get("__file__")
|
file_name = getattr(route.endpoint, "__globals__", {}).get("__file__")
|
||||||
if file_name:
|
if file_name:
|
||||||
message += f" at {file_name}"
|
message += f" at {file_name}"
|
||||||
warnings.warn(message)
|
warnings.warn(message, stacklevel=1)
|
||||||
operation_ids.add(operation_id)
|
operation_ids.add(operation_id)
|
||||||
operation["operationId"] = operation_id
|
operation["operationId"] = operation_id
|
||||||
if route.deprecated:
|
if route.deprecated:
|
||||||
|
|
@ -332,10 +332,8 @@ def get_openapi_path(
|
||||||
openapi_response["description"] = description
|
openapi_response["description"] = description
|
||||||
http422 = str(HTTP_422_UNPROCESSABLE_ENTITY)
|
http422 = str(HTTP_422_UNPROCESSABLE_ENTITY)
|
||||||
if (all_route_params or route.body_field) and not any(
|
if (all_route_params or route.body_field) and not any(
|
||||||
[
|
status in operation["responses"]
|
||||||
status in operation["responses"]
|
for status in [http422, "4XX", "default"]
|
||||||
for status in [http422, "4XX", "default"]
|
|
||||||
]
|
|
||||||
):
|
):
|
||||||
operation["responses"][http422] = {
|
operation["responses"][http422] = {
|
||||||
"description": "Validation Error",
|
"description": "Validation Error",
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,11 @@ from fastapi.dependencies.utils import (
|
||||||
solve_dependencies,
|
solve_dependencies,
|
||||||
)
|
)
|
||||||
from fastapi.encoders import DictIntStrAny, SetIntStr, jsonable_encoder
|
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.types import DecoratedCallable
|
||||||
from fastapi.utils import (
|
from fastapi.utils import (
|
||||||
create_cloned_field,
|
create_cloned_field,
|
||||||
|
|
@ -48,14 +52,15 @@ from starlette.concurrency import run_in_threadpool
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import JSONResponse, Response
|
from starlette.responses import JSONResponse, Response
|
||||||
from starlette.routing import BaseRoute, Match
|
|
||||||
from starlette.routing import Mount as Mount # noqa
|
|
||||||
from starlette.routing import (
|
from starlette.routing import (
|
||||||
|
BaseRoute,
|
||||||
|
Match,
|
||||||
compile_path,
|
compile_path,
|
||||||
get_name,
|
get_name,
|
||||||
request_response,
|
request_response,
|
||||||
websocket_session,
|
websocket_session,
|
||||||
)
|
)
|
||||||
|
from starlette.routing import Mount as Mount # noqa
|
||||||
from starlette.types import ASGIApp, Lifespan, Scope
|
from starlette.types import ASGIApp, Lifespan, Scope
|
||||||
from starlette.websockets import WebSocket
|
from starlette.websockets import WebSocket
|
||||||
|
|
||||||
|
|
@ -763,7 +768,7 @@ class APIRouter(routing.Router):
|
||||||
path = getattr(r, "path") # noqa: B009
|
path = getattr(r, "path") # noqa: B009
|
||||||
name = getattr(r, "name", "unknown")
|
name = getattr(r, "name", "unknown")
|
||||||
if path is not None and not path:
|
if path is not None and not path:
|
||||||
raise Exception(
|
raise FastAPIError(
|
||||||
f"Prefix and path cannot be both empty (path operation: {name})"
|
f"Prefix and path cannot be both empty (path operation: {name})"
|
||||||
)
|
)
|
||||||
if responses is None:
|
if responses is None:
|
||||||
|
|
|
||||||
|
|
@ -66,10 +66,6 @@ all = [
|
||||||
[tool.hatch.version]
|
[tool.hatch.version]
|
||||||
path = "fastapi/__init__.py"
|
path = "fastapi/__init__.py"
|
||||||
|
|
||||||
[tool.isort]
|
|
||||||
profile = "black"
|
|
||||||
known_third_party = ["fastapi", "pydantic", "starlette"]
|
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
strict = true
|
strict = true
|
||||||
|
|
||||||
|
|
@ -125,7 +121,7 @@ select = [
|
||||||
"E", # pycodestyle errors
|
"E", # pycodestyle errors
|
||||||
"W", # pycodestyle warnings
|
"W", # pycodestyle warnings
|
||||||
"F", # pyflakes
|
"F", # pyflakes
|
||||||
# "I", # isort
|
"I", # isort
|
||||||
"C", # flake8-comprehensions
|
"C", # flake8-comprehensions
|
||||||
"B", # flake8-bugbear
|
"B", # flake8-bugbear
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,8 @@
|
||||||
pytest >=7.1.3,<8.0.0
|
pytest >=7.1.3,<8.0.0
|
||||||
coverage[toml] >= 6.5.0,< 8.0
|
coverage[toml] >= 6.5.0,< 8.0
|
||||||
mypy ==1.3.0
|
mypy ==1.3.0
|
||||||
ruff ==0.0.138
|
ruff ==0.0.272
|
||||||
black == 23.1.0
|
black == 23.1.0
|
||||||
isort >=5.0.6,<6.0.0
|
|
||||||
httpx >=0.23.0,<0.24.0
|
httpx >=0.23.0,<0.24.0
|
||||||
email_validator >=1.1.1,<2.0.0
|
email_validator >=1.1.1,<2.0.0
|
||||||
# TODO: once removing databases from tutorial, upgrade SQLAlchemy
|
# TODO: once removing databases from tutorial, upgrade SQLAlchemy
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
-e .[all]
|
-e .[all]
|
||||||
-r requirements-tests.txt
|
-r requirements-tests.txt
|
||||||
-r requirements-docs.txt
|
-r requirements-docs.txt
|
||||||
ruff ==0.0.138
|
|
||||||
uvicorn[standard] >=0.12.0,<0.21.0
|
uvicorn[standard] >=0.12.0,<0.21.0
|
||||||
pre-commit >=2.17.0,<3.0.0
|
pre-commit >=2.17.0,<3.0.0
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,3 @@ set -x
|
||||||
|
|
||||||
ruff fastapi tests docs_src scripts --fix
|
ruff fastapi tests docs_src scripts --fix
|
||||||
black fastapi tests docs_src scripts
|
black fastapi tests docs_src scripts
|
||||||
isort fastapi tests docs_src scripts
|
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,3 @@ set -x
|
||||||
mypy fastapi
|
mypy fastapi
|
||||||
ruff fastapi tests docs_src scripts
|
ruff fastapi tests docs_src scripts
|
||||||
black fastapi tests --check
|
black fastapi tests --check
|
||||||
isort fastapi tests docs_src scripts --check-only
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import APIRouter, FastAPI
|
from fastapi import APIRouter, FastAPI
|
||||||
|
from fastapi.exceptions import FastAPIError
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
@ -31,5 +32,5 @@ def test_use_empty():
|
||||||
|
|
||||||
def test_include_empty():
|
def test_include_empty():
|
||||||
# if both include and router.path are empty - it should raise exception
|
# if both include and router.path are empty - it should raise exception
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(FastAPIError):
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue