Skip to content

Commit

Permalink
♻️ Update internal type annotations and upgrade mypy (#9658)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Jun 11, 2023
1 parent 3390a82 commit 4ac55af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
13 changes: 8 additions & 5 deletions fastapi/openapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from fastapi.logger import logger
from pydantic import AnyUrl, BaseModel, Field
from typing_extensions import Literal

try:
import email_validator # type: ignore
Expand Down Expand Up @@ -298,18 +299,18 @@ class APIKeyIn(Enum):


class APIKey(SecurityBase):
type_ = Field(SecuritySchemeType.apiKey, alias="type")
type_: SecuritySchemeType = Field(default=SecuritySchemeType.apiKey, alias="type")
in_: APIKeyIn = Field(alias="in")
name: str


class HTTPBase(SecurityBase):
type_ = Field(SecuritySchemeType.http, alias="type")
type_: SecuritySchemeType = Field(default=SecuritySchemeType.http, alias="type")
scheme: str


class HTTPBearer(HTTPBase):
scheme = "bearer"
scheme: Literal["bearer"] = "bearer"
bearerFormat: Optional[str] = None


Expand Down Expand Up @@ -349,12 +350,14 @@ class Config:


class OAuth2(SecurityBase):
type_ = Field(SecuritySchemeType.oauth2, alias="type")
type_: SecuritySchemeType = Field(default=SecuritySchemeType.oauth2, alias="type")
flows: OAuthFlows


class OpenIdConnect(SecurityBase):
type_ = Field(SecuritySchemeType.openIdConnect, alias="type")
type_: SecuritySchemeType = Field(
default=SecuritySchemeType.openIdConnect, alias="type"
)
openIdConnectUrl: str


Expand Down
12 changes: 9 additions & 3 deletions fastapi/security/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def __init__(
auto_error: bool = True,
):
self.model: APIKey = APIKey(
**{"in": APIKeyIn.query}, name=name, description=description
**{"in": APIKeyIn.query}, # type: ignore[arg-type]
name=name,
description=description,
)
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error
Expand All @@ -48,7 +50,9 @@ def __init__(
auto_error: bool = True,
):
self.model: APIKey = APIKey(
**{"in": APIKeyIn.header}, name=name, description=description
**{"in": APIKeyIn.header}, # type: ignore[arg-type]
name=name,
description=description,
)
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error
Expand All @@ -75,7 +79,9 @@ def __init__(
auto_error: bool = True,
):
self.model: APIKey = APIKey(
**{"in": APIKeyIn.cookie}, name=name, description=description
**{"in": APIKeyIn.cookie}, # type: ignore[arg-type]
name=name,
description=description,
)
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error
Expand Down
25 changes: 16 additions & 9 deletions fastapi/security/oauth2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union, cast

from fastapi.exceptions import HTTPException
from fastapi.openapi.models import OAuth2 as OAuth2Model
Expand Down Expand Up @@ -121,7 +121,9 @@ def __init__(
description: Optional[str] = None,
auto_error: bool = True,
):
self.model = OAuth2Model(flows=flows, description=description)
self.model = OAuth2Model(
flows=cast(OAuthFlowsModel, flows), description=description
)
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error

Expand All @@ -148,7 +150,9 @@ def __init__(
):
if not scopes:
scopes = {}
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes})
flows = OAuthFlowsModel(
password=cast(Any, {"tokenUrl": tokenUrl, "scopes": scopes})
)
super().__init__(
flows=flows,
scheme_name=scheme_name,
Expand Down Expand Up @@ -185,12 +189,15 @@ def __init__(
if not scopes:
scopes = {}
flows = OAuthFlowsModel(
authorizationCode={
"authorizationUrl": authorizationUrl,
"tokenUrl": tokenUrl,
"refreshUrl": refreshUrl,
"scopes": scopes,
}
authorizationCode=cast(
Any,
{
"authorizationUrl": authorizationUrl,
"tokenUrl": tokenUrl,
"refreshUrl": refreshUrl,
"scopes": scopes,
},
)
)
super().__init__(
flows=flows,
Expand Down
2 changes: 1 addition & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-e .
pytest >=7.1.3,<8.0.0
coverage[toml] >= 6.5.0,< 8.0
mypy ==0.982
mypy ==1.3.0
ruff ==0.0.138
black == 23.1.0
isort >=5.0.6,<6.0.0
Expand Down

0 comments on commit 4ac55af

Please sign in to comment.