diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ab4aa795..175ecdc1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -34,6 +34,7 @@ Bug Fixes :: + * Fixing werkzeug 3 deprecated version import. Import is replaced by new style version check with importlib (#573) [Ryu-CZ] * Fixing flask 3.0+ compatibility of `ModuleNotFoundError: No module named 'flask.scaffold'` Import error. (#567) [Ryu-CZ] diff --git a/flask_restx/api.py b/flask_restx/api.py index bd0413dd..131697e4 100644 --- a/flask_restx/api.py +++ b/flask_restx/api.py @@ -28,20 +28,13 @@ InternalServerError, ) -from werkzeug import __version__ as werkzeug_version - -if werkzeug_version.split(".")[0] >= "2": - from werkzeug.wrappers import Response as BaseResponse -else: - from werkzeug.wrappers import BaseResponse - from . import apidoc from .mask import ParseError, MaskError from .namespace import Namespace from .postman import PostmanCollectionV1 from .resource import Resource from .swagger import Swagger -from .utils import default_id, camel_to_dash, unpack, import_check_view_func +from .utils import default_id, camel_to_dash, unpack, import_check_view_func, BaseResponse from .representations import output_json from ._http import HTTPStatus diff --git a/flask_restx/resource.py b/flask_restx/resource.py index dd23d0b5..c7dc5eaf 100644 --- a/flask_restx/resource.py +++ b/flask_restx/resource.py @@ -1,15 +1,10 @@ from flask import request from flask.views import MethodView -from werkzeug import __version__ as werkzeug_version -if werkzeug_version.split(".")[0] >= "2": - from werkzeug.wrappers import Response as BaseResponse -else: - from werkzeug.wrappers import BaseResponse from .model import ModelBase -from .utils import unpack +from .utils import unpack, BaseResponse class Resource(MethodView): diff --git a/flask_restx/utils.py b/flask_restx/utils.py index 367527a5..ce94859c 100644 --- a/flask_restx/utils.py +++ b/flask_restx/utils.py @@ -19,10 +19,29 @@ "not_none", "not_none_sorted", "unpack", + "BaseResponse", "import_check_view_func", ) +def import_werkzeug_response(): + """Resolve `werkzeug` `Response` class import because + `BaseResponse` was renamed in version 2.* to `Response`""" + import importlib.metadata + + werkzeug_major = int(importlib.metadata.version("werkzeug").split(".")[0]) + if werkzeug_major < 2: + from werkzeug.wrappers import BaseResponse + + return BaseResponse + + from werkzeug.wrappers import Response + + return Response + + +BaseResponse = import_werkzeug_response() + class FlaskCompatibilityWarning(DeprecationWarning): pass diff --git a/tests/test_utils.py b/tests/test_utils.py index 36975820..33e1c698 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -47,6 +47,11 @@ def test_recursions_with_empty(self): assert utils.merge(a, b) == b +class UnpackImportResponse(object): + def test_import_werkzeug_response(self): + assert utils.import_werkzeug_response() != None + + class CamelToDashTestCase(object): def test_no_transform(self): assert utils.camel_to_dash("test") == "test"