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

Skip to content

Commit f1ce720

Browse files
committed
Add suggestions to ProjectionRegistry lookup errors
1 parent 6cdfe15 commit f1ce720

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/matplotlib/projections/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
`matplotlib.projections.polar` may also be of interest.
5353
"""
5454

55-
from .. import axes, _docstring
55+
from .. import _api, axes, _docstring
5656
from .geo import AitoffAxes, HammerAxes, LambertAxes, MollweideAxes
5757
from .polar import PolarAxes
5858

@@ -78,9 +78,10 @@ def register(self, *projections):
7878
name = projection.name
7979
self._all_projection_types[name] = projection
8080

81-
def get_projection_class(self, name):
81+
def get_projection_class(self, name, _error_cls=KeyError):
8282
"""Get a projection class from its *name*."""
83-
return self._all_projection_types[name]
83+
return _api.getitem_checked(self._all_projection_types, _error_cls=_error_cls,
84+
projection=name)
8485

8586
def get_projection_names(self):
8687
"""Return the names of all projections currently registered."""
@@ -116,10 +117,7 @@ def get_projection_class(projection=None):
116117
if projection is None:
117118
projection = 'rectilinear'
118119

119-
try:
120-
return projection_registry.get_projection_class(projection)
121-
except KeyError as err:
122-
raise ValueError("Unknown projection %r" % projection) from err
120+
return projection_registry.get_projection_class(projection, _error_cls=ValueError)
123121

124122

125123
get_projection_names = projection_registry.get_projection_names

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,6 +3338,13 @@ def test_scatter_c_facecolor_warning_integration(c, facecolor):
33383338
ax.scatter(x, y, c=c, facecolor=facecolor)
33393339

33403340

3341+
def test_invalid_projection():
3342+
with pytest.raises(ValueError,
3343+
match=r"'aitof' is not a valid value for projection\. "
3344+
r"Did you mean: 'aitoff'\?"):
3345+
plt.subplots(subplot_kw={'projection': 'aitof'})
3346+
3347+
33413348
def test_as_mpl_axes_api():
33423349
# tests the _as_mpl_axes api
33433350
class Polar:

0 commit comments

Comments
 (0)