Skip to content

Move cbook._check_isinstance() to _api.check_isinstance() #19244

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
Jan 11, 2021
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
40 changes: 40 additions & 0 deletions lib/matplotlib/_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ def fget(self):
return self._fget


# In the following check_foo() functions, the first parameter starts with an
# underscore because it is intended to be positional-only (e.g., so that
# `_api.check_isinstance([...], types=foo)` doesn't fail.

def check_isinstance(_types, **kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is an instance
of one of *_types*; if not, raise an appropriate TypeError.

As a special case, a ``None`` entry in *_types* is treated as NoneType.

Examples
--------
>>> _api.check_isinstance((SomeClass, None), arg=arg)
"""
types = _types
none_type = type(None)
types = ((types,) if isinstance(types, type) else
(none_type,) if types is None else
tuple(none_type if tp is None else tp for tp in types))

def type_name(tp):
return ("None" if tp is none_type
else tp.__qualname__ if tp.__module__ == "builtins"
else f"{tp.__module__}.{tp.__qualname__}")

for k, v in kwargs.items():
if not isinstance(v, types):
names = [*map(type_name, types)]
if "None" in names: # Move it to the end for better wording.
names.remove("None")
names.append("None")
raise TypeError(
"{!r} must be an instance of {}, not a {}".format(
k,
", ".join(names[:-1]) + " or " + names[-1]
if len(names) > 1 else names[0],
type_name(type(v))))


def check_in_list(_values, *, _print_supported_values=True, **kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is in *_values*.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ def sharex(self, other):
axes, and cannot be used if the x-axis is already being shared with
another axes.
"""
cbook._check_isinstance(_AxesBase, other=other)
_api.check_isinstance(_AxesBase, other=other)
if self._sharex is not None and other is not self._sharex:
raise ValueError("x-axis is already shared")
self._shared_x_axes.join(self, other)
Expand All @@ -1094,7 +1094,7 @@ def sharey(self, other):
axes, and cannot be used if the y-axis is already being shared with
another axes.
"""
cbook._check_isinstance(_AxesBase, other=other)
_api.check_isinstance(_AxesBase, other=other)
if self._sharey is not None and other is not self._sharey:
raise ValueError("y-axis is already shared")
self._shared_y_axes.join(self, other)
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ def _set_formatter(self, formatter, level):
not isinstance(formatter, mticker.TickHelper)):
formatter = mticker.FuncFormatter(formatter)
else:
cbook._check_isinstance(mticker.Formatter, formatter=formatter)
_api.check_isinstance(mticker.Formatter, formatter=formatter)

if (isinstance(formatter, mticker.FixedFormatter)
and len(formatter.seq) > 0
Expand All @@ -1624,7 +1624,7 @@ def set_major_locator(self, locator):
----------
locator : `~matplotlib.ticker.Locator`
"""
cbook._check_isinstance(mticker.Locator, locator=locator)
_api.check_isinstance(mticker.Locator, locator=locator)
self.isDefault_majloc = False
self.major.locator = locator
if self.major.formatter:
Expand All @@ -1640,7 +1640,7 @@ def set_minor_locator(self, locator):
----------
locator : `~matplotlib.ticker.Locator`
"""
cbook._check_isinstance(mticker.Locator, locator=locator)
_api.check_isinstance(mticker.Locator, locator=locator)
self.isDefault_minloc = False
self.minor.locator = locator
if self.minor.formatter:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def set_clip_rectangle(self, rectangle):

def set_clip_path(self, path):
"""Set the clip path to a `.TransformedPath` or None."""
cbook._check_isinstance((transforms.TransformedPath, None), path=path)
_api.check_isinstance((transforms.TransformedPath, None), path=path)
self._clippath = path

def set_dashes(self, dash_offset, dash_list):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/blocking_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import logging
from numbers import Integral

from matplotlib import cbook
from matplotlib import _api
from matplotlib.backend_bases import MouseButton
import matplotlib.lines as mlines

Expand Down Expand Up @@ -78,7 +78,7 @@ def pop_event(self, index=-1):

def __call__(self, n=1, timeout=30):
"""Blocking call to retrieve *n* events."""
cbook._check_isinstance(Integral, n=n)
_api.check_isinstance(Integral, n=n)
self.n = n
self.events = []

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np

from matplotlib import cbook, ticker, units
from matplotlib import _api, ticker, units


_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -217,7 +217,7 @@ def update(self, data):
convertible = True
for val in OrderedDict.fromkeys(data):
# OrderedDict just iterates over unique values in data.
cbook._check_isinstance((str, bytes), value=val)
_api.check_isinstance((str, bytes), value=val)
if convertible:
# this will only be called so long as convertible is True.
convertible = self._str_is_convertible(val)
Expand Down
41 changes: 0 additions & 41 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,47 +2242,6 @@ def _check_and_log_subprocess(command, logger, **kwargs):
return proc.stdout


# In the following _check_foo functions, the first parameter starts with an
# underscore because it is intended to be positional-only (e.g., so that
# `_check_isinstance([...], types=foo)` doesn't fail.


def _check_isinstance(_types, **kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is an instance
of one of *_types*; if not, raise an appropriate TypeError.

As a special case, a ``None`` entry in *_types* is treated as NoneType.

Examples
--------
>>> cbook._check_isinstance((SomeClass, None), arg=arg)
"""
types = _types
none_type = type(None)
types = ((types,) if isinstance(types, type) else
(none_type,) if types is None else
tuple(none_type if tp is None else tp for tp in types))

def type_name(tp):
return ("None" if tp is none_type
else tp.__qualname__ if tp.__module__ == "builtins"
else f"{tp.__module__}.{tp.__qualname__}")

for k, v in kwargs.items():
if not isinstance(v, types):
names = [*map(type_name, types)]
if "None" in names: # Move it to the end for better wording.
names.remove("None")
names.append("None")
raise TypeError(
"{!r} must be an instance of {}, not a {}".format(
k,
", ".join(names[:-1]) + " or " + names[-1]
if len(names) > 1 else names[0],
type_name(type(v))))


def _backend_module_name(name):
"""
Convert a backend name (either a standard backend -- "Agg", "TkAgg", ... --
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
the registered colormap will be immutable.

"""
cbook._check_isinstance((str, None), name=name)
_api.check_isinstance((str, None), name=name)
if name is None:
try:
name = cmap.name
Expand Down Expand Up @@ -448,7 +448,7 @@ def set_norm(self, norm):
the norm of the mappable will reset the norm, locator, and formatters
on the colorbar to default.
"""
cbook._check_isinstance((colors.Normalize, None), norm=norm)
_api.check_isinstance((colors.Normalize, None), norm=norm)
in_init = self.norm is None
if norm is None:
norm = colors.Normalize()
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import numpy as np

import matplotlib as mpl
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
from matplotlib import _api, collections, cm, colors, contour, ticker
import matplotlib.artist as martist
import matplotlib.patches as mpatches
import matplotlib.path as mpath
Expand Down Expand Up @@ -429,7 +429,7 @@ def __init__(self, ax, cmap=None,
extendrect=False,
label='',
):
cbook._check_isinstance([colors.Colormap, None], cmap=cmap)
_api.check_isinstance([colors.Colormap, None], cmap=cmap)
_api.check_in_list(
['vertical', 'horizontal'], orientation=orientation)
_api.check_in_list(
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ class DviFont:
__slots__ = ('texname', 'size', 'widths', '_scale', '_vf', '_tfm')

def __init__(self, scale, tfm, texname, vf):
cbook._check_isinstance(bytes, texname=texname)
_api.check_isinstance(bytes, texname=texname)
self._scale = scale
self._tfm = tfm
self.texname = texname
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def add(self, key, a):
"""
# All the error checking may be unnecessary; but this method
# is called so seldom that the overhead is negligible.
cbook._check_isinstance(Axes, a=a)
_api.check_isinstance(Axes, a=a)
try:
hash(key)
except TypeError:
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def validate_color(s):


def _validate_cmap(s):
cbook._check_isinstance((str, Colormap), cmap=s)
_api.check_isinstance((str, Colormap), cmap=s)
return s


Expand Down Expand Up @@ -770,7 +770,7 @@ def validate_hatch(s):
"""
if not isinstance(s, str):
raise ValueError("Hatch pattern must be a string")
cbook._check_isinstance(str, hatch_pattern=s)
_api.check_isinstance(str, hatch_pattern=s)
unknown = set(s) - {'\\', '/', '|', '-', '+', '*', '.', 'x', 'o', 'O'}
if unknown:
raise ValueError("Unknown hatch symbol(s): %s" % list(unknown))
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

import matplotlib
from matplotlib import _api, cbook, docstring, rcParams
from matplotlib import _api, docstring, rcParams
from matplotlib.artist import allow_rasterization
import matplotlib.transforms as mtransforms
import matplotlib.patches as mpatches
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(self, axes, spine_type, path, **kwargs):
# non-rectangular axes is currently implemented, and this lets
# them pass through the spines machinery without errors.)
self._position = None
cbook._check_isinstance(matplotlib.path.Path, path=path)
_api.check_isinstance(matplotlib.path.Path, path=path)
self._path = path

# To support drawing both linear and circular spines, this
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Thanks to John Gill for providing the class and table.
"""

from . import artist, cbook, docstring
from . import _api, artist, docstring
from .artist import Artist, allow_rasterization
from .patches import Rectangle
from .text import Text
Expand Down Expand Up @@ -342,7 +342,7 @@ def __setitem__(self, position, cell):
"""
Set a custom cell in a given position.
"""
cbook._check_isinstance(Cell, cell=cell)
_api.check_isinstance(Cell, cell=cell)
try:
row, col = position[0], position[1]
except Exception as err:
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import numpy as np
from numpy.linalg import inv

from matplotlib import _api, cbook
from matplotlib import _api
from matplotlib._path import (
affine_transform, count_bboxes_overlapping_bbox, update_path_extents)
from .path import Path
Expand Down Expand Up @@ -1054,7 +1054,7 @@ def __init__(self, bbox, transform, **kwargs):
"""
if not bbox.is_bbox:
raise ValueError("'bbox' is not a bbox")
cbook._check_isinstance(Transform, transform=transform)
_api.check_isinstance(Transform, transform=transform)
if transform.input_dims != 2 or transform.output_dims != 2:
raise ValueError(
"The input and output dimensions of 'transform' must be 2")
Expand Down Expand Up @@ -1660,7 +1660,7 @@ def __init__(self, child):
*child*: A `Transform` instance. This child may later
be replaced with :meth:`set`.
"""
cbook._check_isinstance(Transform, child=child)
_api.check_isinstance(Transform, child=child)
self._init(child)
self.set_children(child)

Expand Down Expand Up @@ -1911,7 +1911,7 @@ def set(self, other):
Set this transformation from the frozen copy of another
`Affine2DBase` object.
"""
cbook._check_isinstance(Affine2DBase, other=other)
_api.check_isinstance(Affine2DBase, other=other)
self._mtx = other.get_matrix()
self.invalidate()

Expand Down Expand Up @@ -2686,7 +2686,7 @@ def __init__(self, path, transform):
path : `~.path.Path`
transform : `Transform`
"""
cbook._check_isinstance(Transform, transform=transform)
_api.check_isinstance(Transform, transform=transform)
super().__init__()
self._path = path
self._transform = transform
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tri/trifinder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from matplotlib import cbook
from matplotlib import _api
from matplotlib.tri import Triangulation


Expand All @@ -17,7 +17,7 @@ class TriFinder:
"""

def __init__(self, triangulation):
cbook._check_isinstance(Triangulation, triangulation=triangulation)
_api.check_isinstance(Triangulation, triangulation=triangulation)
self._triangulation = triangulation


Expand Down
9 changes: 4 additions & 5 deletions lib/matplotlib/tri/triinterpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from matplotlib import _api, cbook
from matplotlib import _api
from matplotlib.tri import Triangulation
from matplotlib.tri.trifinder import TriFinder
from matplotlib.tri.tritools import TriAnalyzer
Expand All @@ -31,15 +31,15 @@ class TriInterpolator:
"""

def __init__(self, triangulation, z, trifinder=None):
cbook._check_isinstance(Triangulation, triangulation=triangulation)
_api.check_isinstance(Triangulation, triangulation=triangulation)
self._triangulation = triangulation

self._z = np.asarray(z)
if self._z.shape != self._triangulation.x.shape:
raise ValueError("z array must have same length as triangulation x"
" and y arrays")

cbook._check_isinstance((TriFinder, None), trifinder=trifinder)
_api.check_isinstance((TriFinder, None), trifinder=trifinder)
self._trifinder = trifinder or self._triangulation.get_trifinder()

# Default scaling factors : 1.0 (= no scaling)
Expand Down Expand Up @@ -996,8 +996,7 @@ class _DOF_estimator:
gradient coordinates.
"""
def __init__(self, interpolator, **kwargs):
cbook._check_isinstance(
CubicTriInterpolator, interpolator=interpolator)
_api.check_isinstance(CubicTriInterpolator, interpolator=interpolator)
self._pts = interpolator._pts
self._tris_pts = interpolator._tris_pts
self.z = interpolator._z
Expand Down
Loading