Skip to content

Commit a918650

Browse files
author
Tomáš Trval
committed
nicer code and changelog note
1 parent e5599c8 commit a918650

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Bug Fixes
3535
::
3636

3737
* Fixing test as HTTP Header MIMEAccept expects quality-factor number in form of `X.X` (#547) [chipndell]
38-
38+
* Fixing flask 3.0+ compatibility of `flask.scaffold import _endpoint_from_view_func` Import error. (#565) [Ryu-CZ]
3939

4040
.. _enhancements-1.2.0:
4141

flask_restx/utils.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ def unpack(response, default_code=HTTPStatus.OK):
126126
raise ValueError("Too many response values")
127127

128128

129+
def to_view_name(view_func: typing.Callable) -> str:
130+
"""Helper that returns the default endpoint for a given
131+
function. This always is the function name.
132+
133+
Note: copy of simple flask internal helper
134+
"""
135+
assert view_func is not None, "expected view func if endpoint is not provided."
136+
return view_func.__name__
137+
138+
129139
def import_check_view_func():
130140
"""
131141
Resolve import flask _endpoint_from_view_func.
@@ -138,14 +148,6 @@ def import_check_view_func():
138148
import importlib.metadata
139149

140150
flask_version = importlib.metadata.version("flask").split(".")
141-
142-
def local_endpoint_from_view_func(view_func: typing.Callable) -> str:
143-
"""Copy of flask internal helper that returns the default endpoint for a given
144-
function. This always is the function name.
145-
"""
146-
assert view_func is not None, "expected view func if endpoint is not provided."
147-
return view_func.__name__
148-
149151
try:
150152
if flask_version[0] == "1":
151153
from flask.helpers import _endpoint_from_view_func
@@ -160,5 +162,5 @@ def local_endpoint_from_view_func(view_func: typing.Callable) -> str:
160162
warnings.simplefilter("once", FlaskCompatibilityWarning)
161163
_endpoint_from_view_func = None
162164
if _endpoint_from_view_func is None:
163-
_endpoint_from_view_func = local_endpoint_from_view_func
165+
_endpoint_from_view_func = to_view_name
164166
return _endpoint_from_view_func

tests/test_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ def test_value_headers_default_code(self):
9898
def test_too_many_values(self):
9999
with pytest.raises(ValueError):
100100
utils.unpack((None, None, None, None))
101+
102+
103+
class ToViewNameTest(object):
104+
def test_none(self):
105+
with pytest.raises(AssertionError):
106+
_ = utils.to_view_name(None)
107+
108+
def test_name(self):
109+
assert self.test_none == self.test_none.__name__
110+
111+
112+
class ImportCheckViewFuncTest(object):
113+
def test_callable(self):
114+
assert callable(utils.import_check_view_func())

0 commit comments

Comments
 (0)