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