Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate: mdformat.codepoints.ASCII_WHITESPACE #481

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/users/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/mdformat/codepoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions src/mdformat/renderer/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
with pytest.warns(DeprecationWarning):
mdformat.codepoints.ASCII_WHITESPACE