Skip to content

Avoid hard-failure #243

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

Open
wants to merge 2 commits 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
7 changes: 4 additions & 3 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def override_tag(
register: django.template.Library,
name: str,
default_html: typing.Optional[typing.Any] = UNSPECIFIED,
default_html: typing.Optional[typing.Any] = None,
):
"""
An utility that helps you override original tags for use in your pattern library.
Expand Down Expand Up @@ -95,14 +95,15 @@ def node_render(context):
Warning,
)
else:
raise TypeError(
warnings.warn(
'default_html argument to override_tag must be a string (line %s in "%s")'
% (trace.lineno, trace.filename)
)
return ""

# Render provided default;
# if no stub data supplied.
return default_html
return default_html or ""
else:
logger.warning(
'No default or stub data defined for the "%s" tag in the "%s" template',
Expand Down
10 changes: 5 additions & 5 deletions tests/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_falsey_default_html_overide(self):


class TagsTestFailCase(SimpleTestCase):
def test_bad_default_html_warning(self):
def test_bad_default_html_warning_on_older_django_versions(self):
"""
Test that the library raises a warning when passing a non-string `default_html` argument to `override_tag`
in Django < 4.0
Expand Down Expand Up @@ -79,13 +79,13 @@ def test_bad_default_html_warning(self):
str(cm.warnings[0]),
)

def test_bad_default_html_error(self):
def test_bad_default_html_warning_on_newer_django_versions(self):
"""
Test that the library raises a TypeError when passing a non-string `default_html` argument to `override_tag`
Test that the library raises a warning when passing a non-string `default_html` argument to `override_tag`
in Django >= 4.0
"""
with patch("django.VERSION", (4, 2, 0, "final", 0)):
with self.assertRaises(TypeError) as cm:
with self.assertWarns(Warning) as cm:
template_name = (
"patterns/atoms/tags_test_atom/invalid_tags_test_atom.html.fail"
)
Expand All @@ -97,5 +97,5 @@ def test_bad_default_html_error(self):
)
self.assertIn(
"default_html argument to override_tag must be a string",
str(cm.exception),
str(cm.warnings[0]),
)