From 391aa07d3c68d4d0888b31fdea62516df9e19cb8 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 2 Jun 2023 08:09:41 -0700 Subject: [PATCH 1/2] 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") From c438e4d5ab799fcafc165472526b7c81455f58e7 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 2 Jun 2023 08:21:45 -0700 Subject: [PATCH 2/2] implement more str methods casefold, removeprefix, removesuffix --- CHANGES.rst | 3 ++- src/markupsafe/__init__.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 92a77d44..cc503598 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,8 +3,9 @@ Version 2.1.3 Unreleased +- Implement ``format_map``, ``casefold``, ``removeprefix``, and ``removesuffix`` + methods. :issue:`370` - 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 1e773ced..40d165eb 100644 --- a/src/markupsafe/__init__.py +++ b/src/markupsafe/__init__.py @@ -1,6 +1,7 @@ import functools import re import string +import sys import typing as t if t.TYPE_CHECKING: @@ -193,6 +194,11 @@ def escape(cls, s: t.Any) -> "Markup": expandtabs = _simple_escaping_wrapper(str.expandtabs) swapcase = _simple_escaping_wrapper(str.swapcase) zfill = _simple_escaping_wrapper(str.zfill) + casefold = _simple_escaping_wrapper(str.casefold) + + if sys.version_info >= (3, 9): + removeprefix = _simple_escaping_wrapper(str.removeprefix) + removesuffix = _simple_escaping_wrapper(str.removesuffix) def partition(self, sep: str) -> t.Tuple["Markup", "Markup", "Markup"]: l, s, r = super().partition(self.escape(sep))