From 5191fef64b535c9b721fd8e0308227971b05f4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Wed, 19 Jun 2024 17:03:01 +0200 Subject: [PATCH] adapter.http: improve type hints Remove 'type: ignore' comments now that we require werkzeug >=3.0.3 [1]. Furthermore, fix the type hint of `WSGIApp._get_slice()` and make two other 'type: ignore' comments more explicit. [1]: https://github.com/pallets/werkzeug/issues/2836 --- basyx/aas/adapter/http.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/basyx/aas/adapter/http.py b/basyx/aas/adapter/http.py index 10a37197e..3cc2dfe9b 100644 --- a/basyx/aas/adapter/http.py +++ b/basyx/aas/adapter/http.py @@ -5,10 +5,6 @@ # # SPDX-License-Identifier: MIT -# TODO: remove this once the werkzeug type annotations have been fixed -# https://github.com/pallets/werkzeug/issues/2836 -# mypy: disable-error-code="arg-type" - import abc import base64 import binascii @@ -18,7 +14,7 @@ import json import itertools -from lxml import etree # type: ignore +from lxml import etree import werkzeug.exceptions import werkzeug.routing import werkzeug.urls @@ -629,7 +625,7 @@ def _get_submodel_reference(cls, aas: model.AssetAdministrationShell, submodel_i raise NotFound(f"The AAS {aas!r} doesn't have a submodel reference to {submodel_id!r}!") @classmethod - def _get_slice(cls, request: Request, iterator: Iterator[T]) -> Tuple[Iterator[T], int]: + def _get_slice(cls, request: Request, iterator: Iterable[T]) -> Tuple[Iterator[T], int]: limit_str = request.args.get('limit', default="10") cursor_str = request.args.get('cursor', default="0") try: @@ -701,9 +697,7 @@ def handle_request(self, request: Request): endpoint, values = map_adapter.match() if endpoint is None: raise werkzeug.exceptions.NotImplemented("This route is not yet implemented.") - # TODO: remove this 'type: ignore' comment once the werkzeug type annotations have been fixed - # https://github.com/pallets/werkzeug/issues/2836 - return endpoint(request, values, map_adapter=map_adapter) # type: ignore[operator] + return endpoint(request, values, map_adapter=map_adapter) # any raised error that leaves this function will cause a 500 internal server error # so catch raised http exceptions and return them except werkzeug.exceptions.NotAcceptable as e: @@ -951,7 +945,8 @@ def post_submodel_submodel_elements_id_short_path(self, request: Request, url_ar raise BadRequest(f"{parent!r} is not a namespace, can't add child submodel element!") # TODO: remove the following type: ignore comment when mypy supports abstract types for Type[T] # see https://github.com/python/mypy/issues/5374 - new_submodel_element = HTTPApiDecoder.request_body(request, model.SubmodelElement, # type: ignore + new_submodel_element = HTTPApiDecoder.request_body(request, + model.SubmodelElement, # type: ignore[type-abstract] is_stripped_request(request)) try: parent.add_referable(new_submodel_element) @@ -971,7 +966,8 @@ def put_submodel_submodel_elements_id_short_path(self, request: Request, url_arg submodel_element = self._get_submodel_submodel_elements_id_short_path(url_args) # TODO: remove the following type: ignore comment when mypy supports abstract types for Type[T] # see https://github.com/python/mypy/issues/5374 - new_submodel_element = HTTPApiDecoder.request_body(request, model.SubmodelElement, # type: ignore + new_submodel_element = HTTPApiDecoder.request_body(request, + model.SubmodelElement, # type: ignore[type-abstract] is_stripped_request(request)) submodel_element.update_from(new_submodel_element) submodel_element.commit()