Browse Source

⬆️ Upgrade configuration for Ruff v0.2.0 (#11075)

pull/11352/head
Charlie Marsh 1 year ago
committed by GitHub
parent
commit
5ccc869fee
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      .pre-commit-config.yaml
  2. 2
      docs_src/dependencies/tutorial005_an.py
  3. 2
      docs_src/dependencies/tutorial005_an_py310.py
  4. 2
      docs_src/dependencies/tutorial005_an_py39.py
  5. 2
      docs_src/header_params/tutorial002.py
  6. 2
      docs_src/header_params/tutorial002_an_py310.py
  7. 2
      docs_src/header_params/tutorial002_py310.py
  8. 2
      docs_src/query_params_str_validations/tutorial003.py
  9. 2
      docs_src/query_params_str_validations/tutorial003_an.py
  10. 2
      docs_src/query_params_str_validations/tutorial003_an_py310.py
  11. 2
      docs_src/query_params_str_validations/tutorial003_an_py39.py
  12. 2
      docs_src/query_params_str_validations/tutorial007.py
  13. 2
      docs_src/query_params_str_validations/tutorial007_an.py
  14. 2
      docs_src/query_params_str_validations/tutorial007_an_py310.py
  15. 2
      docs_src/query_params_str_validations/tutorial007_an_py39.py
  16. 2
      docs_src/query_params_str_validations/tutorial007_py310.py
  17. 2
      docs_src/query_params_str_validations/tutorial014.py
  18. 2
      docs_src/query_params_str_validations/tutorial014_an.py
  19. 2
      docs_src/query_params_str_validations/tutorial014_an_py310.py
  20. 2
      docs_src/query_params_str_validations/tutorial014_an_py39.py
  21. 2
      docs_src/query_params_str_validations/tutorial014_py310.py
  22. 4
      docs_src/security/tutorial003_an.py
  23. 4
      docs_src/security/tutorial003_an_py310.py
  24. 4
      docs_src/security/tutorial003_an_py39.py
  25. 2
      docs_src/security/tutorial004.py
  26. 8
      docs_src/security/tutorial004_an.py
  27. 8
      docs_src/security/tutorial004_an_py310.py
  28. 8
      docs_src/security/tutorial004_an_py39.py
  29. 2
      docs_src/security/tutorial004_py310.py
  30. 6
      docs_src/security/tutorial005.py
  31. 8
      docs_src/security/tutorial005_an.py
  32. 8
      docs_src/security/tutorial005_an_py310.py
  33. 8
      docs_src/security/tutorial005_an_py39.py
  34. 6
      docs_src/security/tutorial005_py310.py
  35. 6
      docs_src/security/tutorial005_py39.py
  36. 2
      docs_src/security/tutorial007_an.py
  37. 2
      docs_src/security/tutorial007_an_py39.py
  38. 2
      fastapi/dependencies/utils.py
  39. 2
      fastapi/encoders.py
  40. 8
      pyproject.toml
  41. 2
      requirements-tests.txt
  42. 6
      tests/test_param_include_in_schema.py
  43. 2
      tests/test_regex_deprecated_body.py
  44. 2
      tests/test_regex_deprecated_params.py

2
.pre-commit-config.yaml

@ -14,7 +14,7 @@ repos:
- id: end-of-file-fixer - id: end-of-file-fixer
- id: trailing-whitespace - id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit - repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.2 rev: v0.2.0
hooks: hooks:
- id: ruff - id: ruff
args: args:

2
docs_src/dependencies/tutorial005_an.py

@ -21,6 +21,6 @@ def query_or_cookie_extractor(
@app.get("/items/") @app.get("/items/")
async def read_query( async def read_query(
query_or_default: Annotated[str, Depends(query_or_cookie_extractor)] query_or_default: Annotated[str, Depends(query_or_cookie_extractor)],
): ):
return {"q_or_cookie": query_or_default} return {"q_or_cookie": query_or_default}

2
docs_src/dependencies/tutorial005_an_py310.py

@ -20,6 +20,6 @@ def query_or_cookie_extractor(
@app.get("/items/") @app.get("/items/")
async def read_query( async def read_query(
query_or_default: Annotated[str, Depends(query_or_cookie_extractor)] query_or_default: Annotated[str, Depends(query_or_cookie_extractor)],
): ):
return {"q_or_cookie": query_or_default} return {"q_or_cookie": query_or_default}

2
docs_src/dependencies/tutorial005_an_py39.py

@ -20,6 +20,6 @@ def query_or_cookie_extractor(
@app.get("/items/") @app.get("/items/")
async def read_query( async def read_query(
query_or_default: Annotated[str, Depends(query_or_cookie_extractor)] query_or_default: Annotated[str, Depends(query_or_cookie_extractor)],
): ):
return {"q_or_cookie": query_or_default} return {"q_or_cookie": query_or_default}

2
docs_src/header_params/tutorial002.py

@ -7,6 +7,6 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
strange_header: Union[str, None] = Header(default=None, convert_underscores=False) strange_header: Union[str, None] = Header(default=None, convert_underscores=False),
): ):
return {"strange_header": strange_header} return {"strange_header": strange_header}

2
docs_src/header_params/tutorial002_an_py310.py

@ -7,6 +7,6 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
strange_header: Annotated[str | None, Header(convert_underscores=False)] = None strange_header: Annotated[str | None, Header(convert_underscores=False)] = None,
): ):
return {"strange_header": strange_header} return {"strange_header": strange_header}

2
docs_src/header_params/tutorial002_py310.py

@ -5,6 +5,6 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
strange_header: str | None = Header(default=None, convert_underscores=False) strange_header: str | None = Header(default=None, convert_underscores=False),
): ):
return {"strange_header": strange_header} return {"strange_header": strange_header}

2
docs_src/query_params_str_validations/tutorial003.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Union[str, None] = Query(default=None, min_length=3, max_length=50) q: Union[str, None] = Query(default=None, min_length=3, max_length=50),
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial003_an.py

@ -8,7 +8,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[Union[str, None], Query(min_length=3, max_length=50)] = None q: Annotated[Union[str, None], Query(min_length=3, max_length=50)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial003_an_py310.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[str | None, Query(min_length=3, max_length=50)] = None q: Annotated[str | None, Query(min_length=3, max_length=50)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial003_an_py39.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[Union[str, None], Query(min_length=3, max_length=50)] = None q: Annotated[Union[str, None], Query(min_length=3, max_length=50)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial007.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Union[str, None] = Query(default=None, title="Query string", min_length=3) q: Union[str, None] = Query(default=None, title="Query string", min_length=3),
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial007_an.py

@ -8,7 +8,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[Union[str, None], Query(title="Query string", min_length=3)] = None q: Annotated[Union[str, None], Query(title="Query string", min_length=3)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial007_an_py310.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[str | None, Query(title="Query string", min_length=3)] = None q: Annotated[str | None, Query(title="Query string", min_length=3)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial007_an_py39.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[Union[str, None], Query(title="Query string", min_length=3)] = None q: Annotated[Union[str, None], Query(title="Query string", min_length=3)] = None,
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial007_py310.py

@ -5,7 +5,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: str | None = Query(default=None, title="Query string", min_length=3) q: str | None = Query(default=None, title="Query string", min_length=3),
): ):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q: if q:

2
docs_src/query_params_str_validations/tutorial014.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
hidden_query: Union[str, None] = Query(default=None, include_in_schema=False) hidden_query: Union[str, None] = Query(default=None, include_in_schema=False),
): ):
if hidden_query: if hidden_query:
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

2
docs_src/query_params_str_validations/tutorial014_an.py

@ -8,7 +8,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
hidden_query: Annotated[Union[str, None], Query(include_in_schema=False)] = None hidden_query: Annotated[Union[str, None], Query(include_in_schema=False)] = None,
): ):
if hidden_query: if hidden_query:
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

2
docs_src/query_params_str_validations/tutorial014_an_py310.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
hidden_query: Annotated[str | None, Query(include_in_schema=False)] = None hidden_query: Annotated[str | None, Query(include_in_schema=False)] = None,
): ):
if hidden_query: if hidden_query:
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

2
docs_src/query_params_str_validations/tutorial014_an_py39.py

@ -7,7 +7,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
hidden_query: Annotated[Union[str, None], Query(include_in_schema=False)] = None hidden_query: Annotated[Union[str, None], Query(include_in_schema=False)] = None,
): ):
if hidden_query: if hidden_query:
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

2
docs_src/query_params_str_validations/tutorial014_py310.py

@ -5,7 +5,7 @@ app = FastAPI()
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
hidden_query: str | None = Query(default=None, include_in_schema=False) hidden_query: str | None = Query(default=None, include_in_schema=False),
): ):
if hidden_query: if hidden_query:
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

4
docs_src/security/tutorial003_an.py

@ -68,7 +68,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -90,6 +90,6 @@ async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
@app.get("/users/me") @app.get("/users/me")
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user

4
docs_src/security/tutorial003_an_py310.py

@ -67,7 +67,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -89,6 +89,6 @@ async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
@app.get("/users/me") @app.get("/users/me")
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user

4
docs_src/security/tutorial003_an_py39.py

@ -67,7 +67,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -89,6 +89,6 @@ async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
@app.get("/users/me") @app.get("/users/me")
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user

2
docs_src/security/tutorial004.py

@ -114,7 +114,7 @@ async def get_current_active_user(current_user: User = Depends(get_current_user)
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends() form_data: OAuth2PasswordRequestForm = Depends(),
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:

8
docs_src/security/tutorial004_an.py

@ -108,7 +108,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -117,7 +117,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -135,13 +135,13 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

8
docs_src/security/tutorial004_an_py310.py

@ -107,7 +107,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -116,7 +116,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -134,13 +134,13 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

8
docs_src/security/tutorial004_an_py39.py

@ -107,7 +107,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Depends(get_current_user)] current_user: Annotated[User, Depends(get_current_user)],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -116,7 +116,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -134,13 +134,13 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

2
docs_src/security/tutorial004_py310.py

@ -113,7 +113,7 @@ async def get_current_active_user(current_user: User = Depends(get_current_user)
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends() form_data: OAuth2PasswordRequestForm = Depends(),
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:

6
docs_src/security/tutorial005.py

@ -136,7 +136,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: User = Security(get_current_user, scopes=["me"]) current_user: User = Security(get_current_user, scopes=["me"]),
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -145,7 +145,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends() form_data: OAuth2PasswordRequestForm = Depends(),
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -165,7 +165,7 @@ async def read_users_me(current_user: User = Depends(get_current_active_user)):
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: User = Security(get_current_active_user, scopes=["items"]) current_user: User = Security(get_current_active_user, scopes=["items"]),
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

8
docs_src/security/tutorial005_an.py

@ -137,7 +137,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Security(get_current_user, scopes=["me"])] current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -146,7 +146,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -161,14 +161,14 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])] current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

8
docs_src/security/tutorial005_an_py310.py

@ -136,7 +136,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Security(get_current_user, scopes=["me"])] current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -145,7 +145,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -160,14 +160,14 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])] current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

8
docs_src/security/tutorial005_an_py39.py

@ -136,7 +136,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: Annotated[User, Security(get_current_user, scopes=["me"])] current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -145,7 +145,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -160,14 +160,14 @@ async def login_for_access_token(
@app.get("/users/me/", response_model=User) @app.get("/users/me/", response_model=User)
async def read_users_me( async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)] current_user: Annotated[User, Depends(get_current_active_user)],
): ):
return current_user return current_user
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])] current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])],
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

6
docs_src/security/tutorial005_py310.py

@ -135,7 +135,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: User = Security(get_current_user, scopes=["me"]) current_user: User = Security(get_current_user, scopes=["me"]),
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -144,7 +144,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends() form_data: OAuth2PasswordRequestForm = Depends(),
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -164,7 +164,7 @@ async def read_users_me(current_user: User = Depends(get_current_active_user)):
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: User = Security(get_current_active_user, scopes=["items"]) current_user: User = Security(get_current_active_user, scopes=["items"]),
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

6
docs_src/security/tutorial005_py39.py

@ -136,7 +136,7 @@ async def get_current_user(
async def get_current_active_user( async def get_current_active_user(
current_user: User = Security(get_current_user, scopes=["me"]) current_user: User = Security(get_current_user, scopes=["me"]),
): ):
if current_user.disabled: if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user") raise HTTPException(status_code=400, detail="Inactive user")
@ -145,7 +145,7 @@ async def get_current_active_user(
@app.post("/token") @app.post("/token")
async def login_for_access_token( async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends() form_data: OAuth2PasswordRequestForm = Depends(),
) -> Token: ) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password) user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user: if not user:
@ -165,7 +165,7 @@ async def read_users_me(current_user: User = Depends(get_current_active_user)):
@app.get("/users/me/items/") @app.get("/users/me/items/")
async def read_own_items( async def read_own_items(
current_user: User = Security(get_current_active_user, scopes=["items"]) current_user: User = Security(get_current_active_user, scopes=["items"]),
): ):
return [{"item_id": "Foo", "owner": current_user.username}] return [{"item_id": "Foo", "owner": current_user.username}]

2
docs_src/security/tutorial007_an.py

@ -10,7 +10,7 @@ security = HTTPBasic()
def get_current_username( def get_current_username(
credentials: Annotated[HTTPBasicCredentials, Depends(security)] credentials: Annotated[HTTPBasicCredentials, Depends(security)],
): ):
current_username_bytes = credentials.username.encode("utf8") current_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = b"stanleyjobson" correct_username_bytes = b"stanleyjobson"

2
docs_src/security/tutorial007_an_py39.py

@ -10,7 +10,7 @@ security = HTTPBasic()
def get_current_username( def get_current_username(
credentials: Annotated[HTTPBasicCredentials, Depends(security)] credentials: Annotated[HTTPBasicCredentials, Depends(security)],
): ):
current_username_bytes = credentials.username.encode("utf8") current_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = b"stanleyjobson" correct_username_bytes = b"stanleyjobson"

2
fastapi/dependencies/utils.py

@ -743,7 +743,7 @@ async def request_body_to_args(
results: List[Union[bytes, str]] = [] results: List[Union[bytes, str]] = []
async def process_fn( async def process_fn(
fn: Callable[[], Coroutine[Any, Any, Any]] fn: Callable[[], Coroutine[Any, Any, Any]],
) -> None: ) -> None:
result = await fn() result = await fn()
results.append(result) # noqa: B023 results.append(result) # noqa: B023

2
fastapi/encoders.py

@ -86,7 +86,7 @@ ENCODERS_BY_TYPE: Dict[Type[Any], Callable[[Any], Any]] = {
def generate_encoders_by_class_tuples( def generate_encoders_by_class_tuples(
type_encoder_map: Dict[Any, Callable[[Any], Any]] type_encoder_map: Dict[Any, Callable[[Any], Any]],
) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]: ) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]:
encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict( encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(
tuple tuple

8
pyproject.toml

@ -139,7 +139,7 @@ omit = [
"docs_src/response_model/tutorial003_04_py310.py", "docs_src/response_model/tutorial003_04_py310.py",
] ]
[tool.ruff] [tool.ruff.lint]
select = [ select = [
"E", # pycodestyle errors "E", # pycodestyle errors
"W", # pycodestyle warnings "W", # pycodestyle warnings
@ -155,7 +155,7 @@ ignore = [
"W191", # indentation contains tabs "W191", # indentation contains tabs
] ]
[tool.ruff.per-file-ignores] [tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"] "__init__.py" = ["F401"]
"docs_src/dependencies/tutorial007.py" = ["F821"] "docs_src/dependencies/tutorial007.py" = ["F821"]
"docs_src/dependencies/tutorial008.py" = ["F821"] "docs_src/dependencies/tutorial008.py" = ["F821"]
@ -188,9 +188,9 @@ ignore = [
"docs_src/dependencies/tutorial008b_an_py39.py" = ["B904"] "docs_src/dependencies/tutorial008b_an_py39.py" = ["B904"]
[tool.ruff.isort] [tool.ruff.lint.isort]
known-third-party = ["fastapi", "pydantic", "starlette"] known-third-party = ["fastapi", "pydantic", "starlette"]
[tool.ruff.pyupgrade] [tool.ruff.lint.pyupgrade]
# Preserve types, even if a file imports `from __future__ import annotations`. # Preserve types, even if a file imports `from __future__ import annotations`.
keep-runtime-typing = true keep-runtime-typing = true

2
requirements-tests.txt

@ -4,7 +4,7 @@ pydantic-settings >=2.0.0
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.4.1 mypy ==1.4.1
ruff ==0.1.2 ruff ==0.2.0
email_validator >=1.1.1,<3.0.0 email_validator >=1.1.1,<3.0.0
dirty-equals ==0.6.0 dirty-equals ==0.6.0
# TODO: once removing databases from tutorial, upgrade SQLAlchemy # TODO: once removing databases from tutorial, upgrade SQLAlchemy

6
tests/test_param_include_in_schema.py

@ -9,14 +9,14 @@ app = FastAPI()
@app.get("/hidden_cookie") @app.get("/hidden_cookie")
async def hidden_cookie( async def hidden_cookie(
hidden_cookie: Optional[str] = Cookie(default=None, include_in_schema=False) hidden_cookie: Optional[str] = Cookie(default=None, include_in_schema=False),
): ):
return {"hidden_cookie": hidden_cookie} return {"hidden_cookie": hidden_cookie}
@app.get("/hidden_header") @app.get("/hidden_header")
async def hidden_header( async def hidden_header(
hidden_header: Optional[str] = Header(default=None, include_in_schema=False) hidden_header: Optional[str] = Header(default=None, include_in_schema=False),
): ):
return {"hidden_header": hidden_header} return {"hidden_header": hidden_header}
@ -28,7 +28,7 @@ async def hidden_path(hidden_path: str = Path(include_in_schema=False)):
@app.get("/hidden_query") @app.get("/hidden_query")
async def hidden_query( async def hidden_query(
hidden_query: Optional[str] = Query(default=None, include_in_schema=False) hidden_query: Optional[str] = Query(default=None, include_in_schema=False),
): ):
return {"hidden_query": hidden_query} return {"hidden_query": hidden_query}

2
tests/test_regex_deprecated_body.py

@ -14,7 +14,7 @@ def get_client():
@app.post("/items/") @app.post("/items/")
async def read_items( async def read_items(
q: Annotated[str | None, Form(regex="^fixedquery$")] = None q: Annotated[str | None, Form(regex="^fixedquery$")] = None,
): ):
if q: if q:
return f"Hello {q}" return f"Hello {q}"

2
tests/test_regex_deprecated_params.py

@ -14,7 +14,7 @@ def get_client():
@app.get("/items/") @app.get("/items/")
async def read_items( async def read_items(
q: Annotated[str | None, Query(regex="^fixedquery$")] = None q: Annotated[str | None, Query(regex="^fixedquery$")] = None,
): ):
if q: if q:
return f"Hello {q}" return f"Hello {q}"

Loading…
Cancel
Save