Skip to content

Versioned

has_version = is_versioned module-attribute

An alias of is_versioned.

Versioned

Bases: Protocol

Represents versioned objects, that is, objects that have the __version__ attribute of type str.

Source code in versions/versioned.py
20
21
22
23
24
25
26
@runtime_checkable
class Versioned(Protocol):
    """Represents versioned objects, that is, objects that have the `__version__`
    attribute of type [`str`][str].
    """

    __version__: str

is_versioned(item: Any) -> TypeGuard[Versioned]

Checks if the item implements the Versioned protocol.

That is, whether the item has the __version__ attribute of type str.

Parameters:

Name Type Description Default
item Any

The item to check.

required

Returns:

Type Description
TypeGuard[Versioned]

Whether the item implements the Versioned protocol.

Source code in versions/versioned.py
29
30
31
32
33
34
35
36
37
38
39
40
def is_versioned(item: Any) -> TypeGuard[Versioned]:
    """Checks if the `item` implements the [`Versioned`][versions.versioned.Versioned] protocol.

    That is, whether the `item` has the `__version__` attribute of type [`str`][str].

    Parameters:
        item: The item to check.

    Returns:
        Whether the `item` implements the [`Versioned`][versions.versioned.Versioned] protocol.
    """
    return is_instance(item, Versioned)

get_version(item: Versioned, version_type: Type[Version] = Version) -> Version

Fetches the __version__ attribute of the item, parsing it into the version of version_type, without checking whether the attribute exists.

Parameters:

Name Type Description Default
item Versioned

The item to fetch the version from.

required
version_type Type[Version]

The type of the version to parse.

Version

Returns:

Type Description
Version

The version of version_type of the item.

Source code in versions/versioned.py
60
61
62
63
64
65
66
67
68
69
70
71
def get_version(item: Versioned, version_type: Type[Version] = Version) -> Version:
    """Fetches the `__version__` attribute of the `item`,
    parsing it into the version of `version_type`, without checking whether the attribute exists.

    Parameters:
        item: The item to fetch the version from.
        version_type: The type of the version to parse.

    Returns:
        The version of `version_type` of the item.
    """
    return parse_version(item.__version__, version_type)