mirror of https://github.com/tiangolo/fastapi.git
✨ Use Ruff for linting (#5630)
This commit is contained in:
parent
bcd9ab95e1
commit
fa74093440
5
.flake8
5
.flake8
|
|
@ -1,5 +0,0 @@
|
||||||
[flake8]
|
|
||||||
max-line-length = 88
|
|
||||||
select = C,E,F,W,B,B9
|
|
||||||
ignore = E203, E501, W503
|
|
||||||
exclude = __init__.py
|
|
||||||
|
|
@ -18,19 +18,12 @@ repos:
|
||||||
args:
|
args:
|
||||||
- --py3-plus
|
- --py3-plus
|
||||||
- --keep-runtime-typing
|
- --keep-runtime-typing
|
||||||
- repo: https://github.com/PyCQA/autoflake
|
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
||||||
rev: v1.7.7
|
rev: v0.0.114
|
||||||
hooks:
|
hooks:
|
||||||
- id: autoflake
|
- id: ruff
|
||||||
args:
|
args:
|
||||||
- --recursive
|
- --fix
|
||||||
- --in-place
|
|
||||||
- --remove-all-unused-imports
|
|
||||||
- --remove-unused-variables
|
|
||||||
- --expand-star-imports
|
|
||||||
- --exclude
|
|
||||||
- __init__.py
|
|
||||||
- --remove-duplicate-keys
|
|
||||||
- repo: https://github.com/pycqa/isort
|
- repo: https://github.com/pycqa/isort
|
||||||
rev: 5.10.1
|
rev: 5.10.1
|
||||||
hooks:
|
hooks:
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ async def get_current_user(
|
||||||
if security_scopes.scopes:
|
if security_scopes.scopes:
|
||||||
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
||||||
else:
|
else:
|
||||||
authenticate_value = f"Bearer"
|
authenticate_value = "Bearer"
|
||||||
credentials_exception = HTTPException(
|
credentials_exception = HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Could not validate credentials",
|
detail="Could not validate credentials",
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ async def get_current_user(
|
||||||
if security_scopes.scopes:
|
if security_scopes.scopes:
|
||||||
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
||||||
else:
|
else:
|
||||||
authenticate_value = f"Bearer"
|
authenticate_value = "Bearer"
|
||||||
credentials_exception = HTTPException(
|
credentials_exception = HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Could not validate credentials",
|
detail="Could not validate credentials",
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ async def get_current_user(
|
||||||
if security_scopes.scopes:
|
if security_scopes.scopes:
|
||||||
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
||||||
else:
|
else:
|
||||||
authenticate_value = f"Bearer"
|
authenticate_value = "Bearer"
|
||||||
credentials_exception = HTTPException(
|
credentials_exception = HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Could not validate credentials",
|
detail="Could not validate credentials",
|
||||||
|
|
|
||||||
|
|
@ -426,21 +426,21 @@ def is_coroutine_callable(call: Callable[..., Any]) -> bool:
|
||||||
return inspect.iscoroutinefunction(call)
|
return inspect.iscoroutinefunction(call)
|
||||||
if inspect.isclass(call):
|
if inspect.isclass(call):
|
||||||
return False
|
return False
|
||||||
dunder_call = getattr(call, "__call__", None)
|
dunder_call = getattr(call, "__call__", None) # noqa: B004
|
||||||
return inspect.iscoroutinefunction(dunder_call)
|
return inspect.iscoroutinefunction(dunder_call)
|
||||||
|
|
||||||
|
|
||||||
def is_async_gen_callable(call: Callable[..., Any]) -> bool:
|
def is_async_gen_callable(call: Callable[..., Any]) -> bool:
|
||||||
if inspect.isasyncgenfunction(call):
|
if inspect.isasyncgenfunction(call):
|
||||||
return True
|
return True
|
||||||
dunder_call = getattr(call, "__call__", None)
|
dunder_call = getattr(call, "__call__", None) # noqa: B004
|
||||||
return inspect.isasyncgenfunction(dunder_call)
|
return inspect.isasyncgenfunction(dunder_call)
|
||||||
|
|
||||||
|
|
||||||
def is_gen_callable(call: Callable[..., Any]) -> bool:
|
def is_gen_callable(call: Callable[..., Any]) -> bool:
|
||||||
if inspect.isgeneratorfunction(call):
|
if inspect.isgeneratorfunction(call):
|
||||||
return True
|
return True
|
||||||
dunder_call = getattr(call, "__call__", None)
|
dunder_call = getattr(call, "__call__", None) # noqa: B004
|
||||||
return inspect.isgeneratorfunction(dunder_call)
|
return inspect.isgeneratorfunction(dunder_call)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -724,14 +724,14 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
|
||||||
# in case a sub-dependency is evaluated with a single unique body field
|
# in case a sub-dependency is evaluated with a single unique body field
|
||||||
# That is combined (embedded) with other body fields
|
# That is combined (embedded) with other body fields
|
||||||
for param in flat_dependant.body_params:
|
for param in flat_dependant.body_params:
|
||||||
setattr(param.field_info, "embed", True)
|
setattr(param.field_info, "embed", True) # noqa: B010
|
||||||
model_name = "Body_" + name
|
model_name = "Body_" + name
|
||||||
BodyModel: Type[BaseModel] = create_model(model_name)
|
BodyModel: Type[BaseModel] = create_model(model_name)
|
||||||
for f in flat_dependant.body_params:
|
for f in flat_dependant.body_params:
|
||||||
BodyModel.__fields__[f.name] = f
|
BodyModel.__fields__[f.name] = f
|
||||||
required = any(True for f in flat_dependant.body_params if f.required)
|
required = any(True for f in flat_dependant.body_params if f.required)
|
||||||
|
|
||||||
BodyFieldInfo_kwargs: Dict[str, Any] = dict(default=None)
|
BodyFieldInfo_kwargs: Dict[str, Any] = {"default": None}
|
||||||
if any(isinstance(f.field_info, params.File) for f in flat_dependant.body_params):
|
if any(isinstance(f.field_info, params.File) for f in flat_dependant.body_params):
|
||||||
BodyFieldInfo: Type[params.Body] = params.File
|
BodyFieldInfo: Type[params.Body] = params.File
|
||||||
elif any(isinstance(f.field_info, params.Form) for f in flat_dependant.body_params):
|
elif any(isinstance(f.field_info, params.Form) for f in flat_dependant.body_params):
|
||||||
|
|
@ -740,7 +740,7 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
|
||||||
BodyFieldInfo = params.Body
|
BodyFieldInfo = params.Body
|
||||||
|
|
||||||
body_param_media_types = [
|
body_param_media_types = [
|
||||||
getattr(f.field_info, "media_type")
|
f.field_info.media_type
|
||||||
for f in flat_dependant.body_params
|
for f in flat_dependant.body_params
|
||||||
if isinstance(f.field_info, params.Body)
|
if isinstance(f.field_info, params.Body)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -701,7 +701,7 @@ class APIRouter(routing.Router):
|
||||||
), "A path prefix must not end with '/', as the routes will start with '/'"
|
), "A path prefix must not end with '/', as the routes will start with '/'"
|
||||||
else:
|
else:
|
||||||
for r in router.routes:
|
for r in router.routes:
|
||||||
path = getattr(r, "path")
|
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 Exception(
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ test = [
|
||||||
"pytest >=7.1.3,<8.0.0",
|
"pytest >=7.1.3,<8.0.0",
|
||||||
"coverage[toml] >= 6.5.0,<7.0",
|
"coverage[toml] >= 6.5.0,<7.0",
|
||||||
"mypy ==0.982",
|
"mypy ==0.982",
|
||||||
"flake8 >=3.8.3,<6.0.0",
|
"ruff ==0.0.114",
|
||||||
"black == 22.8.0",
|
"black == 22.8.0",
|
||||||
"isort >=5.0.6,<6.0.0",
|
"isort >=5.0.6,<6.0.0",
|
||||||
"httpx >=0.23.0,<0.24.0",
|
"httpx >=0.23.0,<0.24.0",
|
||||||
|
|
@ -87,8 +87,7 @@ doc = [
|
||||||
"pyyaml >=5.3.1,<7.0.0",
|
"pyyaml >=5.3.1,<7.0.0",
|
||||||
]
|
]
|
||||||
dev = [
|
dev = [
|
||||||
"autoflake >=1.4.0,<2.0.0",
|
"ruff ==0.0.114",
|
||||||
"flake8 >=3.8.3,<6.0.0",
|
|
||||||
"uvicorn[standard] >=0.12.0,<0.19.0",
|
"uvicorn[standard] >=0.12.0,<0.19.0",
|
||||||
"pre-commit >=2.17.0,<3.0.0",
|
"pre-commit >=2.17.0,<3.0.0",
|
||||||
]
|
]
|
||||||
|
|
@ -156,3 +155,30 @@ source = [
|
||||||
"fastapi"
|
"fastapi"
|
||||||
]
|
]
|
||||||
context = '${CONTEXT}'
|
context = '${CONTEXT}'
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
select = [
|
||||||
|
"E", # pycodestyle errors
|
||||||
|
"W", # pycodestyle warnings
|
||||||
|
"F", # pyflakes
|
||||||
|
# "I", # isort
|
||||||
|
"C", # flake8-comprehensions
|
||||||
|
"B", # flake8-bugbear
|
||||||
|
]
|
||||||
|
ignore = [
|
||||||
|
"E501", # line too long, handled by black
|
||||||
|
"B008", # do not perform function calls in argument defaults
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.ruff.per-file-ignores]
|
||||||
|
"__init__.py" = ["F401"]
|
||||||
|
"docs_src/dependencies/tutorial007.py" = ["F821"]
|
||||||
|
"docs_src/dependencies/tutorial008.py" = ["F821"]
|
||||||
|
"docs_src/dependencies/tutorial009.py" = ["F821"]
|
||||||
|
"docs_src/dependencies/tutorial010.py" = ["F821"]
|
||||||
|
"docs_src/custom_response/tutorial007.py" = ["B007"]
|
||||||
|
"docs_src/dataclasses/tutorial003.py" = ["I001"]
|
||||||
|
|
||||||
|
|
||||||
|
[tool.ruff.isort]
|
||||||
|
known-third-party = ["fastapi", "pydantic", "starlette"]
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,7 @@ def serve():
|
||||||
os.chdir("site")
|
os.chdir("site")
|
||||||
server_address = ("", 8008)
|
server_address = ("", 8008)
|
||||||
server = HTTPServer(server_address, SimpleHTTPRequestHandler)
|
server = HTTPServer(server_address, SimpleHTTPRequestHandler)
|
||||||
typer.echo(f"Serving at: http://127.0.0.1:8008")
|
typer.echo("Serving at: http://127.0.0.1:8008")
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -420,7 +420,7 @@ def get_file_to_nav_map(nav: list) -> Dict[str, Tuple[str, ...]]:
|
||||||
file_to_nav = {}
|
file_to_nav = {}
|
||||||
for item in nav:
|
for item in nav:
|
||||||
if type(item) is str:
|
if type(item) is str:
|
||||||
file_to_nav[item] = tuple()
|
file_to_nav[item] = ()
|
||||||
elif type(item) is dict:
|
elif type(item) is dict:
|
||||||
item_key = list(item.keys())[0]
|
item_key = list(item.keys())[0]
|
||||||
sub_nav = item[item_key]
|
sub_nav = item[item_key]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place docs_src fastapi tests scripts --exclude=__init__.py
|
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
|
isort fastapi tests docs_src scripts
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,6 @@ set -e
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
mypy fastapi
|
mypy fastapi
|
||||||
flake8 fastapi tests
|
ruff fastapi tests docs_src scripts
|
||||||
black fastapi tests --check
|
black fastapi tests --check
|
||||||
isort fastapi tests docs_src scripts --check-only
|
isort fastapi tests docs_src scripts --check-only
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,6 @@ def test_route_classes():
|
||||||
for r in app.router.routes:
|
for r in app.router.routes:
|
||||||
assert isinstance(r, Route)
|
assert isinstance(r, Route)
|
||||||
routes[r.path] = r
|
routes[r.path] = r
|
||||||
assert getattr(routes["/a/"], "x_type") == "A"
|
assert getattr(routes["/a/"], "x_type") == "A" # noqa: B009
|
||||||
assert getattr(routes["/a/b/"], "x_type") == "B"
|
assert getattr(routes["/a/b/"], "x_type") == "B" # noqa: B009
|
||||||
assert getattr(routes["/a/b/c/"], "x_type") == "C"
|
assert getattr(routes["/a/b/c/"], "x_type") == "C" # noqa: B009
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,7 @@ def test_router_ws_depends():
|
||||||
|
|
||||||
def test_router_ws_depends_with_override():
|
def test_router_ws_depends_with_override():
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
app.dependency_overrides[ws_dependency] = lambda: "Override"
|
app.dependency_overrides[ws_dependency] = lambda: "Override" # noqa: E731
|
||||||
with client.websocket_connect("/router-ws-depends/") as websocket:
|
with client.websocket_connect("/router-ws-depends/") as websocket:
|
||||||
assert websocket.receive_text() == "Override"
|
assert websocket.receive_text() == "Override"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue