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

Skip to content
15 changes: 15 additions & 0 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,21 @@ def register_scale(scale_class):
)


def deregister_scale(name):
"""
Remove a custom scale from the registry.

Parameters
----------
name : str
The name of the scale to remove.
"""
if name not in _scale_mapping:
raise ValueError(f"Scale '{name}' is not registered.")
_scale_mapping.pop(name)
_scale_has_axis_parameter.pop(name, None)


def _get_scale_docs():
"""
Helper function for generating docstrings related to scales.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/scale.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,5 @@ class LogitScale(ScaleBase):
def get_scale_names() -> list[str]: ...
def scale_factory(scale: str, axis: Axis, **kwargs) -> ScaleBase: ...
def register_scale(scale_class: type[ScaleBase]) -> None: ...
def deregister_scale(name: str) -> None: ...
def _make_axis_parameter_optional(init_func: Callable[..., None]) -> Callable[..., None]: ...
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,23 @@ def test_val_in_range_base_fallback():
assert s.val_in_range(np.nan) is False
assert s.val_in_range(np.inf) is False
assert s.val_in_range(-np.inf) is False


def test_unregister_scale():
"""Test that unregister_scale removes a scale correctly."""
# Register a temporary custom scale
class TempScale(mscale.LinearScale):
name = 'temp_test_scale'

mscale.register_scale(TempScale)
assert 'temp_test_scale' in mscale._scale_mapping

# Now unregister it
mscale.deregister_scale('temp_test_scale')
assert 'temp_test_scale' not in mscale._scale_mapping


def test_unregister_scale_invalid():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

deregister

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @story645!

I've updated the function and test names to use deregister_scale. Please taek a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

still didn't catch everything,

"""Test that unregister_scale raises ValueError for unknown scale."""
with pytest.raises(ValueError, match="not registered"):
mscale.deregister_scale('this_does_not_exist')
Loading