From a5fb9cb9a4245f1d3d18904473b50ad571ec312c Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 14 Apr 2026 02:35:29 +0500 Subject: [PATCH 1/5] 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 --- CHANGELOG.md | 5 +++++ .../opentelemetry/instrumentation/flask/__init__.py | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8818f8b070..7c65e78e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump `pylint` to `4.0.5` ([#4244](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4244)) +### Fixed + +- `opentelemetry-instrumentation-flask`: Stop reading the deprecated `flask.__version__` attribute; resolve the Flask version via `importlib.metadata` only so import no longer emits a `DeprecationWarning` under Flask 3.1+ + ([#4422](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4422)) + ### Breaking changes - Drop Python 3.9 support diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index ced6c8c6f7..d42e126e4b 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -301,10 +301,6 @@ def response_hook(span: Span, status: str, response_headers: List): ) _logger = getLogger(__name__) -# Global constants for Flask 3.1+ streaming context cleanup -_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse( - flask.__version__ -) >= package_version.parse("3.1.0") _ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key" _ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key" @@ -314,8 +310,16 @@ def response_hook(span: Span, status: str, response_headers: List): _excluded_urls_from_env = get_excluded_urls("FLASK") +# Use importlib.metadata rather than flask.__version__: the latter has been +# deprecated in Flask 3.1 and emits a DeprecationWarning on import; it is +# scheduled for removal in Flask 3.2. flask_version = version("flask") +# Global constant for Flask 3.1+ streaming context cleanup. +_IS_FLASK_31_PLUS = package_version.parse(flask_version) >= package_version.parse( + "3.1.0" +) + if package_version.parse(flask_version) >= package_version.parse("2.2.0"): def _request_ctx_ref() -> weakref.ReferenceType: From 6d58dbba528f72e3295585a73ce72fcf851d5eb2 Mon Sep 17 00:00:00 2001 From: alliasgher Date: Wed, 15 Apr 2026 04:27:09 +0500 Subject: [PATCH 2/5] style: apply ruff format Signed-off-by: alliasgher --- .../src/opentelemetry/instrumentation/flask/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index d42e126e4b..0379574489 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -316,9 +316,9 @@ def response_hook(span: Span, status: str, response_headers: List): flask_version = version("flask") # Global constant for Flask 3.1+ streaming context cleanup. -_IS_FLASK_31_PLUS = package_version.parse(flask_version) >= package_version.parse( - "3.1.0" -) +_IS_FLASK_31_PLUS = package_version.parse( + flask_version +) >= package_version.parse("3.1.0") if package_version.parse(flask_version) >= package_version.parse("2.2.0"): From 93fb7da1a7ecc930fae11790e1639afc365b6b14 Mon Sep 17 00:00:00 2001 From: alliasgher Date: Wed, 15 Apr 2026 15:56:45 +0500 Subject: [PATCH 3/5] fix(flask): also stop reading deprecated flask.__version__ in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../tests/test_flask_compatibility.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py index 8e1804038b..3263fb2402 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py @@ -20,6 +20,7 @@ import io import threading import time +from importlib.metadata import version from unittest import mock, skipIf import flask @@ -35,7 +36,7 @@ class TestFlaskCompatibility(WsgiTestBase): def setUp(self): super().setUp() - self.flask_version = flask.__version__ + self.flask_version = version("flask") def test_streaming_response_context_cleanup(self): """Test that streaming responses properly clean up context""" From 0a2422da2ef1359329ffc3a1e8d072fab00ab2ad Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 21 Apr 2026 18:31:03 +0200 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Riccardo Magliocchetti --- CHANGELOG.md | 2 +- .../src/opentelemetry/instrumentation/flask/__init__.py | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c65e78e55..78fc1c84f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- `opentelemetry-instrumentation-flask`: Stop reading the deprecated `flask.__version__` attribute; resolve the Flask version via `importlib.metadata` only so import no longer emits a `DeprecationWarning` under Flask 3.1+ +- `opentelemetry-instrumentation-flask`: Stop reading the deprecated (from 3.1) `flask.__version__` attribute; resolve the Flask version via `importlib.metadata` ([#4422](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4422)) ### Breaking changes diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 0379574489..1c35d48301 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -310,12 +310,9 @@ def response_hook(span: Span, status: str, response_headers: List): _excluded_urls_from_env = get_excluded_urls("FLASK") -# Use importlib.metadata rather than flask.__version__: the latter has been -# deprecated in Flask 3.1 and emits a DeprecationWarning on import; it is -# scheduled for removal in Flask 3.2. flask_version = version("flask") -# Global constant for Flask 3.1+ streaming context cleanup. +# Global constant for Flask 3.1+ streaming context cleanup _IS_FLASK_31_PLUS = package_version.parse( flask_version ) >= package_version.parse("3.1.0") From 7fdc99334165b5fd383a08062ee913ec8a30d13f Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 22 Apr 2026 15:15:04 +0200 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7948c98d3..7cfeefea9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,9 +26,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4427)) - `opentelemetry-instrumentation-flask`: Clean up environ keys in `_teardown_request` to prevent duplicate execution ([#4341](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4341)) - -### Fixed - - `opentelemetry-instrumentation-flask`: Stop reading the deprecated (from 3.1) `flask.__version__` attribute; resolve the Flask version via `importlib.metadata` ([#4422](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4422))