Skip to content

Deprecate hatch patterns with invalid values #17926

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 2 commits into from
Jul 17, 2020
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
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/17926-ES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Invalid hatch pattern characters are no longer ignored
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When specifying hatching patterns, characters that are not recognized will
raise a DeprecationWarning. In the future, this will become a hard error.
8 changes: 5 additions & 3 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import matplotlib as mpl
from . import (_path, artist, cbook, cm, colors as mcolors, docstring,
lines as mlines, path as mpath, transforms)
hatch as mhatch, lines as mlines, path as mpath, transforms)
import warnings


Expand Down Expand Up @@ -141,8 +141,8 @@ def __init__(self,
hatch : str, optional
Hatching pattern to use in filled paths, if any. Valid strings are
['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']. See
:doc:`/gallery/shapes_and_collections/hatch_demo` for the meaning
of each hatch type.
:doc:`/gallery/shapes_and_collections/hatch_style_reference` for
the meaning of each hatch type.
pickradius : float, default: 5.0
If ``pickradius <= 0``, then `.Collection.contains` will return
``True`` whenever the test point is inside of one of the polygons
Expand Down Expand Up @@ -522,6 +522,8 @@ def set_hatch(self, hatch):
----------
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
"""
# Use validate_hatch(list) after deprecation.
mhatch._validate_hatch_pattern(hatch)
self._hatch = hatch
self.stale = True

Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Contains classes for generating hatch patterns."""

import numpy as np

from matplotlib import cbook
from matplotlib.path import Path


Expand Down Expand Up @@ -181,6 +183,22 @@ def __init__(self, hatch, density):
]


def _validate_hatch_pattern(hatch):
valid_hatch_patterns = set(r'-+|/\xXoO.*')
if hatch is not None:
invalids = set(hatch).difference(valid_hatch_patterns)
if invalids:
valid = ''.join(sorted(valid_hatch_patterns))
invalids = ''.join(sorted(invalids))
cbook.warn_deprecated(
'3.4',
message=f'hatch must consist of a string of "{valid}" or '
'None, but found the following invalid values '
f'"{invalids}". Passing invalid values is deprecated '
'since %(since)s and will become an error %(removal)s.'
)


def get_path(hatchpattern, density=6):
"""
Given a hatch specifier, *hatchpattern*, generates Path to render
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import numpy as np

import matplotlib as mpl
from . import artist, cbook, colors, docstring, lines as mlines, transforms
from . import (artist, cbook, colors, docstring, hatch as mhatch,
lines as mlines, transforms)
from .bezier import (
NonIntersectingPathException, get_cos_sin, get_intersection,
get_parallels, inside_circle, make_wedged_bezier2,
Expand Down Expand Up @@ -513,6 +514,8 @@ def set_hatch(self, hatch):
----------
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
"""
# Use validate_hatch(list) after deprecation.
mhatch._validate_hatch_pattern(hatch)
self._hatch = hatch
self.stale = True

Expand Down