Skip to content

ENH: include property name in artist AttributeError #28573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/users/next_whats_new/exception_prop_name.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Exception handling control
~~~~~~~~~~~~~~~~~~~~~~~~~~

The exception raised when an invalid keyword parameter is passed now includes
that parameter name as the exception's ``name`` property. This provides more
control for exception handling:


.. code-block:: python

import matplotlib.pyplot as plt

def wobbly_plot(args, **kwargs):
w = kwargs.pop('wobble_factor', None)

try:
plt.plot(args, **kwargs)
except AttributeError as e:
raise AttributeError(f'wobbly_plot does not take parameter {e.name}') from e


wobbly_plot([0, 1], wibble_factor=5)

.. code-block::

AttributeError: wobbly_plot does not take parameter wibble_factor
6 changes: 4 additions & 2 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,8 @@ def _update_props(self, props, errfmt):
Helper for `.Artist.set` and `.Artist.update`.

*errfmt* is used to generate error messages for invalid property
names; it gets formatted with ``type(self)`` and the property name.
names; it gets formatted with ``type(self)`` for "{cls}" and the
property name for "{prop_name}".
"""
ret = []
with cbook._setattr_cm(self, eventson=False):
Expand All @@ -1203,7 +1204,8 @@ def _update_props(self, props, errfmt):
func = getattr(self, f"set_{k}", None)
if not callable(func):
raise AttributeError(
errfmt.format(cls=type(self), prop_name=k))
errfmt.format(cls=type(self), prop_name=k),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring could mention that formatting happens via named placeholders {cls} and {prop_name}.

name=k)
ret.append(func(v))
if ret:
self.pchanged()
Expand Down
3 changes: 2 additions & 1 deletion lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,8 +1501,9 @@ def test_calling_conventions(self):
ax.voxels(x, y)
# x, y, z are positional only - this passes them on as attributes of
# Poly3DCollection
with pytest.raises(AttributeError):
with pytest.raises(AttributeError, match="keyword argument 'x'") as exec_info:
ax.voxels(filled=filled, x=x, y=y, z=z)
assert exec_info.value.name == 'x'


def test_line3d_set_get_data_3d():
Expand Down
Loading