test_api.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from __future__ import annotations
  2. from collections.abc import Callable
  3. import re
  4. import typing
  5. from typing import Any, TypeVar
  6. import numpy as np
  7. import pytest
  8. import matplotlib as mpl
  9. from matplotlib import _api
  10. if typing.TYPE_CHECKING:
  11. from typing_extensions import Self
  12. T = TypeVar('T')
  13. @pytest.mark.parametrize('target,shape_repr,test_shape',
  14. [((None, ), "(N,)", (1, 3)),
  15. ((None, 3), "(N, 3)", (1,)),
  16. ((None, 3), "(N, 3)", (1, 2)),
  17. ((1, 5), "(1, 5)", (1, 9)),
  18. ((None, 2, None), "(M, 2, N)", (1, 3, 1))
  19. ])
  20. def test_check_shape(target: tuple[int | None, ...],
  21. shape_repr: str,
  22. test_shape: tuple[int, ...]) -> None:
  23. error_pattern = "^" + re.escape(
  24. f"'aardvark' must be {len(target)}D with shape {shape_repr}, but your input "
  25. f"has shape {test_shape}")
  26. data = np.zeros(test_shape)
  27. with pytest.raises(ValueError, match=error_pattern):
  28. _api.check_shape(target, aardvark=data)
  29. def test_classproperty_deprecation() -> None:
  30. class A:
  31. @_api.deprecated("0.0.0")
  32. @_api.classproperty
  33. def f(cls: Self) -> None:
  34. pass
  35. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  36. A.f
  37. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  38. a = A()
  39. a.f
  40. def test_warn_deprecated():
  41. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  42. match=r'foo was deprecated in Matplotlib 3\.10 and will be '
  43. r'removed in 3\.12\.'):
  44. _api.warn_deprecated('3.10', name='foo')
  45. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  46. match=r'The foo class was deprecated in Matplotlib 3\.10 and '
  47. r'will be removed in 3\.12\.'):
  48. _api.warn_deprecated('3.10', name='foo', obj_type='class')
  49. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  50. match=r'foo was deprecated in Matplotlib 3\.10 and will be '
  51. r'removed in 3\.12\. Use bar instead\.'):
  52. _api.warn_deprecated('3.10', name='foo', alternative='bar')
  53. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  54. match=r'foo was deprecated in Matplotlib 3\.10 and will be '
  55. r'removed in 3\.12\. More information\.'):
  56. _api.warn_deprecated('3.10', name='foo', addendum='More information.')
  57. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  58. match=r'foo was deprecated in Matplotlib 3\.10 and will be '
  59. r'removed in 4\.0\.'):
  60. _api.warn_deprecated('3.10', name='foo', removal='4.0')
  61. with pytest.warns(mpl.MatplotlibDeprecationWarning,
  62. match=r'foo was deprecated in Matplotlib 3\.10\.'):
  63. _api.warn_deprecated('3.10', name='foo', removal=False)
  64. with pytest.warns(PendingDeprecationWarning,
  65. match=r'foo will be deprecated in a future version'):
  66. _api.warn_deprecated('3.10', name='foo', pending=True)
  67. with pytest.raises(ValueError, match=r'cannot have a scheduled removal'):
  68. _api.warn_deprecated('3.10', name='foo', pending=True, removal='3.12')
  69. with pytest.warns(mpl.MatplotlibDeprecationWarning, match=r'Complete replacement'):
  70. _api.warn_deprecated('3.10', message='Complete replacement', name='foo',
  71. alternative='bar', addendum='More information.',
  72. obj_type='class', removal='4.0')
  73. def test_deprecate_privatize_attribute() -> None:
  74. class C:
  75. def __init__(self) -> None: self._attr = 1
  76. def _meth(self, arg: T) -> T: return arg
  77. attr: int = _api.deprecate_privatize_attribute("0.0")
  78. meth: Callable = _api.deprecate_privatize_attribute("0.0")
  79. c = C()
  80. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  81. assert c.attr == 1
  82. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  83. c.attr = 2
  84. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  85. assert c.attr == 2
  86. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  87. assert c.meth(42) == 42
  88. def test_delete_parameter() -> None:
  89. @_api.delete_parameter("3.0", "foo")
  90. def func1(foo: Any = None) -> None:
  91. pass
  92. @_api.delete_parameter("3.0", "foo")
  93. def func2(**kwargs: Any) -> None:
  94. pass
  95. for func in [func1, func2]: # type: ignore[list-item]
  96. func() # No warning.
  97. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  98. func(foo="bar")
  99. def pyplot_wrapper(foo: Any = _api.deprecation._deprecated_parameter) -> None:
  100. func1(foo)
  101. pyplot_wrapper() # No warning.
  102. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  103. func(foo="bar")
  104. def test_make_keyword_only() -> None:
  105. @_api.make_keyword_only("3.0", "arg")
  106. def func(pre: Any, arg: Any, post: Any = None) -> None:
  107. pass
  108. func(1, arg=2) # Check that no warning is emitted.
  109. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  110. func(1, 2)
  111. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  112. func(1, 2, 3)
  113. def test_deprecation_alternative() -> None:
  114. alternative = "`.f1`, `f2`, `f3(x) <.f3>` or `f4(x)<f4>`"
  115. @_api.deprecated("1", alternative=alternative)
  116. def f() -> None:
  117. pass
  118. if f.__doc__ is None:
  119. pytest.skip('Documentation is disabled')
  120. assert alternative in f.__doc__
  121. def test_empty_check_in_list() -> None:
  122. with pytest.raises(TypeError, match="No argument to check!"):
  123. _api.check_in_list(["a"])
  124. def test_check_in_list_numpy() -> None:
  125. with pytest.raises(ValueError, match=r"array\(5\) is not a valid value"):
  126. _api.check_in_list(['a', 'b'], value=np.array(5))