Skip to content

Commit 22de1c1

Browse files
alliasgherxrmx
andauthored
fix(flask): stop reading deprecated flask.__version__ (#4422)
* fix(flask): stop reading deprecated flask.__version__ Flask 3.1 deprecated the module-level __version__ attribute and emits a DeprecationWarning on access; 3.2 will remove it. The Flask instrumentor was hitting that warning at import time via _IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(flask.__version__) >= package_version.parse("3.1.0") even though it already computes the installed Flask version via importlib.metadata.version("flask") a few lines below. Use that flask_version for the 3.1+ check and drop the deprecated attribute access entirely. Fixes #4402 Signed-off-by: Ali <alliasgher123@gmail.com> * style: apply ruff format Signed-off-by: alliasgher <alliasgher123@gmail.com> * fix(flask): also stop reading deprecated flask.__version__ in tests Per @xrmx review — the test suite was the remaining caller of the deprecated attribute. Switch to importlib.metadata.version("flask") to match the production code change. Signed-off-by: alliasgher <alliasgher123@gmail.com> * Apply suggestions from code review Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com> * Update CHANGELOG.md --------- Signed-off-by: Ali <alliasgher123@gmail.com> Signed-off-by: alliasgher <alliasgher123@gmail.com> Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 1190dfc commit 22de1c1

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
([#4427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4427))
2727
- `opentelemetry-instrumentation-flask`: Clean up environ keys in `_teardown_request` to prevent duplicate execution
2828
([#4341](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4341))
29+
- `opentelemetry-instrumentation-flask`: Stop reading the deprecated (from 3.1) `flask.__version__` attribute; resolve the Flask version via `importlib.metadata`
30+
([#4422](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4422))
2931

3032
### Breaking changes
3133

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,6 @@ def response_hook(span: Span, status: str, response_headers: List):
301301
)
302302

303303
_logger = getLogger(__name__)
304-
# Global constants for Flask 3.1+ streaming context cleanup
305-
_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(
306-
flask.__version__
307-
) >= package_version.parse("3.1.0")
308304

309305
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
310306
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
@@ -316,6 +312,11 @@ def response_hook(span: Span, status: str, response_headers: List):
316312

317313
flask_version = version("flask")
318314

315+
# Global constant for Flask 3.1+ streaming context cleanup
316+
_IS_FLASK_31_PLUS = package_version.parse(
317+
flask_version
318+
) >= package_version.parse("3.1.0")
319+
319320
if package_version.parse(flask_version) >= package_version.parse("2.2.0"):
320321

321322
def _request_ctx_ref() -> weakref.ReferenceType:

instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io
2121
import threading
2222
import time
23+
from importlib.metadata import version
2324
from unittest import mock, skipIf
2425

2526
import flask
@@ -37,7 +38,7 @@
3738
class TestFlaskCompatibility(WsgiTestBase):
3839
def setUp(self):
3940
super().setUp()
40-
self.flask_version = flask.__version__
41+
self.flask_version = version("flask")
4142

4243
def test_streaming_response_context_cleanup(self):
4344
"""Test that streaming responses properly clean up context"""

0 commit comments

Comments
 (0)