Skip to content

Commit 888c0e1

Browse files
authored
Rename 'with_locals' to 'include_local_variables' (#1924)
Created an alias 'include_local_variables' for the 'with_locals' options. Updated tests to make sure everything still works as expected.
1 parent 99ff1d2 commit 888c0e1

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

sentry_sdk/client.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@ def _get_options(*args, **kwargs):
7171

7272
for key, value in iteritems(options):
7373
if key not in rv:
74+
# Option "with_locals" was renamed to "include_local_variables"
75+
if key == "with_locals":
76+
msg = (
77+
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. "
78+
"Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
79+
)
80+
logger.warning(msg)
81+
rv["include_local_variables"] = value
82+
continue
83+
7484
raise TypeError("Unknown option %r" % (key,))
85+
7586
rv[key] = value
7687

7788
if rv["dsn"] is None:
@@ -213,7 +224,7 @@ def _prepare_event(
213224
"values": [
214225
{
215226
"stacktrace": current_stacktrace(
216-
self.options["with_locals"]
227+
self.options["include_local_variables"]
217228
),
218229
"crashed": False,
219230
"current": True,

sentry_sdk/consts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class ClientConstructor(object):
8989
def __init__(
9090
self,
9191
dsn=None, # type: Optional[str]
92-
with_locals=True, # type: bool
9392
max_breadcrumbs=DEFAULT_MAX_BREADCRUMBS, # type: int
9493
release=None, # type: Optional[str]
9594
environment=None, # type: Optional[str]
@@ -125,6 +124,7 @@ def __init__(
125124
before_send_transaction=None, # type: Optional[TransactionProcessor]
126125
project_root=None, # type: Optional[str]
127126
enable_tracing=None, # type: Optional[bool]
127+
include_local_variables=True, # type: Optional[bool]
128128
trace_propagation_targets=[ # noqa: B006
129129
MATCH_ALL
130130
], # type: Optional[Sequence[str]]

sentry_sdk/integrations/logging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _emit(self, record):
219219
"values": [
220220
{
221221
"stacktrace": current_stacktrace(
222-
client_options["with_locals"]
222+
client_options["include_local_variables"]
223223
),
224224
"crashed": False,
225225
"current": True,

sentry_sdk/utils.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def filename_for_module(module, abs_path):
591591
return abs_path
592592

593593

594-
def serialize_frame(frame, tb_lineno=None, with_locals=True):
594+
def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
595595
# type: (FrameType, Optional[int], bool) -> Dict[str, Any]
596596
f_code = getattr(frame, "f_code", None)
597597
if not f_code:
@@ -620,21 +620,23 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
620620
"context_line": context_line,
621621
"post_context": post_context,
622622
} # type: Dict[str, Any]
623-
if with_locals:
623+
if include_local_variables:
624624
rv["vars"] = frame.f_locals
625625

626626
return rv
627627

628628

629-
def current_stacktrace(with_locals=True):
629+
def current_stacktrace(include_local_variables=True):
630630
# type: (bool) -> Any
631631
__tracebackhide__ = True
632632
frames = []
633633

634634
f = sys._getframe() # type: Optional[FrameType]
635635
while f is not None:
636636
if not should_hide_frame(f):
637-
frames.append(serialize_frame(f, with_locals=with_locals))
637+
frames.append(
638+
serialize_frame(f, include_local_variables=include_local_variables)
639+
)
638640
f = f.f_back
639641

640642
frames.reverse()
@@ -668,12 +670,16 @@ def single_exception_from_error_tuple(
668670
)
669671

670672
if client_options is None:
671-
with_locals = True
673+
include_local_variables = True
672674
else:
673-
with_locals = client_options["with_locals"]
675+
include_local_variables = client_options["include_local_variables"]
674676

675677
frames = [
676-
serialize_frame(tb.tb_frame, tb_lineno=tb.tb_lineno, with_locals=with_locals)
678+
serialize_frame(
679+
tb.tb_frame,
680+
tb_lineno=tb.tb_lineno,
681+
include_local_variables=include_local_variables,
682+
)
677683
for tb in iter_stacks(tb)
678684
]
679685

tests/integrations/pure_eval/test_pure_eval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99

1010
@pytest.mark.parametrize("integrations", [[], [PureEvalIntegration()]])
11-
def test_with_locals_enabled(sentry_init, capture_events, integrations):
12-
sentry_init(with_locals=True, integrations=integrations)
11+
def test_include_local_variables_enabled(sentry_init, capture_events, integrations):
12+
sentry_init(include_local_variables=True, integrations=integrations)
1313
events = capture_events()
1414

1515
def foo():

tests/test_client.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
import os
33
import json
4+
import mock
45
import pytest
56
import subprocess
67
import sys
@@ -22,6 +23,7 @@
2223
from sentry_sdk.transport import Transport
2324
from sentry_sdk._compat import reraise, text_type, PY2
2425
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
26+
from sentry_sdk.utils import logger
2527
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
2628
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS
2729

@@ -291,8 +293,48 @@ def e(exc):
291293
pytest.raises(EventCapturedError, lambda: e(ValueError()))
292294

293295

294-
def test_with_locals_enabled(sentry_init, capture_events):
295-
sentry_init(with_locals=True)
296+
def test_with_locals_deprecation_enabled(sentry_init):
297+
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
298+
sentry_init(with_locals=True)
299+
300+
client = Hub.current.client
301+
assert "with_locals" not in client.options
302+
assert "include_local_variables" in client.options
303+
assert client.options["include_local_variables"]
304+
305+
fake_warning.assert_called_once_with(
306+
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
307+
)
308+
309+
310+
def test_with_locals_deprecation_disabled(sentry_init):
311+
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
312+
sentry_init(with_locals=False)
313+
314+
client = Hub.current.client
315+
assert "with_locals" not in client.options
316+
assert "include_local_variables" in client.options
317+
assert not client.options["include_local_variables"]
318+
319+
fake_warning.assert_called_once_with(
320+
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
321+
)
322+
323+
324+
def test_include_local_variables_deprecation(sentry_init):
325+
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
326+
sentry_init(include_local_variables=False)
327+
328+
client = Hub.current.client
329+
assert "with_locals" not in client.options
330+
assert "include_local_variables" in client.options
331+
assert not client.options["include_local_variables"]
332+
333+
fake_warning.assert_not_called()
334+
335+
336+
def test_include_local_variables_enabled(sentry_init, capture_events):
337+
sentry_init(include_local_variables=True)
296338
events = capture_events()
297339
try:
298340
1 / 0
@@ -307,8 +349,8 @@ def test_with_locals_enabled(sentry_init, capture_events):
307349
)
308350

309351

310-
def test_with_locals_disabled(sentry_init, capture_events):
311-
sentry_init(with_locals=False)
352+
def test_include_local_variables_disabled(sentry_init, capture_events):
353+
sentry_init(include_local_variables=False)
312354
events = capture_events()
313355
try:
314356
1 / 0
@@ -372,7 +414,7 @@ def bar():
372414

373415

374416
def test_attach_stacktrace_enabled_no_locals(sentry_init, capture_events):
375-
sentry_init(attach_stacktrace=True, with_locals=False)
417+
sentry_init(attach_stacktrace=True, include_local_variables=False)
376418
events = capture_events()
377419

378420
def foo():

0 commit comments

Comments
 (0)