Skip to content

fail-on-template-vars: modernize stack inspection code #1129

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

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
18 changes: 5 additions & 13 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,11 @@

# Try to use topmost `self.origin` first (Django 1.9+, and with
# TEMPLATE_DEBUG)..
for f in stack[2:]:
func = f[3]
if func == "render":
frame = f[0]
for frame_info in stack[2:]:
if frame_info.function == "render":
origin: str | None
try:
origin = frame.f_locals["self"].origin
origin = frame_info.frame.f_locals["self"].origin
except (AttributeError, KeyError):
origin = None
if origin is not None:
Expand All @@ -686,16 +684,10 @@

# finding the ``render`` needle in the stack
frameinfo = reduce(
lambda x, y: y[3] == "render" and "base.py" in y[1] and y or x, stack
lambda x, y: y if y.function == "render" and "base.py" in y.filename else x, stack
)
# assert 0, stack
frame = frameinfo[0]
# finding only the frame locals in all frame members
f_locals = reduce(
lambda x, y: y[0] == "f_locals" and y or x, inspect.getmembers(frame)
)[1]
# ``django.template.base.Template``
template = f_locals["self"]
template = frameinfo.frame.f_locals["self"]

Check warning on line 690 in pytest_django/plugin.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/plugin.py#L690

Added line #L690 was not covered by tests
if isinstance(template, Template):
name: str = template.name
return name
Expand Down