Skip to content

type (10)

datetime

datetime(value: Any) -> bool

Return whether a value is a datetime.

Example

Jinja call:

{% if "a" is datetime %}
True!
{% endif %}
Result:

DocStrings
Source code in src/jinjarope/envtests.py
97
98
99
def _is_datetime(value: Any) -> bool:
    """Return whether a value is a datetime."""
    return isinstance(value, datetime.datetime)

dict

dict(value: Any) -> bool

Return whether a value is a tuple.

Example

Jinja call:

{% if {"a": "b"} is dict %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
56
57
58
def _is_dict(value: Any) -> bool:
    """Return whether a value is a tuple."""
    return isinstance(value, dict)

instance

instance(obj: object, typ: str | type) -> bool

Like the isinstance builtin, but also accepts strs as type.

Example

Jinja call:

{% if {"a": "b"} is instance("dict") %}
True!
{% endif %}
Result:
True!

DocStrings

Parameters:

Name Type Description Default
obj object

The object to check

required
typ str | type

A type (name)

required
Source code in src/jinjarope/envtests.py
71
72
73
74
75
76
77
78
79
80
81
def is_instance(obj: object, typ: str | type) -> bool:
    """Like the isinstance builtin, but also accepts strs as type.

    Args:
        obj: The object to check
        typ: A type (name)
    """
    kls = utils.resolve(typ) if isinstance(typ, str) else typ
    if not isinstance(kls, type):
        raise TypeError(kls)
    return isinstance(obj, kls)

list

list(value: Any) -> bool

Return whether a value is a list.

Example

Jinja call:

{% if [] is list %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
41
42
43
def _is_list(value: Any) -> bool:
    """Return whether a value is a list."""
    return isinstance(value, list)

number

number(value: Any) -> bool

Try to convert value to a float.

Example

Jinja call:

{% if 5 is number %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
27
28
29
30
31
32
33
def is_number(value: Any) -> bool:
    """Try to convert value to a float."""
    try:
        fvalue = float(value)
    except (ValueError, TypeError):
        return False
    return math.isfinite(fvalue)

set

set(value: Any) -> bool

Return whether a value is a set.

Example

Jinja call:

{% if [] is set %}
True!
{% endif %}
Result:

DocStrings
Source code in src/jinjarope/envtests.py
46
47
48
def _is_set(value: Any) -> bool:
    """Return whether a value is a set."""
    return isinstance(value, set)

string_like

string_like(value: Any) -> bool

Return whether a value is a string or string like object.

Example

Jinja call:

{% if "abc" is string_like %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
102
103
104
def _is_string_like(value: Any) -> bool:
    """Return whether a value is a string or string like object."""
    return isinstance(value, str | bytes | bytearray)

subclass

subclass(obj: type, typ: str | type) -> bool

Like the issubclass builtin, but also accepts strs as type.

Example

Jinja call:

{% if cycler is subclass("dict") %}
True!
{% endif %}
Result:

DocStrings

Parameters:

Name Type Description Default
obj type

The class to check

required
typ str | type

A type (name)

required
Source code in src/jinjarope/envtests.py
84
85
86
87
88
89
90
91
92
93
94
def is_subclass(obj: type, typ: str | type) -> bool:
    """Like the issubclass builtin, but also accepts strs as type.

    Args:
        obj: The class to check
        typ: A type (name)
    """
    kls = utils.resolve(typ) if isinstance(typ, str) else typ
    if not isinstance(kls, type):
        raise TypeError(kls)
    return issubclass(obj, kls)

tuple

tuple(value: Any) -> bool

Return whether a value is a tuple.

Example

Jinja call:

{% if () is tuple %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
51
52
53
def _is_tuple(value: Any) -> bool:
    """Return whether a value is a tuple."""
    return isinstance(value, tuple)

type

type(value: Any) -> bool

Return whether a value is a type.

Example

Jinja call:

{% if zip is type %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in src/jinjarope/envtests.py
36
37
38
def _is_type(value: Any) -> bool:
    """Return whether a value is a type."""
    return isinstance(value, type)