Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ae9d51

Browse files
committed
ENH: include property name in artist AttributeError
1 parent c4510ca commit 4ae9d51

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Exception handling control
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The exception raised when an invalid keyword parameter is passed now includes
5+
that parameter name as the exception's ``name`` property. This provides more
6+
control for exception handling:
7+
8+
9+
.. code-block:: python
10+
11+
import matplotlib.pyplot as plt
12+
13+
def wobbly_plot(args, **kwargs):
14+
w = kwargs.pop('wobble_factor', None)
15+
16+
try:
17+
plt.plot(args, **kwargs)
18+
except AttributeError as e:
19+
raise AttributeError(f'wobbly_plot does not take parameter {e.name}') from e
20+
21+
22+
wobbly_plot([0, 1], wibble_factor=5)
23+
24+
.. code-block::
25+
26+
AttributeError: wobbly_plot does not take parameter wibble_factor

lib/matplotlib/artist.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,8 @@ def _update_props(self, props, errfmt):
11901190
Helper for `.Artist.set` and `.Artist.update`.
11911191
11921192
*errfmt* is used to generate error messages for invalid property
1193-
names; it gets formatted with ``type(self)`` and the property name.
1193+
names; it gets formatted with ``type(self)`` for "{cls}" and the
1194+
property name for "{prop_name}".
11941195
"""
11951196
ret = []
11961197
with cbook._setattr_cm(self, eventson=False):
@@ -1203,7 +1204,8 @@ def _update_props(self, props, errfmt):
12031204
func = getattr(self, f"set_{k}", None)
12041205
if not callable(func):
12051206
raise AttributeError(
1206-
errfmt.format(cls=type(self), prop_name=k))
1207+
errfmt.format(cls=type(self), prop_name=k),
1208+
name=k)
12071209
ret.append(func(v))
12081210
if ret:
12091211
self.pchanged()

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,8 +1501,9 @@ def test_calling_conventions(self):
15011501
ax.voxels(x, y)
15021502
# x, y, z are positional only - this passes them on as attributes of
15031503
# Poly3DCollection
1504-
with pytest.raises(AttributeError):
1504+
with pytest.raises(AttributeError, match="keyword argument 'x'") as exec_info:
15051505
ax.voxels(filled=filled, x=x, y=y, z=z)
1506+
assert exec_info.value.name == 'x'
15061507

15071508

15081509
def test_line3d_set_get_data_3d():

0 commit comments

Comments
 (0)