Skip to content

robot.utils.robottypes

type_name

type_name(item, capitalize=False)

Return "non-technical" type name for objects and types.

For example, 'integer' instead of 'int' and 'file' instead of 'TextIOWrapper'.

Source code in src/robot/utils/robottypes.py
def type_name(item, capitalize=False):
    """Return "non-technical" type name for objects and types.

    For example, 'integer' instead of 'int' and 'file' instead of 'TextIOWrapper'.
    """
    if getattr(item, '__origin__', None):
        item = item.__origin__
    if hasattr(item, '_name') and item._name:
        # Prior to Python 3.10 Union, Any, etc. from typing didn't have `__name__`.
        # but instead had `_name`. Python 3.10 has both and newer only `__name__`.
        # Also, pandas.Series has `_name` but it's None.
        name = item._name
    elif is_union(item):
        name = 'Union'
    elif isinstance(item, IOBase):
        name = 'file'
    else:
        typ = type(item) if not isinstance(item, type) else item
        named_types = {str: 'string', bool: 'boolean', int: 'integer',
                       type(None): 'None', dict: 'dictionary'}
        name = named_types.get(typ, typ.__name__.strip('_'))
    return name.capitalize() if capitalize and name.islower() else name

type_repr

type_repr(typ, nested=True)

Return string representation for types.

Aims to look as much as the source code as possible. For example, 'List[Any]' instead of 'typing.List[typing.Any]'.

Source code in src/robot/utils/robottypes.py
def type_repr(typ, nested=True):
    """Return string representation for types.

    Aims to look as much as the source code as possible. For example, 'List[Any]'
    instead of 'typing.List[typing.Any]'.
    """
    if typ is type(None):
        return 'None'
    if typ is Ellipsis:
        return '...'
    if is_union(typ):
        return ' | '.join(type_repr(a) for a in typ.__args__) if nested else 'Union'
    if getattr(typ, '__origin__', None) is Literal:
        if nested:
            args = ', '.join(repr(a) for a in typ.__args__)
            return f'Literal[{args}]'
        return 'Literal'
    name = _get_type_name(typ)
    if nested and has_args(typ):
        args = ', '.join(type_repr(a) for a in typ.__args__)
        return f'{name}[{args}]'
    return name

has_args

has_args(type)

Helper to check has type valid __args__.

__args__ contains TypeVars when accessed directly from typing.List and other such types with Python 3.8. Python 3.9+ don't have __args__ at all. Parameterize usages like List[int].__args__ always work the same way.

This helper can be removed in favor of using hasattr(type, '__args__') when we support only Python 3.9 and newer.

Source code in src/robot/utils/robottypes.py
def has_args(type):
    """Helper to check has type valid ``__args__``.

   ``__args__`` contains TypeVars when accessed directly from ``typing.List`` and
   other such types with Python 3.8. Python 3.9+ don't have ``__args__`` at all.
   Parameterize usages like ``List[int].__args__`` always work the same way.

    This helper can be removed in favor of using ``hasattr(type, '__args__')``
    when we support only Python 3.9 and newer.
    """
    args = getattr(type, '__args__', None)
    return bool(args and not all(isinstance(a, TypeVar) for a in args))

is_truthy

is_truthy(item)

Returns True or False depending on is the item considered true or not.

Validation rules:

  • If the value is a string, it is considered false if it is 'FALSE', 'NO', 'OFF', '0', 'NONE' or '', case-insensitively.
  • Other strings are considered true.
  • Other values are handled by using the standard bool() function.

Designed to be used also by external test libraries that want to handle Boolean values similarly as Robot Framework itself. See also :func:is_falsy.

Source code in src/robot/utils/robottypes.py
def is_truthy(item):
    """Returns `True` or `False` depending on is the item considered true or not.

    Validation rules:

    - If the value is a string, it is considered false if it is `'FALSE'`,
      `'NO'`, `'OFF'`, `'0'`, `'NONE'` or `''`, case-insensitively.
    - Other strings are considered true.
    - Other values are handled by using the standard `bool()` function.

    Designed to be used also by external test libraries that want to handle
    Boolean values similarly as Robot Framework itself. See also
    :func:`is_falsy`.
    """
    if is_string(item):
        return item.upper() not in FALSE_STRINGS
    return bool(item)

is_falsy

is_falsy(item)

Opposite of :func:is_truthy.

Source code in src/robot/utils/robottypes.py
def is_falsy(item):
    """Opposite of :func:`is_truthy`."""
    return not is_truthy(item)