Skip to content

Commit adb926e

Browse files
author
Julien Danjou
authored
refactor: raise error on wrong unwrap (#2864)
The current `unwrap` function hides error on calling `unwrap` on wrong object. This has two bad side effects: 1. It hides existing error. There are integrations that do their unpatch uncorrectly and this hides it. 2. It makes it difficult for contribution author to catch their errors. If they ever write a wrong unpatch() function which is used in testing, it can have wrong side effects by not unpatching correctly. This makes sure that if unwrap() is wrongly called, it raises an error rather than hiding it.
1 parent e1ab88c commit adb926e

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

ddtrace/contrib/algoliasearch/patch.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def unpatch():
5555
if getattr(algoliasearch, DD_PATCH_ATTR, False):
5656
setattr(algoliasearch, DD_PATCH_ATTR, False)
5757

58-
if algoliasearch_version < (2, 0) and algoliasearch_version >= (1, 0):
59-
_u(algoliasearch.index.Index, "search")
60-
elif algoliasearch_version >= (2, 0) and algoliasearch_version < (3, 0):
61-
from algoliasearch import search_index
62-
63-
_u(search_index.SearchIndex, "search")
64-
else:
65-
return
58+
if algoliasearch_version < (2, 0) and algoliasearch_version >= (1, 0):
59+
_u(algoliasearch.index.Index, "search")
60+
elif algoliasearch_version >= (2, 0) and algoliasearch_version < (3, 0):
61+
from algoliasearch import search_index
62+
63+
_u(search_index.SearchIndex, "search")
64+
else:
65+
return
6666

6767

6868
# DEV: this maps serves the dual purpose of enumerating the algoliasearch.search() query_args that

ddtrace/contrib/asyncio/patch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def unpatch():
3131
if getattr(asyncio, "_datadog_patch", False):
3232
setattr(asyncio, "_datadog_patch", False)
3333

34-
if sys.version_info < (3, 7, 0):
35-
_u(asyncio.BaseEventLoop, "create_task")
34+
if sys.version_info < (3, 7, 0):
35+
_u(asyncio.BaseEventLoop, "create_task")
3636

37-
# also unpatch event loop if not inheriting the already unwrapped create_task from BaseEventLoop
38-
loop = asyncio.get_event_loop()
39-
if isinstance(loop.create_task, ObjectProxy):
40-
_u(loop, "create_task")
37+
# also unpatch event loop if not inheriting the already unwrapped create_task from BaseEventLoop
38+
loop = asyncio.get_event_loop()
39+
if isinstance(loop.create_task, ObjectProxy):
40+
_u(loop, "create_task")

ddtrace/contrib/molten/patch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def unpatch():
6262

6363
_u(molten.BaseApp, "__init__")
6464
_u(molten.App, "__call__")
65-
_u(molten.Router, "add_route")
6665

6766

6867
def patch_app_call(wrapped, instance, args, kwargs):

ddtrace/contrib/tornado/patch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def unpatch():
6767
_u(tornado.web.RequestHandler, "on_finish")
6868
_u(tornado.web.RequestHandler, "log_exception")
6969
_u(tornado.web.Application, "__init__")
70-
_u(tornado.concurrent, "run_on_executor")
7170
_u(tornado.template.Template, "generate")
7271

7372
# unpatch `futures`

ddtrace/utils/wrappers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def iswrapped(obj, attr=None):
2727

2828
def unwrap(obj, attr):
2929
# type: (Any, str) -> None
30-
f = getattr(obj, attr, None)
31-
if f and isinstance(f, wrapt.ObjectProxy) and hasattr(f, "__wrapped__"):
32-
setattr(obj, attr, f.__wrapped__)
30+
f = getattr(obj, attr)
31+
setattr(obj, attr, f.__wrapped__)
3332

3433

3534
@deprecated("`wrapt` library is used instead", version="1.0.0")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
The ``ddtrace.utils.wrappers.unwrap`` function now raises an error if
5+
trying to unwrap a non-wrapped object.

0 commit comments

Comments
 (0)