Skip to content

Commit

Permalink
fixed deprecat.classic test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhajharia committed Jan 18, 2024
1 parent f8c7501 commit f8ae390
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion deprecat/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_deprecat_on_function(a, b):

test_deprecat_on_function(1,2)

@deprecat(deprecated_args={'a':{'version':'4.0','reason':'nothing'}})
@deprecat(deprecated_args={'a':{'version':'4.0','reason':'nothing', 'remove_version': '5.0'}})
def test_deprecated_args(a, b, c=3, d=4):
"""
Here we test deprecation on a function with arguments.
Expand Down
7 changes: 3 additions & 4 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding: utf-8
import pkg_resources

from packaging import version
import deprecat


Expand All @@ -13,6 +12,6 @@ def test_deprecat_has_docstring():
def test_deprecat_has_version():
# The deprecat package must have a valid version number
assert deprecat.__version__ is not None
version = pkg_resources.parse_version(deprecat.__version__)
version_ = version.parse(deprecat.__version__)

assert 'Legacy' not in version.__class__.__name__
assert 'Legacy' not in version_.__class__.__name__
33 changes: 29 additions & 4 deletions tests/test_deprecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class MyDeprecationWarning(DeprecationWarning):
((), {'reason': 'Good reason'}),
((), {'version': '1.2.3'}),
((), {'action': 'once'}),
((), {'category': MyDeprecationWarning}),
((), {'category': MyDeprecationWarning},),
((), {'remove_version': '1.2.4'}),
]


Expand Down Expand Up @@ -223,6 +224,18 @@ def foo():
warn = warns[0]
assert version in str(warn.message)

def test_warning_msg_has_remove_version():
version = "1.2.3"
remove_version="1.2.4"

@deprecat.classic.deprecat(remove_version=remove_version)
def foo():
pass

with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert remove_version in str(warn.message)

def test_warning_is_ignored():
@deprecat.classic.deprecat(action='ignore')
Expand Down Expand Up @@ -266,7 +279,19 @@ def foo(a,b):
with warnings.catch_warnings(record=True) as warns:
foo(a=2,b=3)
warn = warns[0]
assert 'Call to deprecated Parameter a. (nothing) -- Deprecated since v4.0.' in str(warn.message)
assert "Call to deprecated Parameter a. nothing\n-- Deprecated since v4.0." in str(warn.message)

def test_deprecated_arg_warn_function_remove_version():
@deprecat.classic.deprecat(deprecated_args={'a':{'version': '1.5', 'remove_version':'4.0', 'reason':'nothing'}})
def foo(a,b):
pass

with warnings.catch_warnings(record=True) as warns:
foo(a=2,b=3)
warn = warns[0]
print(warn.message)
assert "Call to deprecated Parameter a. nothing\n-- Deprecated since v1.5.\n-- Will be removed in version 4.0." in str(warn.message)


def test_deprecated_arg_warn_class_method():

Expand All @@ -280,7 +305,7 @@ def foo5(cls, a, b):
Foo5.foo5(a=3, b=4)

warn = warns[0]
assert 'Call to deprecated Parameter a. (nothing) -- Deprecated since v4.0.' in str(warn.message)
assert 'Call to deprecated Parameter a. nothing\n-- Deprecated since v4.0.' in str(warn.message)

def test_deprecated_arg_warn_class_init():

Expand All @@ -295,4 +320,4 @@ def __init__(self,a,b):
foo_cls(a=3,b=4)

warn = warns[0]
assert 'Call to deprecated Parameter a. (nothing) -- Deprecated since v4.0.' in str(warn.message)
assert 'Call to deprecated Parameter a. nothing\n-- Deprecated since v4.0.' in str(warn.message)
31 changes: 17 additions & 14 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@ def sphinx_directive(directive):

# noinspection PyShadowingNames
@pytest.mark.parametrize(
"reason, version, expected",
"reason, version, remove_version, expected",
[
(
'A good reason',
'1.2.0',
'1.3.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
{reason}
"""
"\n.. {directive}:: {version}\n {reason}\n\n Warning: This deprecated feature will be removed in version\n {remove_version}\n"
),
),
(
None,
'1.2.0',
None,
textwrap.dedent(
"""\
.. {directive}:: {version}
Expand All @@ -65,7 +64,7 @@ def sphinx_directive(directive):
],
ids=["reason&version", "version"],
)
def test_has_sphinx_docstring(docstring, directive, sphinx_directive, reason, version, expected):
def test_has_sphinx_docstring(docstring, directive, sphinx_directive, reason, version, remove_version, expected):
# The function:
def foo(x, y):
return x + y
Expand All @@ -79,7 +78,7 @@ def foo(x, y):
foo = decorator(foo)

# The function must contain this Sphinx docstring:
expected = expected.format(directive=sphinx_directive, version=version, reason=reason)
expected = expected.format(directive=sphinx_directive, version=version, reason=reason, remove_version=remove_version)

current = textwrap.dedent(foo.__doc__)
assert current.endswith(expected)
Expand Down Expand Up @@ -108,21 +107,25 @@ def foo(x, y):
sys.version_info < (3, 3), reason="Classes should have mutable docstrings -- resolved in python 3.3"
)
@pytest.mark.parametrize(
"reason, version, expected",
"reason, version, remove_version, expected",
[
(
'A good reason',
'1.2.0',
'1.3.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
{reason}
{reason}
Warning: This deprecated feature will be removed in version {remove_version}
"""
),
),
(
None,
'1.2.0',
None,
textwrap.dedent(
"""\
.. {directive}:: {version}
Expand All @@ -132,7 +135,7 @@ def foo(x, y):
],
ids=["reason&version", "version"],
)
def test_cls_has_sphinx_docstring(docstring, directive, sphinx_directive, reason, version, expected):
def test_cls_has_sphinx_docstring(docstring, directive, sphinx_directive, reason, version, remove_version, expected):
# The class:
class Foo(object):
pass
Expand All @@ -146,7 +149,7 @@ class Foo(object):
Foo = decorator(Foo)

# The class must contain this Sphinx docstring:
expected = expected.format(directive=sphinx_directive, version=version, reason=reason)
expected = expected.format(directive=sphinx_directive, version=version, remove_version=remove_version, reason=reason)

current = textwrap.dedent(Foo.__doc__)
assert current.endswith(expected)
Expand Down Expand Up @@ -372,7 +375,7 @@ def __init__(self,a,b):
assert 'Call to deprecated Parameter a. (nothing) -- Deprecated since v4.0.' in str(warn.message)

def test_deprecated_arg_warn_function_docstring():
@deprecat.sphinx.deprecat(deprecated_args={'a':{'version':'4.0','reason':'nothing'}})
@deprecat.sphinx.deprecat(deprecated_args={'a':{'version':'4.0','reason':'nothing', 'remove_version': '5.0'}})
def foo(x,a,b):
"""
Parameters
Expand All @@ -386,7 +389,7 @@ def foo(x,a,b):
"""
pass

expected_new_docstring = "\nParameters\n----------\nx : int \n [description]\na : int\n [description]\n\n .. admonition:: Deprecated\n :class: warning\n\n Parameter a deprecated since 4.0\n\nb : int\n [description]\n\n"
expected_new_docstring = "\nParameters\n----------\nx : int \n [description]\na : int\n [description]\n\n .. admonition:: Deprecated\n :class: warning\n\n Parameter a deprecated since 4.0 and will be removed in version 5.0.\n\nb : int\n [description]\n\n"
assert foo.__doc__ == expected_new_docstring

def test_deprecated_arg_warn_class_method_docstring():
Expand Down Expand Up @@ -489,4 +492,4 @@ def foo():
def test_get_deprecat_msg(reason, expected):
adapter = deprecat.sphinx.SphinxAdapter("deprecated", reason=reason, version="1")
actual = adapter.get_deprecated_msg(lambda: None, None, None)
assert expected in list(actual.values())[0]
assert expected in list(actual.values())[0]

0 comments on commit f8ae390

Please sign in to comment.