11from importlib import import_module
22from pkgutil import walk_packages
3+ import sys
4+ import warnings
35
4- import matplotlib
56import pytest
67
8+ import matplotlib
9+ from matplotlib .testing import is_ci_environment , subprocess_run_helper
10+
711# Get the names of all matplotlib submodules,
812# 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 )
1724
1825
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 ):
2327 """
2428 Test that __getattr__ methods raise AttributeError for unknown keys.
2529 See #20822, #20855.
@@ -28,8 +32,35 @@ def test_getattr(module_name):
2832 module = import_module (module_name )
2933 except (ImportError , RuntimeError , OSError ) as e :
3034 # 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
3240
3341 key = 'THIS_SYMBOL_SHOULD_NOT_EXIST'
3442 if hasattr (module , key ):
3543 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