spyder.api.utils#

Helper functions to work with the Spyder plugin API.

Functions

abstract_attribute([obj])

Decorator to mark abstract attributes.

get_class_values(cls)

Get the attribute values for the class enumerations used in our API.

Classes

ABCMeta(name, bases, namespace, /, **kwargs)

Metaclass to mark abstract classes.

DummyAttribute()

Dummy class to mark abstract attributes.

PrefixNode([path])

Utility class used to represent a prefixed string tuple.

PrefixedTuple([path])

Utility class to store and iterate over prefixed string tuples.

classproperty([fget, fset, fdel, doc])

Decorator to declare class constants requiring computation as properties.

spyder.api.utils.get_class_values(cls) list[str][source]#

Get the attribute values for the class enumerations used in our API.

Idea from Stack Overflow.

Parameters:

cls – Class object to list the enumeration values of.

Returns:

String attribute values from a Spyder pseudo-“enum” class.

Return type:

list[str]

class spyder.api.utils.PrefixNode(path: tuple[str, ...] | None = None)[source]#

Bases: object

Utility class used to represent a prefixed string tuple.

__init__(path: tuple[str, ...] | None = None) None[source]#

Representation of a prefixed string tuple.

Parameters:

path (tuple[str, ...] | None, optional) – Underlying prefixed string tuple. The default is None.

Return type:

None

add_path(path: tuple[str, ...]) None[source]#

Add a path to the prefix node.

Parameters:

path (tuple[str, ...]) – Underlying prefixed string tuple.

Return type:

None

class spyder.api.utils.PrefixedTuple(path: tuple[str, ...] | None = None)[source]#

Bases: PrefixNode

Utility class to store and iterate over prefixed string tuples.

class spyder.api.utils.classproperty(fget=None, fset=None, fdel=None, doc=None)[source]#

Bases: property

Decorator to declare class constants requiring computation as properties.

Idea from Stack Overflow.

__get__(cls, owner)[source]#

Return an attribute of instance, which is of type owner.

class spyder.api.utils.DummyAttribute[source]#

Bases: object

Dummy class to mark abstract attributes.

spyder.api.utils.abstract_attribute(obj: Callable[[_P], _T] | DummyAttribute | None = None) _T[source]#

Decorator to mark abstract attributes.

Must be used in conjunction with the abc.ABCMeta metaclass.

Parameters:

obj (collections.abc.Callable[_P, _T] | DummyAttribute | None, optional) – The callable attribute to mark as abstract, a new instance of DummyAttribute by default.

Returns:

The result of executing the callable attribute obj.

Return type:

_T

class spyder.api.utils.ABCMeta(name, bases, namespace, /, **kwargs)[source]#

Bases: ABCMeta

Metaclass to mark abstract classes.

Adds support for abstract attributes. If a class has abstract attributes and is instantiated, a NotImplementedError is raised.

Usage#

class MyABC(metaclass=ABCMeta):
    @abstract_attribute
    def my_abstract_attribute(self):
        pass

class MyClassOK(MyABC):
    def __init__(self):
        self.my_abstract_attribute = 1

class MyClassNotOK(MyABC):
    pass
raises NotImplementedError:

When it’s not possible to instantiate an abstract class with abstract attributes.

__call__(*args, **kwargs)[source]#

Call self as a function.