1
1
from importlib import import_module
2
2
from pkgutil import walk_packages
3
+ import sys
4
+ import warnings
3
5
4
- import matplotlib
5
6
import pytest
6
7
8
+ import matplotlib
9
+ from matplotlib .testing import is_ci_environment , subprocess_run_helper
10
+
7
11
# Get the names of all matplotlib submodules,
8
12
# except for the unit tests and private modules.
9
- module_names = [
10
- m .name
11
- for m in walk_packages (
12
- path = matplotlib .__path__ , prefix = f'{ matplotlib .__name__ } .'
13
- )
14
- if not m .name .startswith (__package__ )
15
- and not any (x .startswith ('_' ) for x in m .name .split ('.' ))
16
- ]
13
+ module_names = []
14
+ backend_module_names = []
15
+ for m in walk_packages (path = matplotlib .__path__ , prefix = f'{ matplotlib .__name__ } .' ):
16
+ if m .name .startswith (__package__ ):
17
+ continue
18
+ if any (x .startswith ('_' ) for x in m .name .split ('.' )):
19
+ continue
20
+ if 'backends.backend_' in m .name :
21
+ backend_module_names .append (m .name )
22
+ else :
23
+ module_names .append (m .name )
17
24
18
25
19
- @pytest .mark .parametrize ('module_name' , module_names )
20
- @pytest .mark .filterwarnings ('ignore::DeprecationWarning' )
21
- @pytest .mark .filterwarnings ('ignore::ImportWarning' )
22
- def test_getattr (module_name ):
26
+ def _test_getattr (module_name , use_pytest = True ):
23
27
"""
24
28
Test that __getattr__ methods raise AttributeError for unknown keys.
25
29
See #20822, #20855.
@@ -28,8 +32,35 @@ def test_getattr(module_name):
28
32
module = import_module (module_name )
29
33
except (ImportError , RuntimeError , OSError ) as e :
30
34
# Skip modules that cannot be imported due to missing dependencies
31
- pytest .skip (f'Cannot import { module_name } due to { e } ' )
35
+ if use_pytest :
36
+ pytest .skip (f'Cannot import { module_name } due to { e } ' )
37
+ else :
38
+ print (f'SKIP: Cannot import { module_name } due to { e } ' )
39
+ return
32
40
33
41
key = 'THIS_SYMBOL_SHOULD_NOT_EXIST'
34
42
if hasattr (module , key ):
35
43
delattr (module , key )
44
+
45
+
46
+ @pytest .mark .parametrize ('module_name' , module_names )
47
+ @pytest .mark .filterwarnings ('ignore::DeprecationWarning' )
48
+ @pytest .mark .filterwarnings ('ignore::ImportWarning' )
49
+ def test_getattr (module_name ):
50
+ _test_getattr (module_name )
51
+
52
+
53
+ def _test_module_getattr ():
54
+ warnings .filterwarnings ('ignore' , category = DeprecationWarning )
55
+ warnings .filterwarnings ('ignore' , category = ImportWarning )
56
+ module_name = sys .argv [1 ]
57
+ _test_getattr (module_name , use_pytest = False )
58
+
59
+
60
+ @pytest .mark .parametrize ('module_name' , backend_module_names )
61
+ def test_backend_getattr (module_name ):
62
+ proc = subprocess_run_helper (_test_module_getattr , module_name ,
63
+ timeout = 120 if is_ci_environment () else 20 )
64
+ if 'SKIP: ' in proc .stdout :
65
+ pytest .skip (proc .stdout .removeprefix ('SKIP: ' ))
66
+ print (proc .stdout )
0 commit comments