Skip to content

Commit 91b8f48

Browse files
authored
chore: fix for 2.21 psycopg circular import (#13411)
Fix to the circular import with 2.21 an the psycopg2 contrib. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 55d531a commit 91b8f48

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

ddtrace/contrib/internal/psycopg/patch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from ddtrace import config
77
from ddtrace.contrib import dbapi
8-
from ddtrace.trace import Pin
98

109

1110
try:
@@ -116,6 +115,8 @@ def _patch(psycopg_module):
116115
return
117116
psycopg_module._datadog_patch = True
118117

118+
from ddtrace.trace import Pin
119+
119120
Pin(_config=config.psycopg).onto(psycopg_module)
120121

121122
if psycopg_module.__name__ == "psycopg2":
@@ -162,6 +163,8 @@ def _unpatch(psycopg_module):
162163
psycopg_module.Connection.connect = _original_connect
163164
psycopg_module.AsyncConnection.connect = _original_async_connect
164165

166+
from ddtrace.trace import Pin
167+
165168
pin = Pin.get_from(psycopg_module)
166169
if pin:
167170
pin.remove_from(psycopg_module)
@@ -180,6 +183,8 @@ def init_cursor_from_connection(wrapped_cursor_cls, _, args, kwargs):
180183
if not connection:
181184
return wrapped_cursor_cls(*args, **kwargs)
182185

186+
from ddtrace.trace import Pin
187+
183188
pin = Pin.get_from(connection).clone()
184189
cfg = config.psycopg
185190

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fix a potential circular import with the psycopg2 contrib.

tests/contrib/psycopg2/test_psycopg_patch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# removed the ``_generated`` suffix from the file name, to prevent the content
44
# from being overwritten by future re-generations.
55

6+
import pytest
7+
68
from ddtrace.contrib.internal.psycopg.patch import get_version
79
from ddtrace.contrib.internal.psycopg.patch import get_versions
810
from ddtrace.contrib.internal.psycopg.patch import patch
@@ -40,3 +42,17 @@ def test_and_emit_get_version(self):
4042
assert versions.get("psycopg2")
4143
emit_integration_and_version_to_test_agent("psycopg", versions["psycopg2"], "psycopg2")
4244
unpatch()
45+
46+
47+
def test_psycopg_circular_import():
48+
import importlib
49+
import sys
50+
51+
patch_module_name = "ddtrace.contrib.internal.psycopg.patch"
52+
if patch_module_name in sys.modules:
53+
del sys.modules[patch_module_name]
54+
55+
try:
56+
importlib.import_module(patch_module_name)
57+
except Exception as e:
58+
pytest.fail(f"Failed to import psycopg patch module: {str(e)}")

0 commit comments

Comments
 (0)