]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
♻️ Update internal type annotations and upgrade mypy (#9658)
authorSebastián Ramírez <tiangolo@gmail.com>
Sun, 11 Jun 2023 22:16:01 +0000 (00:16 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Jun 2023 22:16:01 +0000 (22:16 +0000)
fastapi/openapi/models.py
fastapi/security/api_key.py
fastapi/security/oauth2.py
requirements-tests.txt

index 11edfe38ade876a12259b559490fd5d086f29485..81a24f389b63be0eee554c679479e8a4f83fc5bc 100644 (file)
@@ -3,6 +3,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Union
 
 from fastapi.logger import logger
 from pydantic import AnyUrl, BaseModel, Field
+from typing_extensions import Literal
 
 try:
     import email_validator  # type: ignore
@@ -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
 
 
@@ -349,12 +350,14 @@ class OAuthFlows(BaseModel):
 
 
 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
 
 
index 61730187ad1acc4545f8e432794d761deafe25c1..8b2c5c08059fc6911ceb34efdff8f5bf80df469f 100644 (file)
@@ -21,7 +21,9 @@ class APIKeyQuery(APIKeyBase):
         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
@@ -48,7 +50,9 @@ class APIKeyHeader(APIKeyBase):
         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
@@ -75,7 +79,9 @@ class APIKeyCookie(APIKeyBase):
         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
index dc75dc9febb71cfc949b6367cf0014716bbedf96..938dec37cd677b10bdafbb87a8474711fa4e39d2 100644 (file)
@@ -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
@@ -121,7 +121,9 @@ class OAuth2(SecurityBase):
         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
 
@@ -148,7 +150,9 @@ class OAuth2PasswordBearer(OAuth2):
     ):
         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,
@@ -185,12 +189,15 @@ class OAuth2AuthorizationCodeBearer(OAuth2):
         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,
index 52a44cec5527d5624e02fbbca5e25ceec265bcef..5105071be31c27e070918112e7511e90a57b7bfb 100644 (file)
@@ -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