bagofholding.metadata module

Tools for extracting and logging information about python objects.

class bagofholding.metadata.HasContentType(content_type: 'str')[source]

Bases: object

content_type: str
class bagofholding.metadata.HasFieldIterator[source]

Bases: object

A simple helper mixin for dataclasses

field_items() → ItemsView[str, str | None][source]
class bagofholding.metadata.HasVersionInfo(qualname: 'str | None' = None, module: 'str | None' = None, version: 'str | None' = None)[source]

Bases: object

module: str | None = None
qualname: str | None = None
version: str | None = None
class bagofholding.metadata.Metadata(content_type: 'str', qualname: 'str | None' = None, module: 'str | None' = None, version: 'str | None' = None, meta: 'str | None' = None)[source]

Bases: HasVersionInfo, HasContentType, HasFieldIterator

meta: str | None = None
bagofholding.metadata.get_module(obj: Any) → str[source]
bagofholding.metadata.get_qualname(obj: Any) → str[source]
bagofholding.metadata.get_version(module_name: str, version_scraping: dict[str, Callable[[str], str | None]] | None = None) → str | None[source]

Given a module name, get its associated version (if any). By default, this simply looks for the __version__ attribute on the imported module.

For builtins this is just the python interpreter version.

Parameters:
  • module_name (str) – The module to examine.

  • version_scraping (VersionScrapingMap | None) – Since some modules may store their version in other ways, this provides an optional map between module names and callables to leverage for extracting that module’s version.

Returns:

The module’s version as a string, if any can be found.

Return type:

(str | None)

bagofholding.metadata.validate_version(metadata: Metadata, validator: Literal['exact', 'semantic-patch', 'semantic-minor', 'semantic-major', 'none'] | Callable[[str, str], bool] = 'exact', version_scraping: dict[str, Callable[[str], str | None]] | None = None) → None[source]

Check whether versioning information in a piece of metadata matches the current environment.

Parameters:
  • metadata (Metadata) – The metadata to validate.

  • validator (VersionValidatorType) – A recognized keyword or a callable; see versions_match() for semantics.

  • version_scraping (dict[str, Callable[[str], str]] | None) – An optional dictionary mapping module names to a callable that takes this name and returns a version (or None). The default callable imports the module string and looks for a __version__ attribute.

Raises:

EnvironmentMismatch – If the module in the metadata cannot be found, or if the current and metadata versions do not pass validation.

bagofholding.metadata.versions_match(current: str, stored: str, validator: Literal['exact', 'semantic-patch', 'semantic-minor', 'semantic-major', 'none'] | Callable[[str, str], bool]) → bool[source]

Compare a current version string against a stored reference.

Parameters:
  • current (str) – The version in the current environment.

  • stored (str) – The version recorded at save time.

  • validator (VersionValidatorType) – “exact” (literal string equality), “semantic-patch”/”semantic-minor”/”semantic-major” (PEP 440 versions match in their major.minor.micro / major.minor / major release components, ignoring pre-, post-, dev- and local segments; versions that cannot be parsed must match exactly), “none” (always matches), or a callable taking (current, stored) and returning a bool.

Returns:

Whether the versions match under the validator.

Return type:

(bool)

Raises:

ValueError – If the validator is an unrecognized keyword.