Browse Source

🎨 Auto format

pull/14407/head
pre-commit-ci-lite[bot] 5 months ago
committed by GitHub
parent
commit
0ccc4a233b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 12
      docs_src/security/tutorial005_an_py39.py
  2. 11
      docs_src/security/tutorial005_py39.py
  3. 4
      tests/test_dependency_paramless.py
  4. 6
      tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py

12
docs_src/security/tutorial005_an_py39.py

@ -1,5 +1,5 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Annotated, Union from typing import Annotated
import jwt import jwt
from fastapi import Depends, FastAPI, HTTPException, Security, status from fastapi import Depends, FastAPI, HTTPException, Security, status
@ -43,15 +43,15 @@ class Token(BaseModel):
class TokenData(BaseModel): class TokenData(BaseModel):
username: Union[str, None] = None username: str | None = None
scopes: list[str] = [] scopes: list[str] = []
class User(BaseModel): class User(BaseModel):
username: str username: str
email: Union[str, None] = None email: str | None = None
full_name: Union[str, None] = None full_name: str | None = None
disabled: Union[bool, None] = None disabled: bool | None = None
class UserInDB(User): class UserInDB(User):
@ -91,7 +91,7 @@ def authenticate_user(fake_db, username: str, password: str):
return user return user
def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None): def create_access_token(data: dict, expires_delta: timedelta | None = None):
to_encode = data.copy() to_encode = data.copy()
if expires_delta: if expires_delta:
expire = datetime.now(timezone.utc) + expires_delta expire = datetime.now(timezone.utc) + expires_delta

11
docs_src/security/tutorial005_py39.py

@ -1,5 +1,4 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Union
import jwt import jwt
from fastapi import Depends, FastAPI, HTTPException, Security, status from fastapi import Depends, FastAPI, HTTPException, Security, status
@ -43,15 +42,15 @@ class Token(BaseModel):
class TokenData(BaseModel): class TokenData(BaseModel):
username: Union[str, None] = None username: str | None = None
scopes: list[str] = [] scopes: list[str] = []
class User(BaseModel): class User(BaseModel):
username: str username: str
email: Union[str, None] = None email: str | None = None
full_name: Union[str, None] = None full_name: str | None = None
disabled: Union[bool, None] = None disabled: bool | None = None
class UserInDB(User): class UserInDB(User):
@ -91,7 +90,7 @@ def authenticate_user(fake_db, username: str, password: str):
return user return user
def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None): def create_access_token(data: dict, expires_delta: timedelta | None = None):
to_encode = data.copy() to_encode = data.copy()
if expires_delta: if expires_delta:
expire = datetime.now(timezone.utc) + expires_delta expire = datetime.now(timezone.utc) + expires_delta

4
tests/test_dependency_paramless.py

@ -1,4 +1,4 @@
from typing import Annotated, Union from typing import Annotated
from fastapi import FastAPI, HTTPException, Security from fastapi import FastAPI, HTTPException, Security
from fastapi.security import ( from fastapi.security import (
@ -13,7 +13,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def process_auth( def process_auth(
credentials: Annotated[Union[str, None], Security(oauth2_scheme)], credentials: Annotated[str | None, Security(oauth2_scheme)],
security_scopes: SecurityScopes, security_scopes: SecurityScopes,
): ):
# This is an incorrect way of using it, this is not checking if the scopes are # This is an incorrect way of using it, this is not checking if the scopes are

6
tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py

@ -1,6 +1,6 @@
# Ref: https://github.com/fastapi/fastapi/issues/14454 # Ref: https://github.com/fastapi/fastapi/issues/14454
from typing import Annotated, Optional from typing import Annotated
from fastapi import APIRouter, Depends, FastAPI, Security from fastapi import APIRouter, Depends, FastAPI, Security
from fastapi.security import OAuth2AuthorizationCodeBearer from fastapi.security import OAuth2AuthorizationCodeBearer
@ -47,13 +47,13 @@ router = APIRouter(dependencies=[Security(oauth2_scheme, oauth_scopes=["read"])]
@router.get("/items/") @router.get("/items/")
async def read_items(token: Optional[str] = Depends(oauth2_scheme)): async def read_items(token: str | None = Depends(oauth2_scheme)):
return {"token": token} return {"token": token}
@router.post("/items/") @router.post("/items/")
async def create_item( async def create_item(
token: Optional[str] = Security(oauth2_scheme, oauth_scopes=["read", "write"]), token: str | None = Security(oauth2_scheme, oauth_scopes=["read", "write"]),
): ):
return {"token": token} return {"token": token}

Loading…
Cancel
Save