Skip to content

Commit fa74093

Browse files
authored
✨ Use Ruff for linting (#5630)
1 parent bcd9ab9 commit fa74093

File tree

13 files changed

+51
-37
lines changed

13 files changed

+51
-37
lines changed

.flake8

Lines changed: 0 additions & 5 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,12 @@ repos:
1818
args:
1919
- --py3-plus
2020
- --keep-runtime-typing
21-
- repo: https://github.com/PyCQA/autoflake
22-
rev: v1.7.7
21+
- repo: https://github.com/charliermarsh/ruff-pre-commit
22+
rev: v0.0.114
2323
hooks:
24-
- id: autoflake
24+
- id: ruff
2525
args:
26-
- --recursive
27-
- --in-place
28-
- --remove-all-unused-imports
29-
- --remove-unused-variables
30-
- --expand-star-imports
31-
- --exclude
32-
- __init__.py
33-
- --remove-duplicate-keys
26+
- --fix
3427
- repo: https://github.com/pycqa/isort
3528
rev: 5.10.1
3629
hooks:

docs_src/security/tutorial005.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def get_current_user(
107107
if security_scopes.scopes:
108108
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
109109
else:
110-
authenticate_value = f"Bearer"
110+
authenticate_value = "Bearer"
111111
credentials_exception = HTTPException(
112112
status_code=status.HTTP_401_UNAUTHORIZED,
113113
detail="Could not validate credentials",

docs_src/security/tutorial005_py310.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def get_current_user(
106106
if security_scopes.scopes:
107107
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
108108
else:
109-
authenticate_value = f"Bearer"
109+
authenticate_value = "Bearer"
110110
credentials_exception = HTTPException(
111111
status_code=status.HTTP_401_UNAUTHORIZED,
112112
detail="Could not validate credentials",

docs_src/security/tutorial005_py39.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def get_current_user(
107107
if security_scopes.scopes:
108108
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
109109
else:
110-
authenticate_value = f"Bearer"
110+
authenticate_value = "Bearer"
111111
credentials_exception = HTTPException(
112112
status_code=status.HTTP_401_UNAUTHORIZED,
113113
detail="Could not validate credentials",

fastapi/dependencies/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,21 +426,21 @@ def is_coroutine_callable(call: Callable[..., Any]) -> bool:
426426
return inspect.iscoroutinefunction(call)
427427
if inspect.isclass(call):
428428
return False
429-
dunder_call = getattr(call, "__call__", None)
429+
dunder_call = getattr(call, "__call__", None) # noqa: B004
430430
return inspect.iscoroutinefunction(dunder_call)
431431

432432

433433
def is_async_gen_callable(call: Callable[..., Any]) -> bool:
434434
if inspect.isasyncgenfunction(call):
435435
return True
436-
dunder_call = getattr(call, "__call__", None)
436+
dunder_call = getattr(call, "__call__", None) # noqa: B004
437437
return inspect.isasyncgenfunction(dunder_call)
438438

439439

440440
def is_gen_callable(call: Callable[..., Any]) -> bool:
441441
if inspect.isgeneratorfunction(call):
442442
return True
443-
dunder_call = getattr(call, "__call__", None)
443+
dunder_call = getattr(call, "__call__", None) # noqa: B004
444444
return inspect.isgeneratorfunction(dunder_call)
445445

446446

@@ -724,14 +724,14 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
724724
# in case a sub-dependency is evaluated with a single unique body field
725725
# That is combined (embedded) with other body fields
726726
for param in flat_dependant.body_params:
727-
setattr(param.field_info, "embed", True)
727+
setattr(param.field_info, "embed", True) # noqa: B010
728728
model_name = "Body_" + name
729729
BodyModel: Type[BaseModel] = create_model(model_name)
730730
for f in flat_dependant.body_params:
731731
BodyModel.__fields__[f.name] = f
732732
required = any(True for f in flat_dependant.body_params if f.required)
733733

734-
BodyFieldInfo_kwargs: Dict[str, Any] = dict(default=None)
734+
BodyFieldInfo_kwargs: Dict[str, Any] = {"default": None}
735735
if any(isinstance(f.field_info, params.File) for f in flat_dependant.body_params):
736736
BodyFieldInfo: Type[params.Body] = params.File
737737
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]:
740740
BodyFieldInfo = params.Body
741741

742742
body_param_media_types = [
743-
getattr(f.field_info, "media_type")
743+
f.field_info.media_type
744744
for f in flat_dependant.body_params
745745
if isinstance(f.field_info, params.Body)
746746
]

fastapi/routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def include_router(
701701
), "A path prefix must not end with '/', as the routes will start with '/'"
702702
else:
703703
for r in router.routes:
704-
path = getattr(r, "path")
704+
path = getattr(r, "path") # noqa: B009
705705
name = getattr(r, "name", "unknown")
706706
if path is not None and not path:
707707
raise Exception(

pyproject.toml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test = [
5353
"pytest >=7.1.3,<8.0.0",
5454
"coverage[toml] >= 6.5.0,<7.0",
5555
"mypy ==0.982",
56-
"flake8 >=3.8.3,<6.0.0",
56+
"ruff ==0.0.114",
5757
"black == 22.8.0",
5858
"isort >=5.0.6,<6.0.0",
5959
"httpx >=0.23.0,<0.24.0",
@@ -87,8 +87,7 @@ doc = [
8787
"pyyaml >=5.3.1,<7.0.0",
8888
]
8989
dev = [
90-
"autoflake >=1.4.0,<2.0.0",
91-
"flake8 >=3.8.3,<6.0.0",
90+
"ruff ==0.0.114",
9291
"uvicorn[standard] >=0.12.0,<0.19.0",
9392
"pre-commit >=2.17.0,<3.0.0",
9493
]
@@ -156,3 +155,30 @@ source = [
156155
"fastapi"
157156
]
158157
context = '${CONTEXT}'
158+
159+
[tool.ruff]
160+
select = [
161+
"E", # pycodestyle errors
162+
"W", # pycodestyle warnings
163+
"F", # pyflakes
164+
# "I", # isort
165+
"C", # flake8-comprehensions
166+
"B", # flake8-bugbear
167+
]
168+
ignore = [
169+
"E501", # line too long, handled by black
170+
"B008", # do not perform function calls in argument defaults
171+
]
172+
173+
[tool.ruff.per-file-ignores]
174+
"__init__.py" = ["F401"]
175+
"docs_src/dependencies/tutorial007.py" = ["F821"]
176+
"docs_src/dependencies/tutorial008.py" = ["F821"]
177+
"docs_src/dependencies/tutorial009.py" = ["F821"]
178+
"docs_src/dependencies/tutorial010.py" = ["F821"]
179+
"docs_src/custom_response/tutorial007.py" = ["B007"]
180+
"docs_src/dataclasses/tutorial003.py" = ["I001"]
181+
182+
183+
[tool.ruff.isort]
184+
known-third-party = ["fastapi", "pydantic", "starlette"]

scripts/docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def serve():
332332
os.chdir("site")
333333
server_address = ("", 8008)
334334
server = HTTPServer(server_address, SimpleHTTPRequestHandler)
335-
typer.echo(f"Serving at: http://127.0.0.1:8008")
335+
typer.echo("Serving at: http://127.0.0.1:8008")
336336
server.serve_forever()
337337

338338

@@ -420,7 +420,7 @@ def get_file_to_nav_map(nav: list) -> Dict[str, Tuple[str, ...]]:
420420
file_to_nav = {}
421421
for item in nav:
422422
if type(item) is str:
423-
file_to_nav[item] = tuple()
423+
file_to_nav[item] = ()
424424
elif type(item) is dict:
425425
item_key = list(item.keys())[0]
426426
sub_nav = item[item_key]

scripts/format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh -e
22
set -x
33

4-
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place docs_src fastapi tests scripts --exclude=__init__.py
4+
ruff fastapi tests docs_src scripts --fix
55
black fastapi tests docs_src scripts
66
isort fastapi tests docs_src scripts

0 commit comments

Comments
 (0)