From 79a0a33a129f0bc97aa371d9368e07c0fba13e7b Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Wed, 18 Aug 2021 12:10:23 -0400 Subject: [PATCH] TST: Add unit test to catch recurrences of #20822, #20855 --- lib/matplotlib/tests/test_getattr.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/matplotlib/tests/test_getattr.py diff --git a/lib/matplotlib/tests/test_getattr.py b/lib/matplotlib/tests/test_getattr.py new file mode 100644 index 000000000000..71556b5b4c71 --- /dev/null +++ b/lib/matplotlib/tests/test_getattr.py @@ -0,0 +1,28 @@ +from importlib import import_module +from pkgutil import walk_packages + +import matplotlib +import pytest + +# Get the names of all matplotlib submodules, except for the unit tests. +module_names = [m.name for m in walk_packages(path=matplotlib.__path__, + prefix=f'{matplotlib.__name__}.') + if not m.name.startswith(__package__)] + + +@pytest.mark.parametrize('module_name', module_names) +@pytest.mark.filterwarnings('ignore::DeprecationWarning') +def test_getattr(module_name): + """ + Test that __getattr__ methods raise AttributeError for unknown keys. + See #20822, #20855. + """ + try: + module = import_module(module_name) + except (ImportError, RuntimeError) as e: + # Skip modules that cannot be imported due to missing dependencies + pytest.skip(f'Cannot import {module_name} due to {e}') + + key = 'THIS_SYMBOL_SHOULD_NOT_EXIST' + if hasattr(module, key): + delattr(module, key)