misc_utils

Miscellaneous helper functions used in several modules.

parityos_serialization.utils.misc_utils.get_import_path(cls: type) str

Get full import path of cls.

parityos_serialization.utils.misc_utils.import_from_path(import_path: str) type

Import and return class from import_path.

Parameters:

import_path – Full import path for importing class.

Returns:

class imported from import_path.

Raises:
  • ImportErrorimport_path has no module part, i.e. does not contain “.”

  • ImportError – The class could not be imported from import_path.

parityos_serialization.utils.misc_utils.to_frozenset(elements: T | Iterable[T]) frozenset[T]

Transform a single element or an iterable of elements to a frozenset.

parityos_serialization.utils.misc_utils.is_generic_alias(obj: Any) bool

Check whether an object is a generic alias.

This works both for generic aliases of built int types (e.g. list[int], dict[str, Any]) and custom generic aliases (e.g. subclasses of typing.Generic).

parityos_serialization.utils.misc_utils.is_slotted_class_state(state: object) TypeIs[tuple[None | dict[str, Any], dict[str, Any]]]

Check whether an object is a serialized state of a slotted class.

See also object.__getstate__.

parityos_serialization.utils.misc_utils.is_metaclass(cls: type) TypeIs[type[type]]

Check whether a class is a metaclass (i.e. an instance and a subclass of type).

parityos_serialization.utils.misc_utils.get_unique_subclasses(cls: type) list[type]

Get immediate subclasses with unique names of cls.

This is needed for example for getting subclasses of slotted attrs classes.

Parameters:

cls – Class for which to retrieve immediate subclasses.

Returns:

Immediate subclasses ofr cls with unique names.

parityos_serialization.utils.misc_utils.walk_subclasses(cls: type) Iterator[type]

Iterate through all subclasses of cls in its inheritance tree.

The iteration is depth first and cls is included in the resulting iterator

Parameters:

cls – Class for which to get all subclasses.

Yields:

Subclasses of cls that are currently imported and known to the interpreter.