Skip to content

Commit

Permalink
Change in :meth:~deprecated.sphinx.SphinxAdapter.get_deprecated_msg
Browse files Browse the repository at this point in the history
…: correct the regex used to get rid of the Sphinx cross-referencing syntax (issue #41).
  • Loading branch information
tantale committed Mar 13, 2021
1 parent efbe782 commit fe04e81
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
4 changes: 3 additions & 1 deletion deprecated/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ def get_deprecated_msg(self, wrapped, instance):
"""
msg = super(SphinxAdapter, self).get_deprecated_msg(wrapped, instance)
# Strip Sphinx cross reference syntax (like ":function:", ":py:func:" and ":py:meth:")
msg = re.sub(r"(:[a-z]{2,3})?:[a-z]{2,8}:(`.*?`)", r"\2", msg)
# Possible values are ":role:`foo`", ":domain:role:`foo`"
# where ``role`` and ``domain`` should match "[a-zA-Z]+"
msg = re.sub(r"(?: : [a-zA-Z]+ )? : [a-zA-Z]+ : (`[^`]*`)", r"\1", msg, flags=re.X)
return msg


Expand Down
57 changes: 37 additions & 20 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,28 +366,11 @@ def test_can_catch_warnings():
@pytest.mark.parametrize(
["reason", "expected"],
[
(
"Use :function:`bar` instead",
"Use `bar` instead"
),
(
"Use :py:func:`bar` instead",
"Use `bar` instead"),
(
"Use :py:meth:`Bar.bar` instead",
"Use `Bar.bar` instead"),
(
"Use :py:class:`Bar` instead",
"Use `Bar` instead"
),
(
"Use :py:func:`bar` or :py:meth:`Bar.bar` instead",
"Use `bar` or `Bar.bar` instead"
),
]
("Use :function:`bar` instead", "Use `bar` instead"),
("Use :py:func:`bar` instead", "Use `bar` instead"),
],
)
def test_sphinx_syntax_trimming(reason, expected):

@deprecated.sphinx.deprecated(version="4.5.6", reason=reason)
def foo():
pass
Expand All @@ -396,3 +379,37 @@ def foo():
foo()
warn = warns[0]
assert expected in str(warn.message)


# noinspection SpellCheckingInspection
@pytest.mark.parametrize(
"reason, expected",
[
# classic examples using the default domain (Python)
("Use :func:`bar` instead", "Use `bar` instead"),
("Use :function:`bar` instead", "Use `bar` instead"),
("Use :class:`Baz` instead", "Use `Baz` instead"),
("Use :exc:`Baz` instead", "Use `Baz` instead"),
("Use :exception:`Baz` instead", "Use `Baz` instead"),
("Use :meth:`Baz.bar` instead", "Use `Baz.bar` instead"),
("Use :method:`Baz.bar` instead", "Use `Baz.bar` instead"),
# other examples using a domain :
("Use :py:func:`bar` instead", "Use `bar` instead"),
("Use :cpp:func:`bar` instead", "Use `bar` instead"),
("Use :js:func:`bar` instead", "Use `bar` instead"),
# the reference can have special characters:
("Use :func:`~pkg.mod.bar` instead", "Use `~pkg.mod.bar` instead"),
# edge cases:
("Use :r:`` instead", "Use `` instead"),
("Use :d:r:`` instead", "Use `` instead"),
("Use :r:`foo` instead", "Use `foo` instead"),
("Use :d:r:`foo` instead", "Use `foo` instead"),
("Use r:`bad` instead", "Use r:`bad` instead"),
("Use ::`bad` instead", "Use ::`bad` instead"),
("Use :::`bad` instead", "Use :::`bad` instead"),
],
)
def test_get_deprecated_msg(reason, expected):
adapter = deprecated.sphinx.SphinxAdapter("deprecated", reason=reason, version="1")
actual = adapter.get_deprecated_msg(lambda: None, None)
assert expected in actual

0 comments on commit fe04e81

Please sign in to comment.