From fe04e814a0a44497b3022ca6c359256cc29e3571 Mon Sep 17 00:00:00 2001 From: Laurent LAPORTE Date: Sat, 13 Mar 2021 11:58:18 +0100 Subject: [PATCH] Change in :meth:`~deprecated.sphinx.SphinxAdapter.get_deprecated_msg`: correct the regex used to get rid of the Sphinx cross-referencing syntax (issue #41). --- deprecated/sphinx.py | 4 +++- tests/test_sphinx.py | 57 ++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/deprecated/sphinx.py b/deprecated/sphinx.py index f238f49..18cfa85 100644 --- a/deprecated/sphinx.py +++ b/deprecated/sphinx.py @@ -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 diff --git a/tests/test_sphinx.py b/tests/test_sphinx.py index e6749a7..7a227b6 100644 --- a/tests/test_sphinx.py +++ b/tests/test_sphinx.py @@ -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 @@ -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