From dfbb898c4ae4f6a8da6f307799fdac01fdd97c87 Mon Sep 17 00:00:00 2001 From: druizz90 <32929872+druizz90@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:28:12 +0200 Subject: [PATCH 1/5] Update flake8 pre-commit hook --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ba54705e3..542d5d480 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,8 +2,8 @@ ci: autoupdate_branch: "main" autoupdate_schedule: monthly repos: - - repo: https://github.com/PyCQA/flake8 - rev: 4.0.1 + - repo: https://github.com/pycqa/flake8 + rev: 7.1.1 hooks: - id: flake8 files: "^connexion/" From 0037147d0e5754f031c1bdf817c5c00064924ee5 Mon Sep 17 00:00:00 2001 From: druizz90 <32929872+druizz90@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:32:53 +0200 Subject: [PATCH 2/5] Improve type hints for nullable strings --- connexion/apps/abstract.py | 8 ++++++-- connexion/apps/asynchronous.py | 10 +++++++--- connexion/apps/flask.py | 26 +++++++++++++++++++++----- connexion/exceptions.py | 8 +++++++- connexion/frameworks/abstract.py | 2 +- connexion/frameworks/flask.py | 2 +- connexion/frameworks/starlette.py | 2 +- connexion/middleware/abstract.py | 2 +- connexion/middleware/main.py | 2 +- connexion/middleware/routing.py | 6 +++++- connexion/operations/abstract.py | 5 +++-- connexion/operations/openapi.py | 5 +++-- connexion/operations/swagger2.py | 6 +++--- 13 files changed, 60 insertions(+), 24 deletions(-) diff --git a/connexion/apps/abstract.py b/connexion/apps/abstract.py index 8929e02a9..5b299a786 100644 --- a/connexion/apps/abstract.py +++ b/connexion/apps/abstract.py @@ -197,7 +197,11 @@ def add_api( ) def add_url_rule( - self, rule, endpoint: str = None, view_func: t.Callable = None, **options + self, + rule, + endpoint: t.Optional[str] = None, + view_func: t.Callable = None, + **options, ): """ Connects a URL rule. Works exactly like the `route` decorator. @@ -271,7 +275,7 @@ def test_client(self, **kwargs): passed to the ``StarletteClient``.""" return TestClient(self, **kwargs) - def run(self, import_string: str = None, **kwargs): + def run(self, import_string: t.Optional[str] = None, **kwargs): """Run the application using uvicorn. :param import_string: application as import string (eg. "main:app"). This is needed to run diff --git a/connexion/apps/asynchronous.py b/connexion/apps/asynchronous.py index 9ab08a401..a0ac548f3 100644 --- a/connexion/apps/asynchronous.py +++ b/connexion/apps/asynchronous.py @@ -100,7 +100,7 @@ def __init__(self) -> None: self.router = Router() super().__init__(self.router) - def add_api(self, *args, name: str = None, **kwargs): + def add_api(self, *args, name: t.Optional[str] = None, **kwargs): api = super().add_api(*args, **kwargs) if name is not None: @@ -112,7 +112,7 @@ def add_api(self, *args, name: str = None, **kwargs): def add_url_rule( self, rule, - endpoint: str = None, + endpoint: t.Optional[str] = None, view_func: t.Callable = None, methods: t.List[str] = None, **options, @@ -200,7 +200,11 @@ def __init__( ) def add_url_rule( - self, rule, endpoint: str = None, view_func: t.Callable = None, **options + self, + rule, + endpoint: t.Optional[str] = None, + view_func: t.Callable = None, + **options, ): self._middleware_app.add_url_rule( rule, endpoint=endpoint, view_func=view_func, **options diff --git a/connexion/apps/flask.py b/connexion/apps/flask.py index 057ce8baa..4c7c14ec7 100644 --- a/connexion/apps/flask.py +++ b/connexion/apps/flask.py @@ -108,12 +108,20 @@ def _framework_path_and_name( return flask_path, endpoint_name def _add_operation_internal( - self, method: str, path: str, operation: t.Callable, name: str = None + self, + method: str, + path: str, + operation: t.Callable, + name: t.Optional[str] = None, ) -> None: self.blueprint.add_url_rule(path, name, operation, methods=[method]) def add_url_rule( - self, rule, endpoint: str = None, view_func: t.Callable = None, **options + self, + rule, + endpoint: t.Optional[str] = None, + view_func: t.Callable = None, + **options, ): return self.blueprint.add_url_rule(rule, endpoint, view_func, **options) @@ -132,7 +140,7 @@ def __init__(self, import_name, server_args: dict, **kwargs): self.asgi_app = WSGIMiddleware(self.app.wsgi_app) - def add_api(self, specification, *, name: str = None, **kwargs): + def add_api(self, specification, *, name: t.Optional[str] = None, **kwargs): api = FlaskApi(specification, **kwargs) if name is not None: @@ -143,7 +151,11 @@ def add_api(self, specification, *, name: str = None, **kwargs): return api def add_url_rule( - self, rule, endpoint: str = None, view_func: t.Callable = None, **options + self, + rule, + endpoint: t.Optional[str] = None, + view_func: t.Callable = None, + **options, ): return self.app.add_url_rule(rule, endpoint, view_func, **options) @@ -245,7 +257,11 @@ def _http_exception(self, exc: werkzeug.exceptions.HTTPException): raise starlette.exceptions.HTTPException(exc.code, detail=exc.description) def add_url_rule( - self, rule, endpoint: str = None, view_func: t.Callable = None, **options + self, + rule, + endpoint: t.Optional[str] = None, + view_func: t.Callable = None, + **options, ): self._middleware_app.add_url_rule( rule, endpoint=endpoint, view_func=view_func, **options diff --git a/connexion/exceptions.py b/connexion/exceptions.py index 42fde153f..f84f17ee7 100644 --- a/connexion/exceptions.py +++ b/connexion/exceptions.py @@ -75,7 +75,13 @@ class ClientProblem(ProblemException): """Base exception for any 4XX error. Returns 400 by default, however :class:`BadRequestProblem` should be preferred for 400 errors.""" - def __init__(self, status: int = 400, title: str = None, *, detail: str = None): + def __init__( + self, + status: int = 400, + title: t.Optional[str] = None, + *, + detail: t.Optional[str] = None, + ): super().__init__(status=status, title=title, detail=detail) diff --git a/connexion/frameworks/abstract.py b/connexion/frameworks/abstract.py index 88f90158f..a7f24ac0b 100644 --- a/connexion/frameworks/abstract.py +++ b/connexion/frameworks/abstract.py @@ -23,7 +23,7 @@ def build_response( cls, data: t.Any, *, - content_type: str = None, + content_type: t.Optional[str] = None, headers: dict = None, status_code: int = None ): diff --git a/connexion/frameworks/flask.py b/connexion/frameworks/flask.py index 958759589..303647f67 100644 --- a/connexion/frameworks/flask.py +++ b/connexion/frameworks/flask.py @@ -37,7 +37,7 @@ def build_response( cls, data: t.Any, *, - content_type: str = None, + content_type: t.Optional[str] = None, headers: dict = None, status_code: int = None ): diff --git a/connexion/frameworks/starlette.py b/connexion/frameworks/starlette.py index 097c10e54..c1665294b 100644 --- a/connexion/frameworks/starlette.py +++ b/connexion/frameworks/starlette.py @@ -31,7 +31,7 @@ def build_response( cls, data: t.Any, *, - content_type: str = None, + content_type: t.Optional[str] = None, headers: dict = None, status_code: int = None, ): diff --git a/connexion/middleware/abstract.py b/connexion/middleware/abstract.py index a5fd58aa1..64b6e7c34 100644 --- a/connexion/middleware/abstract.py +++ b/connexion/middleware/abstract.py @@ -141,7 +141,7 @@ def _framework_path_and_name( @abc.abstractmethod def _add_operation_internal( - self, method: str, path: str, operation: OP, name: str = None + self, method: str, path: str, operation: OP, name: t.Optional[str] = None ) -> None: """ Adds the operation according to the user framework in use. diff --git a/connexion/middleware/main.py b/connexion/middleware/main.py index ea1db66fa..7e09ac963 100644 --- a/connexion/middleware/main.py +++ b/connexion/middleware/main.py @@ -464,7 +464,7 @@ def add_error_handler( error_handler = (code_or_exception, function) self.error_handlers.append(error_handler) - def run(self, import_string: str = None, **kwargs): + def run(self, import_string: t.Optional[str] = None, **kwargs): """Run the application using uvicorn. :param import_string: application as import string (eg. "main:app"). This is needed to run diff --git a/connexion/middleware/routing.py b/connexion/middleware/routing.py index cac632d40..8323234f5 100644 --- a/connexion/middleware/routing.py +++ b/connexion/middleware/routing.py @@ -87,7 +87,11 @@ def _framework_path_and_name( return starlette_path, starlette_path def _add_operation_internal( - self, method: str, path: str, operation: RoutingOperation, name: str = None + self, + method: str, + path: str, + operation: RoutingOperation, + name: t.Optional[str] = None, ) -> None: self.router.add_route(path, operation, methods=[method]) diff --git a/connexion/operations/abstract.py b/connexion/operations/abstract.py index ddd5196d4..0e1fa2efc 100644 --- a/connexion/operations/abstract.py +++ b/connexion/operations/abstract.py @@ -5,6 +5,7 @@ import abc import logging +import typing as t from connexion.utils import all_json @@ -168,13 +169,13 @@ def body_name(self, content_type: str) -> str: """ @abc.abstractmethod - def body_schema(self, content_type: str = None) -> dict: + def body_schema(self, content_type: t.Optional[str] = None) -> dict: """ The body schema definition for this operation. """ @abc.abstractmethod - def body_definition(self, content_type: str = None) -> dict: + def body_definition(self, content_type: t.Optional[str] = None) -> dict: """ The body definition for this operation. :rtype: dict diff --git a/connexion/operations/openapi.py b/connexion/operations/openapi.py index a732047c2..88af973c9 100644 --- a/connexion/operations/openapi.py +++ b/connexion/operations/openapi.py @@ -3,6 +3,7 @@ """ import logging +import typing as t from connexion.datastructures import MediaTypeDict from connexion.operations.abstract import AbstractOperation @@ -212,13 +213,13 @@ def get_path_parameter_types(self): def body_name(self, _content_type: str) -> str: return self.request_body.get("x-body-name", "body") - def body_schema(self, content_type: str = None) -> dict: + def body_schema(self, content_type: t.Optional[str] = None) -> dict: """ The body schema definition for this operation. """ return self.body_definition(content_type).get("schema", {}) - def body_definition(self, content_type: str = None) -> dict: + def body_definition(self, content_type: t.Optional[str] = None) -> dict: """ The body complete definition for this operation. diff --git a/connexion/operations/swagger2.py b/connexion/operations/swagger2.py index d9e30f012..680e29370 100644 --- a/connexion/operations/swagger2.py +++ b/connexion/operations/swagger2.py @@ -215,17 +215,17 @@ def example_response(self, status_code=None, *args, **kwargs): return (build_example_from_schema(schema), status_code) - def body_name(self, content_type: str = None) -> str: + def body_name(self, content_type: t.Optional[str] = None) -> str: return self.body_definition(content_type).get("name", "body") - def body_schema(self, content_type: str = None) -> dict: + def body_schema(self, content_type: t.Optional[str] = None) -> dict: """ The body schema definition for this operation. """ body_definition = self.body_definition(content_type) return self.with_definitions(body_definition).get("schema", {}) - def body_definition(self, content_type: str = None) -> dict: + def body_definition(self, content_type: t.Optional[str] = None) -> dict: """ The body complete definition for this operation. From 4e110967d9e59defee2fa4180bf7bb6d39bbabae Mon Sep 17 00:00:00 2001 From: druizz90 <32929872+druizz90@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:38:33 +0200 Subject: [PATCH 3/5] Improve type hints for nullable dicts --- connexion/frameworks/abstract.py | 2 +- connexion/frameworks/flask.py | 2 +- connexion/frameworks/starlette.py | 2 +- connexion/middleware/security.py | 6 +++++- connexion/testing.py | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/connexion/frameworks/abstract.py b/connexion/frameworks/abstract.py index a7f24ac0b..56c5aec0d 100644 --- a/connexion/frameworks/abstract.py +++ b/connexion/frameworks/abstract.py @@ -24,7 +24,7 @@ def build_response( data: t.Any, *, content_type: t.Optional[str] = None, - headers: dict = None, + headers: t.Optional[dict] = None, status_code: int = None ): raise NotImplementedError diff --git a/connexion/frameworks/flask.py b/connexion/frameworks/flask.py index 303647f67..66b561300 100644 --- a/connexion/frameworks/flask.py +++ b/connexion/frameworks/flask.py @@ -38,7 +38,7 @@ def build_response( data: t.Any, *, content_type: t.Optional[str] = None, - headers: dict = None, + headers: t.Optional[dict] = None, status_code: int = None ): if cls.is_framework_response(data): diff --git a/connexion/frameworks/starlette.py b/connexion/frameworks/starlette.py index c1665294b..a6e098454 100644 --- a/connexion/frameworks/starlette.py +++ b/connexion/frameworks/starlette.py @@ -32,7 +32,7 @@ def build_response( data: t.Any, *, content_type: t.Optional[str] = None, - headers: dict = None, + headers: t.Optional[dict] = None, status_code: int = None, ): if isinstance(data, dict) or isinstance(data, list): diff --git a/connexion/middleware/security.py b/connexion/middleware/security.py index f6d150fcf..e283cc5b3 100644 --- a/connexion/middleware/security.py +++ b/connexion/middleware/security.py @@ -113,7 +113,11 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: class SecurityAPI(RoutedAPI[SecurityOperation]): def __init__( - self, *args, auth_all_paths: bool = False, security_map: dict = None, **kwargs + self, + *args, + auth_all_paths: bool = False, + security_map: t.Optional[dict] = None, + **kwargs, ): super().__init__(*args, **kwargs) diff --git a/connexion/testing.py b/connexion/testing.py index 20a4335bf..fb36c1c60 100644 --- a/connexion/testing.py +++ b/connexion/testing.py @@ -14,7 +14,7 @@ class TestContext: def __init__( self, *, - context: dict = None, + context: t.Optional[dict] = None, operation: AbstractOperation = None, receive: Receive = None, scope: Scope = None, From 1851f080e14fcb4d7421c454df78123d45608afd Mon Sep 17 00:00:00 2001 From: druizz90 <32929872+druizz90@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:48:27 +0200 Subject: [PATCH 4/5] Improve type hints for nullable types in testing module --- connexion/testing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/connexion/testing.py b/connexion/testing.py index fb36c1c60..0abcc897a 100644 --- a/connexion/testing.py +++ b/connexion/testing.py @@ -15,9 +15,9 @@ def __init__( self, *, context: t.Optional[dict] = None, - operation: AbstractOperation = None, - receive: Receive = None, - scope: Scope = None, + operation: t.Optional[AbstractOperation] = None, + receive: t.Optional[Receive] = None, + scope: t.Optional[Scope] = None, ) -> None: self.context = context if context is not None else self.build_context() self.operation = operation if operation is not None else self.build_operation() From 5f99dce0489399de22997b796b54936a1a73f721 Mon Sep 17 00:00:00 2001 From: druizz90 <32929872+druizz90@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:52:19 +0200 Subject: [PATCH 5/5] Improve type hints for nullable callables --- connexion/apps/abstract.py | 2 +- connexion/apps/asynchronous.py | 4 ++-- connexion/apps/flask.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/connexion/apps/abstract.py b/connexion/apps/abstract.py index 5b299a786..a332ade66 100644 --- a/connexion/apps/abstract.py +++ b/connexion/apps/abstract.py @@ -200,7 +200,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, **options, ): """ diff --git a/connexion/apps/asynchronous.py b/connexion/apps/asynchronous.py index a0ac548f3..eb51e0afa 100644 --- a/connexion/apps/asynchronous.py +++ b/connexion/apps/asynchronous.py @@ -113,7 +113,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, methods: t.List[str] = None, **options, ): @@ -203,7 +203,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, **options, ): self._middleware_app.add_url_rule( diff --git a/connexion/apps/flask.py b/connexion/apps/flask.py index 4c7c14ec7..2017148ef 100644 --- a/connexion/apps/flask.py +++ b/connexion/apps/flask.py @@ -120,7 +120,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, **options, ): return self.blueprint.add_url_rule(rule, endpoint, view_func, **options) @@ -154,7 +154,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, **options, ): return self.app.add_url_rule(rule, endpoint, view_func, **options) @@ -260,7 +260,7 @@ def add_url_rule( self, rule, endpoint: t.Optional[str] = None, - view_func: t.Callable = None, + view_func: t.Optional[t.Callable] = None, **options, ): self._middleware_app.add_url_rule(