From 23c8ce66767fcb3b7d0b2e27fc985c9fdf9917b7 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:44:43 +0200 Subject: [PATCH 1/2] deprecate: `mdformat.codepoints.ASCII_WHITESPACE` --- docs/users/changelog.md | 5 +++++ src/mdformat/codepoints/__init__.py | 21 ++++++++++++++++++++- src/mdformat/renderer/_util.py | 4 +--- tests/test_api.py | 5 +++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/users/changelog.md b/docs/users/changelog.md index c1427df..1cf4408 100644 --- a/docs/users/changelog.md +++ b/docs/users/changelog.md @@ -3,6 +3,11 @@ This log documents all Python API or CLI breaking backwards incompatible changes. Note that there is currently no guarantee for a stable Markdown formatting style across versions. +## **unreleased** + +- Deprecated + - `mdformat.codepoints.ASCII_WHITESPACE` + ## 0.7.19 - Deprecated diff --git a/src/mdformat/codepoints/__init__.py b/src/mdformat/codepoints/__init__.py index 25cfe95..37929c1 100644 --- a/src/mdformat/codepoints/__init__.py +++ b/src/mdformat/codepoints/__init__.py @@ -5,8 +5,27 @@ "ASCII_WHITESPACE", ) +import warnings + from mdformat.codepoints._unicode_punctuation import UNICODE_PUNCTUATION from mdformat.codepoints._unicode_whitespace import UNICODE_WHITESPACE ASCII_CTRL = frozenset(chr(i) for i in range(32)) -ASCII_WHITESPACE = frozenset({chr(9), chr(10), chr(11), chr(12), chr(13), chr(32)}) + + +def __getattr__(name: str) -> frozenset[str]: + """Attribute getter fallback. + + Used during the deprecation period of `ASCII_WHITESPACE`. + """ + if name == "ASCII_WHITESPACE": + warnings.warn( + "ASCII_WHITESPACE is deprecated because CommonMark v0.30 no longer " + "defines ASCII whitespace.", + DeprecationWarning, + stacklevel=2, + ) + return frozenset({chr(9), chr(10), chr(11), chr(12), chr(13), chr(32)}) + raise AttributeError( + f"module {__name__!r} has no attribute {name!r}" + ) # pragma: no cover diff --git a/src/mdformat/renderer/_util.py b/src/mdformat/renderer/_util.py index c9313e9..45a17a4 100644 --- a/src/mdformat/renderer/_util.py +++ b/src/mdformat/renderer/_util.py @@ -65,9 +65,7 @@ def longest_consecutive_sequence(seq: str, char: str) -> int: def maybe_add_link_brackets(link: str) -> str: """Surround URI with brackets if required by spec.""" - if not link or ( - codepoints.ASCII_CTRL | codepoints.ASCII_WHITESPACE | {"(", ")"} - ).intersection(link): + if not link or (codepoints.ASCII_CTRL | {" ", "(", ")"}).intersection(link): return "<" + link + ">" return link diff --git a/tests/test_api.py b/tests/test_api.py index 20c8f80..b9712b0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -141,3 +141,8 @@ def test_mdrenderer_no_finalize(tmp_path): unfinalized = MDRenderer().render(tokens, {}, env, finalize=False) finalized = MDRenderer().render(tokens, {}, env) assert finalized == unfinalized + "\n\n[gl ref]: https://gitlab.com\n" + + +def test_ascii_whitespace_deprecation(tmp_path): + with pytest.warns(DeprecationWarning): + mdformat.codepoints.ASCII_WHITESPACE From 4b3a52e3b7f2ad9edc1ea7200d3638a11de5251f Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:47:46 +0200 Subject: [PATCH 2/2] remove unused tmp_path --- tests/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index b9712b0..ce90049 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -143,6 +143,6 @@ def test_mdrenderer_no_finalize(tmp_path): assert finalized == unfinalized + "\n\n[gl ref]: https://gitlab.com\n" -def test_ascii_whitespace_deprecation(tmp_path): +def test_ascii_whitespace_deprecation(): with pytest.warns(DeprecationWarning): mdformat.codepoints.ASCII_WHITESPACE