From a1d8b56882a4dc394ab8c973277c8c89d79078fb Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 9 Nov 2021 01:14:24 -0500 Subject: [PATCH] Raise correct exception out of Spines.__getattr__ If the name does not exist, then `__getattr__` should raise `AttributeError`, not `ValueError`. Fixes #21554 --- lib/matplotlib/spines.py | 2 +- lib/matplotlib/tests/test_spines.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index b07514d324d8..7e7a423c787b 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -550,7 +550,7 @@ def __getattr__(self, name): try: return self._dict[name] except KeyError: - raise ValueError( + raise AttributeError( f"'Spines' object does not contain a '{name}' spine") def __getitem__(self, key): diff --git a/lib/matplotlib/tests/test_spines.py b/lib/matplotlib/tests/test_spines.py index 589badc310d9..b8891aec411e 100644 --- a/lib/matplotlib/tests/test_spines.py +++ b/lib/matplotlib/tests/test_spines.py @@ -35,6 +35,8 @@ def set_val(self, val): spines[:].set_val('y') assert all(spine.val == 'y' for spine in spines.values()) + with pytest.raises(AttributeError, match='foo'): + spines.foo with pytest.raises(KeyError, match='foo'): spines['foo'] with pytest.raises(KeyError, match='foo, bar'):