mode.utils.objects

Object utilities.

mode.utils.objects.DefaultsMapping

Mapping of attribute name to attributes default value.

alias of Mapping[str, Any]

mode.utils.objects.FieldMapping

Mapping of attribute name to attribute type.

alias of Mapping[str, Type]

exception mode.utils.objects.InvalidAnnotation

Raised by annotations() when encountering an invalid type.

class mode.utils.objects.KeywordReduce

Mixin class for objects that can be “pickled”.

“Pickled” means the object can be serialiazed using the Python binary serializer – the pickle module.

Python objects are made pickleable through defining the __reduce__ method, that returns a tuple of: (restore_function, function_starargs):

class X:

    def __init__(self, arg1, kw1=None):
        self.arg1 = arg1
        self.kw1 = kw1

    def __reduce__(self) -> Tuple[Callable, Tuple[Any, ...]]:
        return type(self), (self.arg1, self.kw1)

This is tedious since this means you cannot accept **kwargs in the constructur, so what we do is define a __reduce_keywords__ argument that returns a dict instead:

class X:

    def __init__(self, arg1, kw1=None):
        self.arg1 = arg1
        self.kw1 = kw1

    def __reduce_keywords__(self) -> Mapping[str, Any]:
        return {
            'arg1': self.arg1,
            'kw1': self.kw1,
        }
class mode.utils.objects.Unordered(value: _T)

Shield object from being ordered in heapq/__le__/etc.

mode.utils.objects.canoname(obj: Any, *, main_name: Optional[str] = None) str

Get qualname of obj, trying to resolve the real name of __main__.

mode.utils.objects.canonshortname(obj: Any, *, main_name: Optional[str] = None) str

Get non-qualified name of obj, resolve real name of __main__.

mode.utils.objects.eval_type(typ: Any, globalns: Optional[dict[str, Any]] = None, localns: Optional[dict[str, Any]] = None, invalid_types: Optional[set] = None, alias_types: Optional[Mapping] = None) Type

Convert (possible) string annotation to actual type.

Examples

>>> eval_type('List[int]') == typing.List[int]
mode.utils.objects.guess_polymorphic_type(typ: ~typing.Type, *, set_types: tuple[typing.Type, ...] = (typing.AbstractSet, typing.FrozenSet, typing.MutableSet, <class 'set'>, <class 'collections.abc.Set'>), list_types: tuple[typing.Type, ...] = (<class 'list'>, typing.Sequence, typing.MutableSequence, <class 'collections.abc.Sequence'>), tuple_types: tuple[typing.Type, ...] = (<class 'tuple'>, ), dict_types: tuple[typing.Type, ...] = (<class 'dict'>, typing.Mapping, typing.MutableMapping, <class 'collections.abc.Mapping'>)) tuple[Type, Type]

Try to find the polymorphic and concrete type of an abstract type.

Returns tuple of (polymorphic_type, concrete_type).

Examples

>>> guess_polymorphic_type(list[int])
(list, int)
>>> guess_polymorphic_type(Optional[list[int]])
(list, int)
>>> guess_polymorphic_type(MutableMapping[int, str])
(dict, str)
mode.utils.objects.iter_mro_reversed(cls: Type, stop: Type) Iterable[Type]

Iterate over superclasses, in reverse Method Resolution Order.

The stop argument specifies a base class that when seen will stop iterating (well actually start, since this is in reverse, see Example for demonstration).

Parameters:
  • cls (Type) – Target class.

  • stop (Type) – A base class in which we stop iteration.

Notes

The last item produced will be the class itself (cls).

Examples

>>> class A: ...
>>> class B(A): ...
>>> class C(B): ...
>>> list(iter_mro_reverse(C, object))
[A, B, C]
>>> list(iter_mro_reverse(C, A))
[B, C]
Yields:

Iterable[Type] – every class.

mode.utils.objects.label(s: Any) str

Return the name of an object as string.

mode.utils.objects.qualname(obj: Any) str

Get object qualified name.

mode.utils.objects.shortlabel(s: Any) str

Return the shortened name of an object as string.

mode.utils.objects.shortname(obj: Any) str

Get object name (non-qualified).