diff options
-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>") |