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.
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 access name
on the module will resolve the corresponding property (which may be decorated e.g. with _api.deprecated
for 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.
kwargs must consist of a single key, value pair. If key is in _mapping, return _mapping[value]
; else, raise an appropriate ValueError.
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
For each key, value pair in kwargs, check that value is in _values.
Sequence of values to check on.
Whether to print _values when raising ValueError.
key, value pairs as keyword arguments to find in _values.
If any value in kwargs is not found in _values.
>>> _api.check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
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.
>>> _api.check_isinstance((SomeClass, None), arg=arg)
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.
To check for (N, 2) shaped arrays
>>> _api.check_shape((None, 2), arg=arg, other_arg=other_arg)
Bases: object
Like property
, but also triggers on access via the class, and it is the class that's passed as argument.
class C: @classproperty def foo(cls): return cls.__name__ assert C.foo == "C"
Select and call the function that accepts *args, **kwargs
.
funcs is a list of functions which should not raise any exception (other than TypeError
if the arguments passed do not match their signature).
select_matching_signature
tries to call each of the functions in funcs with *args, **kwargs
(in the order in which they are given). Calls that fail with a TypeError
are silently skipped. As soon as a call succeeds, select_matching_signature
returns its return value. If no function accepts *args, **kwargs
, then the TypeError
raised by the last failing call is re-raised.
Callers should normally make sure that any *args, **kwargs
can only bind a single func (to avoid any ambiguity), although this is not checked by select_matching_signature
.
select_matching_signature
is intended to help implementing signature-overloaded functions. In general, such functions should be avoided, except for back-compatibility concerns. A typical use pattern is
def 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 TypeError
corresponding to the new signature if the arguments they passed in do not match any signature.
warnings.warn
wrapper 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
(or functools.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.
Bases: DeprecationWarning
A class for issuing deprecation warnings for Matplotlib users.
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 **kwargs
argument (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
.
@_api.delete_parameter("3.1", "unused") def func(used_arg, other_arg, unused, more_args): ...
Return obj.method
with a deprecation if it was overridden, else None.
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.
Either an object of the class where method is defined, or a subclass of that class.
Whether to allow overrides by "empty" methods without emitting a warning.
Additional parameters passed to warn_deprecated
to generate the deprecation warning; must at least include the "since" key.
Bases: object
Helper 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 makes attr
a property which forwards read and write access to self._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.
Decorator to mark a function, a class, or a property as deprecated.
When deprecating a classmethod, a staticmethod, or a property, the @deprecated
decorator should go under @classmethod
and @staticmethod
(i.e., deprecated
should directly decorate the underlying callable), but over @property
.
When deprecating a class C
intended to be used as a base class in a multiple inheritance hierarchy, C
must define an __init__
method (if C
instead inherited its __init__
from its own base class, then @deprecated
would 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.
@deprecated('1.4.0') def the_function_to_deprecate(): pass
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.py
can access the original signature.
alias of matplotlib._api.deprecation.MatplotlibDeprecationWarning
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.
@_api.rename_parameter("3.1", "bad_name", "good_name") def func(good_name): ...
Display a standardized deprecation.
The release at which this API became deprecated.
Override the default deprecation message. The %(since)s
, %(name)s
, %(alternative)s
, %(obj_type)s
, %(addendum)s
, and %(removal)s
format specifiers will be replaced by the values of the respective arguments passed to this function.
The name of the deprecated object.
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.
If True, uses a PendingDeprecationWarning instead of a DeprecationWarning. Cannot be used together with removal.
The object type being deprecated.
Additional text appended directly to the final message.
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.
# To warn of the deprecation of "matplotlib.name_of_module" warn_deprecated('1.4.0', name='matplotlib.name_of_module', obj_type='module')
© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/_api_api.html