From 24c512f68ec4438a88955f1049f46aa4a99b5f47 Mon Sep 17 00:00:00 2001 From: Francesco Bartoli Date: Sun, 28 Jan 2024 16:36:00 +0100 Subject: [PATCH] Fix mypy errors Fix mypy errors --- app/auth/oauth2.py | 6 ++++-- app/middleware/oauth2.py | 23 ++++++++++------------- app/middleware/pygeoapi.py | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/auth/oauth2.py b/app/auth/oauth2.py index 90bce5a..b11b076 100644 --- a/app/auth/oauth2.py +++ b/app/auth/oauth2.py @@ -17,7 +17,9 @@ def __init__( ) -> None: """Set properties initialization for injectables.""" self.key = key - self.skip_endpoints = [re.compile(skip) for skip in skip_endpoints] + self.skip_endpoints = [ + re.compile(skip) for skip in skip_endpoints + ] # type:ignore @abstractmethod async def extract(self, request: Request) -> List: @@ -30,7 +32,7 @@ class Oauth2Provider: def __init__( self, - authentication: [AuthInterface, List[AuthInterface]], + authentication: [AuthInterface, List[AuthInterface]], # type:ignore injectables: Optional[List[Injectable]] = None, accepted_methods: Optional[List[str]] = [ # noqa B006 "id_token", diff --git a/app/middleware/oauth2.py b/app/middleware/oauth2.py index a4c6683..b0c8be0 100644 --- a/app/middleware/oauth2.py +++ b/app/middleware/oauth2.py @@ -4,7 +4,7 @@ from typing import List from typing import Optional -from app.auth.exceptions import Oauth2Exception +from app.auth.exceptions import Oauth2Error from app.auth.oauth2 import Oauth2Provider from app.config.app import configuration as cfg from app.config.logging import create_logger @@ -16,22 +16,16 @@ from starlette.types import Scope from starlette.types import Send -try: - Pattern = re.Pattern -except AttributeError: - # Python3.6 does not contain re.Pattern - Pattern = None - logger = create_logger("app.middleware.oauth2") -def should_skip_endpoint(endpoint: str, skip_endpoints: List[Pattern]) -> bool: +def should_skip_endpoint(endpoint: str, skip_endpoints: List[re.Pattern]) -> bool: """Evaluate whether a given endpoint should be skipped. Args: endpoint (str): Endpoint path - skip_endpoints (List[Pattern]): Pattern to skip + skip_endpoints (List[re.Pattern]): Pattern to skip Returns: bool: Result of the evaluation @@ -75,12 +69,15 @@ def __init__( """Initialize OAuth2 authentication middleware.""" self.config = config self.app = app - if cfg.FASTGEOAPI_CONTEXT and cfg.FASTGEOAPI_CONTEXT not in skip_endpoints: + if cfg.FASTGEOAPI_CONTEXT not in skip_endpoints: # type:ignore self.skip_endpoints = [ - re.compile(f"{cfg.FASTGEOAPI_CONTEXT}{skip}") for skip in skip_endpoints + re.compile(f"{cfg.FASTGEOAPI_CONTEXT}{skip}") + for skip in skip_endpoints # type:ignore ] else: - self.skip_endpoints = [re.compile(skip) for skip in skip_endpoints] + self.skip_endpoints = [ + re.compile(skip) for skip in skip_endpoints + ] # type:ignore logger.debug(f"Compiled skippable endpoints: {self.skip_endpoints}") async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: @@ -116,7 +113,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if isinstance(user_info_or_auth_redirect, dict): successful = True break - except Oauth2Exception as e: + except Oauth2Error as e: logger.error(f"Authentication Exception raised on login \n{e}") # Some authentication flows require a prior redirect to id provider diff --git a/app/middleware/pygeoapi.py b/app/middleware/pygeoapi.py index 881c37d..53d9376 100644 --- a/app/middleware/pygeoapi.py +++ b/app/middleware/pygeoapi.py @@ -95,7 +95,7 @@ async def send_with_security(self, message: Message) -> None: # noqa: C901 item in ["http", "apiKey", "oauth2", "openIdConnect"] for item in security_scheme_types ): - security_schemes = {"securitySchemes": {}} + security_schemes = {"securitySchemes": {}} # type: dict[str, dict] dumped_schemes = {} for scheme in self.security_schemes: dumped_schemes.update(