test_getattr.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from importlib import import_module
  2. from pkgutil import walk_packages
  3. import matplotlib
  4. import pytest
  5. # Get the names of all matplotlib submodules,
  6. # except for the unit tests and private modules.
  7. module_names = [
  8. m.name
  9. for m in walk_packages(
  10. path=matplotlib.__path__, prefix=f'{matplotlib.__name__}.'
  11. )
  12. if not m.name.startswith(__package__)
  13. and not any(x.startswith('_') for x in m.name.split('.'))
  14. ]
  15. @pytest.mark.parametrize('module_name', module_names)
  16. @pytest.mark.filterwarnings('ignore::DeprecationWarning')
  17. @pytest.mark.filterwarnings('ignore::ImportWarning')
  18. def test_getattr(module_name):
  19. """
  20. Test that __getattr__ methods raise AttributeError for unknown keys.
  21. See #20822, #20855.
  22. """
  23. try:
  24. module = import_module(module_name)
  25. except (ImportError, RuntimeError) as e:
  26. # Skip modules that cannot be imported due to missing dependencies
  27. pytest.skip(f'Cannot import {module_name} due to {e}')
  28. key = 'THIS_SYMBOL_SHOULD_NOT_EXIST'
  29. if hasattr(module, key):
  30. delattr(module, key)