Skip to content

envtests

Class info

🛈 DocStrings

contains_files

contains_files(directory: str | PathLike[str]) -> bool

Check if given directory exists and contains any files.

Supports regular file paths and fsspec URLs.

Parameters:

Name Type Description Default
directory str | PathLike[str]

The directoy to check

required
Source code in src/jinjarope/envtests.py
168
169
170
171
172
173
174
175
176
177
def contains_files(directory: str | os.PathLike[str]) -> bool:
    """Check if given directory exists and contains any files.

    Supports regular file paths and fsspec URLs.

    Args:
        directory: The directoy to check
    """
    path = upath.UPath(directory)
    return path.exists() and any(path.iterdir())

is_env_var

is_env_var(env_var: str) -> bool

Returns true if an environment variable with given name has a value.

Parameters:

Name Type Description Default
env_var str

The environment variable name to check

required
Source code in src/jinjarope/envtests.py
191
192
193
194
195
196
197
def is_env_var(env_var: str) -> bool:
    """Returns true if an environment variable with given name has a value.

    Args:
        env_var: The environment variable name to check
    """
    return bool(os.getenv(env_var))

is_fsspec_url

is_fsspec_url(string: str | PathLike[str]) -> bool

Returns true if the given URL looks like an fsspec protocol, except http/https.

Parameters:

Name Type Description Default
string str | PathLike[str]

The URL to check

required
Source code in src/jinjarope/envtests.py
155
156
157
158
159
160
161
162
163
164
165
def is_fsspec_url(string: str | os.PathLike[str]) -> bool:
    """Returns true if the given URL looks like an fsspec protocol, except http/https.

    Args:
        string: The URL to check
    """
    return (
        isinstance(string, str)
        and bool(_RFC_3986_PATTERN.match(string))
        and not string.startswith(("http://", "https://"))
    )

is_http_url

is_http_url(string: str) -> bool

Return true when given string represents a HTTP url.

Parameters:

Name Type Description Default
string str

The string to check

required
Source code in src/jinjarope/envtests.py
107
108
109
110
111
112
113
def is_http_url(string: str) -> bool:
    """Return true when given string represents a HTTP url.

    Args:
        string: The string to check
    """
    return string.startswith(("http://", "https://", "www.")) and "\n" not in string

is_in_std_library

is_in_std_library(fn: str | Callable[..., Any]) -> bool

Return true when given fn / string is part of the std library.

Parameters:

Name Type Description Default
fn str | Callable[..., Any]

(Name of) function to check

required
Source code in src/jinjarope/envtests.py
145
146
147
148
149
150
151
152
def is_in_std_library(fn: str | Callable[..., Any]) -> bool:
    """Return true when given fn / string is part of the std library.

    Args:
        fn: (Name of) function to check
    """
    name = fn if isinstance(fn, str) else fn.__module__
    return name.split(".")[0] in sys.stdlib_module_names

is_indented

is_indented(text: str, indentation: str = '    ') -> bool

Check whether all lines of given text are indented.

Parameters:

Name Type Description Default
text str

The text to check

required
indentation str

The indent each line must start with

' '
Source code in src/jinjarope/envtests.py
200
201
202
203
204
205
206
207
def is_indented(text: str, indentation: str = "    ") -> bool:
    """Check whether all lines of given text are indented.

    Args:
        text: The text to check
        indentation: The indent each line must start with
    """
    return all(i.startswith(indentation) for i in text.split("\n"))

is_installed

is_installed(package_name: str) -> bool

Returns true if a package with given name is found.

Parameters:

Name Type Description Default
package_name str

The package name to check

required
Source code in src/jinjarope/envtests.py
180
181
182
183
184
185
186
187
188
def is_installed(package_name: str) -> bool:
    """Returns true if a package with given name is found.

    Args:
        package_name: The package name to check
    """
    import importlib.util

    return bool(importlib.util.find_spec(package_name))

is_instance

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

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

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)

is_number

is_number(value: Any) -> bool

Try to convert value to a float.

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)

is_protocol_url

is_protocol_url(string: str) -> bool

Return true when given string represents any type of URL.

Parameters:

Name Type Description Default
string str

The string to check

required
Source code in src/jinjarope/envtests.py
116
117
118
119
120
121
122
def is_protocol_url(string: str) -> bool:
    """Return true when given string represents any type of URL.

    Args:
        string: The string to check
    """
    return "://" in string and "\n" not in string

is_python_builtin

is_python_builtin(fn: str | Callable[..., Any]) -> bool

Return true when given fn / string represents a python builtin.

Parameters:

Name Type Description Default
fn str | Callable[..., Any]

(Name of) function to check

required
Source code in src/jinjarope/envtests.py
136
137
138
139
140
141
142
def is_python_builtin(fn: str | Callable[..., Any]) -> bool:
    """Return true when given fn / string represents a python builtin.

    Args:
        fn: (Name of) function to check
    """
    return fn in dir(builtins) if isinstance(fn, str) else inspect.isbuiltin(fn)

is_python_keyword

is_python_keyword(string: str) -> bool

Return true when given string represents a python keyword.

Parameters:

Name Type Description Default
string str

The string to check

required
Source code in src/jinjarope/envtests.py
125
126
127
128
129
130
131
132
133
def is_python_keyword(string: str) -> bool:
    """Return true when given string represents a python keyword.

    Args:
        string: The string to check
    """
    import keyword

    return keyword.iskeyword(string)

is_subclass

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

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

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)