Skip to content

Commit

Permalink
implement format_map
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Jun 2, 2023
1 parent 60e7eb9 commit 391aa07
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/markupsafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_markupsafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def test_format():
assert result == "<bar/>"


def test_format_map():
result = Markup("<em>{value}</em>").format_map({"value": "<value>"})
assert result == "<em>&lt;value&gt;</em>"


def test_formatting_empty():
formatted = Markup("{}").format(0)
assert formatted == Markup("0")
Expand Down

0 comments on commit 391aa07

Please sign in to comment.