diff options
author | David Lord <davidism@gmail.com> | 2021-08-05 16:50:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-05 16:50:11 -0700 |
commit | cce3c69633dbfe9d8c277372b36e5d5e682c1611 (patch) | |
tree | c37e57c34b7cce4c8f34895a22a3a78985a37cd3 | |
parent | 37c44e554b1b05f8cec09424ad6057b79a0d88b0 (diff) | |
parent | 3d809aed7b7b6af5c371bab68666857087335af9 (diff) | |
download | markupsafe-cce3c69633dbfe9d8c277372b36e5d5e682c1611.tar.gz |
Merge pull request #224 from mart-e/mod-missing-placeholder
raise error upon missing placeholder
-rw-r--r-- | CHANGES.rst | 3 | ||||
-rw-r--r-- | src/markupsafe/__init__.py | 7 | ||||
-rw-r--r-- | tests/test_markupsafe.py | 6 |
3 files changed, 15 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 9d19bc8..e15be20 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,9 @@ Version 2.1.0 Unreleased +- Raise error on missing single placeholder during string + interpolation. :issue:`225` + Version 2.0.2 ------------- diff --git a/src/markupsafe/__init__.py b/src/markupsafe/__init__.py index 048ddb8..d1bf187 100644 --- a/src/markupsafe/__init__.py +++ b/src/markupsafe/__init__.py @@ -102,9 +102,14 @@ class Markup(str): def __mod__(self, arg: t.Any) -> "Markup": if isinstance(arg, tuple): + # a tuple of arguments, each wrapped arg = tuple(_MarkupEscapeHelper(x, self.escape) for x in arg) - else: + elif hasattr(type(arg), "__getitem__") and not isinstance(arg, str): + # a mapping of arguments, wrapped arg = _MarkupEscapeHelper(arg, self.escape) + else: + # a single argument, wrapped with the helper and a tuple + arg = (_MarkupEscapeHelper(arg, self.escape),) return self.__class__(super().__mod__(arg)) diff --git a/tests/test_markupsafe.py b/tests/test_markupsafe.py index fcd9347..1d4ac2e 100644 --- a/tests/test_markupsafe.py +++ b/tests/test_markupsafe.py @@ -45,6 +45,12 @@ def test_html_interop(): assert result == "<strong><em>awesome</em></strong>" +@pytest.mark.parametrize("args", ["foo", 42, ("foo", 42)]) +def test_missing_interpol(args): + with pytest.raises(TypeError): + Markup("<em></em>") % args + + def test_tuple_interpol(): result = Markup("<em>%s:%s</em>") % ("<foo>", "<bar>") expect = Markup("<em><foo>:<bar></em>") |