Skip to content

Commit

Permalink
deprecate: mdformat.codepoints.ASCII_WHITESPACE (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Nov 19, 2024
1 parent 43c6370 commit c22a0b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
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

0 comments on commit c22a0b8

Please sign in to comment.