matplotlib._api#
Helper functions for managing the Matplotlib API.
This documentation is only relevant for Matplotlib developers, not for users.
Warning
This module and its submodules are for internal use only. Do not use them in your own code. We may change the API at any time with no warning.
- matplotlib._api.caching_module_getattr(cls)[source]#
 Helper decorator for implementing module-level
__getattr__as a class.This decorator must be used at the module toplevel as follows:
@caching_module_getattr class __getattr__: # The class *must* be named ``__getattr__``. @property # Only properties are taken into account. def name(self): ...
The
__getattr__class will be replaced by a__getattr__function such that trying to accessnameon the module will resolve the corresponding property (which may be decorated e.g. with_api.deprecatedfor deprecating module globals). The properties are all implicitly cached. Moreover, a suitable AttributeError is generated and raised if no property with the given name exists.
- matplotlib._api.check_getitem(mapping, /, **kwargs)[source]#
 kwargs must consist of a single key, value pair. If key is in mapping, return
mapping[value]; else, raise an appropriate ValueError.Examples
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
- matplotlib._api.check_in_list(values, /, *, _print_supported_values=True, **kwargs)[source]#
 For each key, value pair in kwargs, check that value is in values; if not, raise an appropriate ValueError.
- Parameters:
 - valuesiterable
 Sequence of values to check on.
- _print_supported_valuesbool, default: True
 Whether to print values when raising ValueError.
- **kwargsdict
 key, value pairs as keyword arguments to find in values.
- Raises:
 - ValueError
 If any value in kwargs is not found in values.
Examples
>>> _api.check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
- matplotlib._api.check_isinstance(types, /, **kwargs)[source]#
 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
Noneentry in types is treated as NoneType.Examples
>>> _api.check_isinstance((SomeClass, None), arg=arg)
- matplotlib._api.check_shape(shape, /, **kwargs)[source]#
 For each key, value pair in kwargs, check that value has the shape shape; if not, raise an appropriate ValueError.
None in the shape is treated as a "free" size that can have any length. e.g. (None, 2) -> (N, 2)
The values checked must be numpy arrays.
Examples
To check for (N, 2) shaped arrays
>>> _api.check_shape((None, 2), arg=arg, other_arg=other_arg)
- class matplotlib._api.classproperty(fget, fset=None, fdel=None, doc=None)[source]#
 Bases:
objectLike
property, but also triggers on access via the class, and it is the class that's passed as argument.Examples
class C: @classproperty def foo(cls): return cls.__name__ assert C.foo == "C"
- property fget#
 
- matplotlib._api.define_aliases(alias_d, cls=None)[source]#
 Class decorator for defining property aliases.
Use as
@_api.define_aliases({"property": ["alias", ...], ...}) class C: ...
For each property, if the corresponding
get_propertyis defined in the class so far, an alias namedget_aliaswill be defined; the same will be done for setters. If neither the getter nor the setter exists, an exception will be raised.The alias map is stored as the
_alias_mapattribute on the class and can be used bynormalize_kwargs(which assumes that higher priority aliases come last).
- matplotlib._api.kwarg_error(name, kw)[source]#
 Generate a TypeError to be raised by function calls with wrong kwarg.
- Parameters:
 - namestr
 The name of the calling function.
- kwstr or Iterable[str]
 Either the invalid keyword argument name, or an iterable yielding invalid keyword arguments (e.g., a
kwargsdict).
- matplotlib._api.nargs_error(name, takes, given)[source]#
 Generate a TypeError to be raised by function calls with wrong arity.
- matplotlib._api.recursive_subclasses(cls)[source]#
 Yield cls and direct and indirect subclasses of cls.
- matplotlib._api.select_matching_signature(funcs, *args, **kwargs)[source]#
 Select and call the function that accepts
*args, **kwargs.funcs is a list of functions which should not raise any exception (other than
TypeErrorif the arguments passed do not match their signature).select_matching_signaturetries to call each of the functions in funcs with*args, **kwargs(in the order in which they are given). Calls that fail with aTypeErrorare silently skipped. As soon as a call succeeds,select_matching_signaturereturns its return value. If no function accepts*args, **kwargs, then theTypeErrorraised by the last failing call is re-raised.Callers should normally make sure that any
*args, **kwargscan only bind a single func (to avoid any ambiguity), although this is not checked byselect_matching_signature.Notes
select_matching_signatureis intended to help implementing signature-overloaded functions. In general, such functions should be avoided, except for back-compatibility concerns. A typical use pattern isdef my_func(*args, **kwargs): params = select_matching_signature( [lambda old1, old2: locals(), lambda new: locals()], *args, **kwargs) if "old1" in params: warn_deprecated(...) old1, old2 = params.values() # note that locals() is ordered. else: new, = params.values() # do things with params
which allows my_func to be called either with two parameters (old1 and old2) or a single one (new). Note that the new signature is given last, so that callers get a
TypeErrorcorresponding to the new signature if the arguments they passed in do not match any signature.
- matplotlib._api.warn_external(message, category=None)[source]#
 warnings.warnwrapper that sets stacklevel to "outside Matplotlib".The original emitter of the warning can be obtained by patching this function back to
warnings.warn, i.e._api.warn_external = warnings.warn(orfunctools.partial(warnings.warn, stacklevel=2), etc.).
Helper functions for deprecating parts of the Matplotlib API.
This documentation is only relevant for Matplotlib developers, not for users.
Warning
This module is for internal use only. Do not use it in your own code. We may change the API at any time with no warning.
- exception matplotlib._api.deprecation.MatplotlibDeprecationWarning[source]#
 Bases:
DeprecationWarningA class for issuing deprecation warnings for Matplotlib users.
- matplotlib._api.deprecation.delete_parameter(since, name, func=None, **kwargs)[source]#
 Decorator indicating that parameter name of func is being deprecated.
The actual implementation of func should keep the name parameter in its signature, or accept a
**kwargsargument (through which name would be passed).Parameters that come after the deprecated parameter effectively become keyword-only (as they cannot be passed positionally without triggering the DeprecationWarning on the deprecated parameter), and should be marked as such after the deprecation period has passed and the deprecated parameter is removed.
Parameters other than since, name, and func are keyword-only and forwarded to
warn_deprecated.Examples
@_api.delete_parameter("3.1", "unused") def func(used_arg, other_arg, unused, more_args): ...
- matplotlib._api.deprecation.deprecate_method_override(method, obj, *, allow_empty=False, **kwargs)[source]#
 Return
obj.methodwith a deprecation if it was overridden, else None.- Parameters:
 - method
 An unbound method, i.e. an expression of the form
Class.method_name. Remember that within the body of a method, one can always use__class__to refer to the class that is currently being defined.- obj
 Either an object of the class where method is defined, or a subclass of that class.
- allow_emptybool, default: False
 Whether to allow overrides by "empty" methods without emitting a warning.
- **kwargs
 Additional parameters passed to
warn_deprecatedto generate the deprecation warning; must at least include the "since" key.
- class matplotlib._api.deprecation.deprecate_privatize_attribute(*args, **kwargs)[source]#
 Bases:
objectHelper to deprecate public access to an attribute (or method).
This helper should only be used at class scope, as follows:
class Foo: attr = _deprecate_privatize_attribute(*args, **kwargs)
where all parameters are forwarded to
deprecated. This form makesattra property which forwards read and write access toself._attr(same name but with a leading underscore), with a deprecation warning. Note that the attribute name is derived from the name this helper is assigned to. This helper also works for deprecating methods.
- matplotlib._api.deprecation.deprecated(since, *, message='', name='', alternative='', pending=False, obj_type=None, addendum='', removal='')[source]#
 Decorator to mark a function, a class, or a property as deprecated.
When deprecating a classmethod, a staticmethod, or a property, the
@deprecateddecorator should go under@classmethodand@staticmethod(i.e.,deprecatedshould directly decorate the underlying callable), but over@property.When deprecating a class
Cintended to be used as a base class in a multiple inheritance hierarchy,Cmust define an__init__method (ifCinstead inherited its__init__from its own base class, then@deprecatedwould mess up__init__inheritance when installing its own (deprecation-emitting)C.__init__).Parameters are the same as for
warn_deprecated, except that obj_type defaults to 'class' if decorating a class, 'attribute' if decorating a property, and 'function' otherwise.Examples
@deprecated('1.4.0') def the_function_to_deprecate(): pass
- matplotlib._api.deprecation.make_keyword_only(since, name, func=None)[source]#
 Decorator indicating that passing parameter name (or any of the following ones) positionally to func is being deprecated.
When used on a method that has a pyplot wrapper, this should be the outermost decorator, so that
boilerplate.pycan access the original signature.
- matplotlib._api.deprecation.rename_parameter(since, old, new, func=None)[source]#
 Decorator indicating that parameter old of func is renamed to new.
The actual implementation of func should use new, not old. If old is passed to func, a DeprecationWarning is emitted, and its value is used, even if new is also passed by keyword (this is to simplify pyplot wrapper functions, which always pass new explicitly to the Axes method). If new is also passed but positionally, a TypeError will be raised by the underlying function during argument binding.
Examples
@_api.rename_parameter("3.1", "bad_name", "good_name") def func(good_name): ...
- matplotlib._api.deprecation.warn_deprecated(since, *, message='', name='', alternative='', pending=False, obj_type='', addendum='', removal='')[source]#
 Display a standardized deprecation.
- Parameters:
 - sincestr
 The release at which this API became deprecated.
- messagestr, optional
 Override the default deprecation message. The
%(since)s,%(name)s,%(alternative)s,%(obj_type)s,%(addendum)s, and%(removal)sformat specifiers will be replaced by the values of the respective arguments passed to this function.- namestr, optional
 The name of the deprecated object.
- alternativestr, optional
 An alternative API that the user may use in place of the deprecated API. The deprecation warning will tell the user about this alternative if provided.
- pendingbool, optional
 If True, uses a PendingDeprecationWarning instead of a DeprecationWarning. Cannot be used together with removal.
- obj_typestr, optional
 The object type being deprecated.
- addendumstr, optional
 Additional text appended directly to the final message.
- removalstr, optional
 The expected removal version. With the default (an empty string), a removal version is automatically computed from since. Set to other Falsy values to not schedule a removal date. Cannot be used together with pending.
Examples
# To warn of the deprecation of "matplotlib.name_of_module" warn_deprecated('1.4.0', name='matplotlib.name_of_module', obj_type='module')