Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure locals_max_{length,string} work as expected in case they are None #675

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/structlog/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ def __init__(
max_frames: int = MAX_FRAMES,
use_rich: bool = True,
) -> None:
if locals_max_length < 0:
if locals_max_length is not None and locals_max_length < 0:
msg = f'"locals_max_length" must be >= 0: {locals_max_length}'
raise ValueError(msg)
if locals_max_string < 0:
if locals_max_string is not None and locals_max_string < 0:
msg = f'"locals_max_string" must be >= 0: {locals_max_string}'
raise ValueError(msg)
if max_frames < 2:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,40 @@ def bar(n):
]


@pytest.mark.parametrize(
("kwargs", "local_variable"),
[
(
{"locals_max_string": None},
"x" * (tracebacks.LOCALS_MAX_STRING + 10),
),
(
{"locals_max_length": None},
list(range(tracebacks.LOCALS_MAX_LENGTH + 10)),
),
],
)
def test_exception_dict_transformer_locals_max_accept_none_argument(
kwargs, local_variable
):
try:
my_local = local_variable
1 / 0
except Exception as e:
format_json = tracebacks.ExceptionDictTransformer(
show_locals=True, **kwargs
)
result = format_json((type(e), e, e.__traceback__))

_ = my_local

assert len(result) == 1
assert len(result[0]["frames"]) == 1

frame = result[0]["frames"][0]
assert frame["locals"]["my_local"].strip("'") == str(local_variable)


def test_json_traceback():
"""
Tracebacks are formatted to JSON with all information.
Expand Down