From 391aa07d3c68d4d0888b31fdea62516df9e19cb8 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 2 Jun 2023 08:09:41 -0700 Subject: [PATCH] implement `format_map` --- CHANGES.rst | 1 + src/markupsafe/__init__.py | 4 ++++ tests/test_markupsafe.py | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 90476f76..92a77d44 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,7 @@ Version 2.1.3 Unreleased - Fix static typing for basic ``str`` methods on ``Markup``. :issue:`358` +- Implement ``format_map`` method. :issue:`370` Version 2.1.2 diff --git a/src/markupsafe/__init__.py b/src/markupsafe/__init__.py index c593a133..1e773ced 100644 --- a/src/markupsafe/__init__.py +++ b/src/markupsafe/__init__.py @@ -208,6 +208,10 @@ def format(self, *args: t.Any, **kwargs: t.Any) -> "Markup": formatter = EscapeFormatter(self.escape) return self.__class__(formatter.vformat(self, args, kwargs)) + def format_map(self, map: t.Mapping[str, t.Any]) -> str: # type: ignore[override] + formatter = EscapeFormatter(self.escape) + return self.__class__(formatter.vformat(self, (), map)) + def __html_format__(self, format_spec: str) -> "Markup": if format_spec: raise ValueError("Unsupported format specification for Markup.") diff --git a/tests/test_markupsafe.py b/tests/test_markupsafe.py index a62ebf9d..ea9a9187 100644 --- a/tests/test_markupsafe.py +++ b/tests/test_markupsafe.py @@ -108,6 +108,11 @@ def test_format(): assert result == "" +def test_format_map(): + result = Markup("{value}").format_map({"value": ""}) + assert result == "<value>" + + def test_formatting_empty(): formatted = Markup("{}").format(0) assert formatted == Markup("0")