Skip to content

harbor_cli.types

Attributes

T = TypeVar('T') module-attribute

SEQUENCE_TYPES = (Sequence, abc.Sequence, list, List, tuple, Tuple, set, Set) module-attribute

Classes

RichTableKwargs

Bases: TypedDict

Source code in harbor_cli/types.py
class RichTableKwargs(TypedDict, total=False):
    caption: Optional[TextType]
    width: Optional[int]
    min_width: Optional[int]
    box: Optional[box.Box]
    safe_box: Optional[bool]
    padding: PaddingDimensions
    collapse_padding: bool
    pad_edge: bool
    expand: bool
    show_header: bool
    show_footer: bool
    show_edge: bool
    show_lines: bool
    leading: int
    style: StyleType
    row_styles: Optional[Iterable[StyleType]]
    header_style: Optional[StyleType]
    footer_style: Optional[StyleType]
    border_style: Optional[StyleType]
    title_style: Optional[StyleType]
    caption_style: Optional[StyleType]
    title_justify: "JustifyMethod"
    caption_justify: "JustifyMethod"
    highlight: bool

Attributes

caption: Optional[TextType] instance-attribute
width: Optional[int] instance-attribute
min_width: Optional[int] instance-attribute
box: Optional[box.Box] instance-attribute
safe_box: Optional[bool] instance-attribute
padding: PaddingDimensions instance-attribute
collapse_padding: bool instance-attribute
pad_edge: bool instance-attribute
expand: bool instance-attribute
show_header: bool instance-attribute
show_edge: bool instance-attribute
show_lines: bool instance-attribute
leading: int instance-attribute
style: StyleType instance-attribute
row_styles: Optional[Iterable[StyleType]] instance-attribute
header_style: Optional[StyleType] instance-attribute
footer_style: Optional[StyleType] instance-attribute
border_style: Optional[StyleType] instance-attribute
title_style: Optional[StyleType] instance-attribute
caption_style: Optional[StyleType] instance-attribute
title_justify: 'JustifyMethod' instance-attribute
caption_justify: 'JustifyMethod' instance-attribute
highlight: bool instance-attribute

Functions

is_sequence_func(func: Callable[[Any], Any]) -> bool

Checks if a callable takes a sequence as its first argument.

Source code in harbor_cli/types.py
def is_sequence_func(func: Callable[[Any], Any]) -> bool:
    """Checks if a callable takes a sequence as its first argument."""
    hints = typing.get_type_hints(func)
    if not hints:
        return False
    val = next(iter(hints.values()))
    return is_sequence_annotation(val)

is_sequence_annotation(annotation: Any) -> bool

Source code in harbor_cli/types.py
def is_sequence_annotation(annotation: Any) -> bool:
    # A string annotation will never pass this check, since it can't be
    # used as a generic type annotation. get_origin() will return None.
    # This is fine, however, because we don't actually want to treat
    # string annotations as sequences in this context anyway.
    origin = typing.get_origin(annotation)
    try:
        return issubclass(origin, Sequence)  # type: ignore
    except TypeError:
        return origin in SEQUENCE_TYPES

assert_type(value: Any, expect_type: Type[T]) -> T

Assert that a value is of a given type.

Not to be confused with typing.assert_type which was introcduced in 3.11! Unfortunate naming collision, but typing.assert_type has no runtime effect, while this function has.

Source code in harbor_cli/types.py
def assert_type(value: Any, expect_type: Type[T]) -> T:
    """Assert that a value is of a given type.

    Not to be confused with typing.assert_type which was introcduced in 3.11!
    Unfortunate naming collision, but typing.assert_type has no runtime effect,
    while this function has.
    """
    # TODO: handle Union types
    if is_sequence_annotation(expect_type):
        if value:
            v = value[0]
            args = typing.get_args(expect_type)
            if args:
                t = args[0]
            else:
                t = Any  # annotation was generic with no args
        else:
            v = value
            t = typing.get_origin(expect_type)
    else:
        v = value
        t = expect_type

    # If we find no annotation, we treat it as Any
    if t is None:
        t = Any

    if t != Any and not isinstance(v, t):
        raise TypeError(f"Expected value of type {t} but got {type(v)} instead.")
    return cast(T, value)