From 0ccc4a233bba52e00e0cf7b45890daf87c254887 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 12 Feb 2026 08:31:42 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/security/tutorial005_an_py39.py | 12 ++++++------ docs_src/security/tutorial005_py39.py | 11 +++++------ tests/test_dependency_paramless.py | 4 ++-- ...auth2_authorization_code_bearer_scopes_openapi.py | 6 +++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/docs_src/security/tutorial005_an_py39.py b/docs_src/security/tutorial005_an_py39.py index c8d8a757e..8c12c215f 100644 --- a/docs_src/security/tutorial005_an_py39.py +++ b/docs_src/security/tutorial005_an_py39.py @@ -1,5 +1,5 @@ from datetime import datetime, timedelta, timezone -from typing import Annotated, Union +from typing import Annotated import jwt from fastapi import Depends, FastAPI, HTTPException, Security, status @@ -43,15 +43,15 @@ class Token(BaseModel): class TokenData(BaseModel): - username: Union[str, None] = None + username: str | None = None scopes: list[str] = [] class User(BaseModel): username: str - email: Union[str, None] = None - full_name: Union[str, None] = None - disabled: Union[bool, None] = None + email: str | None = None + full_name: str | None = None + disabled: bool | None = None class UserInDB(User): @@ -91,7 +91,7 @@ def authenticate_user(fake_db, username: str, password: str): 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() if expires_delta: expire = datetime.now(timezone.utc) + expires_delta diff --git a/docs_src/security/tutorial005_py39.py b/docs_src/security/tutorial005_py39.py index 839e77fb3..d25ab869d 100644 --- a/docs_src/security/tutorial005_py39.py +++ b/docs_src/security/tutorial005_py39.py @@ -1,5 +1,4 @@ from datetime import datetime, timedelta, timezone -from typing import Union import jwt from fastapi import Depends, FastAPI, HTTPException, Security, status @@ -43,15 +42,15 @@ class Token(BaseModel): class TokenData(BaseModel): - username: Union[str, None] = None + username: str | None = None scopes: list[str] = [] class User(BaseModel): username: str - email: Union[str, None] = None - full_name: Union[str, None] = None - disabled: Union[bool, None] = None + email: str | None = None + full_name: str | None = None + disabled: bool | None = None class UserInDB(User): @@ -91,7 +90,7 @@ def authenticate_user(fake_db, username: str, password: str): 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() if expires_delta: expire = datetime.now(timezone.utc) + expires_delta diff --git a/tests/test_dependency_paramless.py b/tests/test_dependency_paramless.py index e53c6006e..26ca7dead 100644 --- a/tests/test_dependency_paramless.py +++ b/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.security import ( @@ -13,7 +13,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") def process_auth( - credentials: Annotated[Union[str, None], Security(oauth2_scheme)], + credentials: Annotated[str | None, Security(oauth2_scheme)], security_scopes: SecurityScopes, ): # This is an incorrect way of using it, this is not checking if the scopes are diff --git a/tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py b/tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py index 97d79462a..7dd3421c1 100644 --- a/tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py +++ b/tests/test_security_oauth2_authorization_code_bearer_scopes_openapi.py @@ -1,6 +1,6 @@ # 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.security import OAuth2AuthorizationCodeBearer @@ -47,13 +47,13 @@ router = APIRouter(dependencies=[Security(oauth2_scheme, oauth_scopes=["read"])] @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} @router.post("/items/") 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}