Skip to content

Commit

Permalink
fix: BaseResponse import - replace wekzeug import __version__ by impo…
Browse files Browse the repository at this point in the history
…rtlib (#573) (#575)

* fix: BaseResponse import - replace wekzeug old style import dunder version by importlib (#573)

* add: import_werkzeug_response test

* modify: changelog - note fix (#573)

---------

Co-authored-by: Tomáš Trval <[email protected]>
Co-authored-by: Peter Doggart <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2023
1 parent b7d1ded commit a65dbe7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
9 changes: 1 addition & 8 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 1 addition & 6 deletions flask_restx/resource.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
19 changes: 19 additions & 0 deletions flask_restx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a65dbe7

Please sign in to comment.