omnipy.util.helpers
General-purpose helper functions shared across Omnipy.
This module collects small utilities for mapping normalization, dataclass merging, type introspection, forward-reference resolution, import inspection, newline-aware text handling, and other reusable runtime helpers. Several helpers intentionally favor graceful fallbacks for dynamic Python edge cases, such as unresolved forward references, parameterized generics, optional values, empty inputs, and array-like equality results.
| FUNCTION | DESCRIPTION |
|---|---|
all_equals |
Collapse scalar or vectorized equality results into a single boolean. |
all_type_variants |
Return a flat tuple of all direct variants in a union-like type. |
as_dictable |
Return |
called_from_omnipy_tests |
Return whether the current call stack originates from Omnipy tests. |
create_merged_dict |
Merge two mapping-like inputs into a new dictionary. |
ensure_non_str_byte_iterable |
Wrap scalars in a tuple while leaving non-string iterables unchanged. |
ensure_plain_type |
Normalize a type form to its plain runtime representative when possible. |
evaluate_any_forward_refs_if_possible |
Resolve forward references recursively when enough namespace data exists. |
extract_newline |
Return only the trailing newline sequence from a single line. |
first_key_in_mapping |
Return the first key yielded by a mapping. |
first_value_in_mapping |
Return the first value yielded by a mapping. |
format_classname_with_params |
Format a class name together with rendered generic parameters. |
generate_run_slug |
Generate a short human-readable slug for a specific job run. |
generic_aware_issubclass_ignore_args |
Run |
get_calling_module_name |
Return the first caller module name found above the helper frame. |
get_datetime_format |
Return the locale-dependent date-time format string. |
get_default_if_typevar |
Return a |
get_event_loop_and_check_if_loop_is_running |
Get the current event loop and whether it is already running. |
get_first_item |
Return the first iterated item from a non-empty iterable. |
get_full_job_slug |
Build the full slug used to identify a concrete job run. |
get_job_name_slug |
Build a slug from a job class name and configured job name. |
get_parametrized_type |
Return a generic instance's recorded type when available. |
has_items |
Return whether |
is_iterable |
Return whether |
is_literal_type |
Return whether |
is_non_str_byte_iterable |
Return whether |
is_optional |
Return whether |
is_package_editable |
Return whether an installed package is marked editable in metadata. |
is_strict_subclass |
Return whether |
is_type_specialization |
Return whether |
is_union |
Return whether |
is_unreserved_identifier |
Return whether a string is a valid non-keyword Python identifier. |
max_newline_stripped_width |
Return the longest visible line width after stripping newlines. |
max_or_none |
Return the largest non- |
merge_dataclasses |
Create a new dataclass instance with selected field overrides. |
min_or_none |
Return the smallest non- |
recursive_module_import |
Recursively merge attributes from Omnipy base-class modules. |
recursive_module_import_new |
Import modules discovered under |
remove_forward_ref_notation |
Strip |
remove_none_vals |
Drop keyword arguments whose value is |
repr_max_len |
Return |
resolve |
Await |
sorted_dict_hash |
Return a deterministic tuple representation of a dictionary. |
split_all_content_to_lines |
Split text into newline-preserving logical lines. |
split_to_union_variants |
Return a union's direct variants or a singleton containing |
strip_and_split_newline |
Split a line into its content and trailing newline sequence. |
strip_newline |
Remove a trailing newline from a single line of text. |
strip_newlines |
Strip trailing newlines from each input line. |
takes_input_params_from |
Copy a callable parameter signature for static typing purposes. |
transfer_generic_args_to_cls |
Apply generic arguments from one type to another class when possible. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
Dictable |
TYPE:
|
NumberT |
|
Dictable
module-attribute
-
Reference
Code reference
omnipy
util
helpersas_dictable
-
Reference
Code reference
omnipy
util
helperscreate_merged_dict
NumberT
module-attribute
all_equals
Collapse scalar or vectorized equality results into a single boolean.
| PARAMETER | DESCRIPTION |
|---|---|
first
|
First value or array-like object.
|
second
|
Second value or array-like object.
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Notes
Supports numpy- and pandas-style comparison objects exposing all().
Source code in src/omnipy/util/helpers.py
all_type_variants
all_type_variants(
in_type: type | GenericAlias | UnionType | _UnionGenericAlias,
) -> tuple[type | GenericAlias, ...]
Return a flat tuple of all direct variants in a union-like type.
| PARAMETER | DESCRIPTION |
|---|---|
in_type
|
Type or union expression.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[type | GenericAlias, ...]
|
|
- Reference Code reference
Source code in src/omnipy/util/helpers.py
as_dictable
as_dictable(obj: object) -> Dictable | None
Return obj when it can be treated as mapping input.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Candidate mapping or iterable of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dictable | None
|
|
Notes
Iterable inputs are validated eagerly. Single-pass iterators may therefore be consumed by the tuple-pair check.
Source code in src/omnipy/util/helpers.py
called_from_omnipy_tests
Return whether the current call stack originates from Omnipy tests.
Source code in src/omnipy/util/helpers.py
create_merged_dict
Merge two mapping-like inputs into a new dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
dictable_1
|
Base mapping or iterable of key-value pairs.
TYPE:
|
dictable_2
|
Mapping or iterable whose values override
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[_KeyT, Any]
|
A new dictionary containing keys from both inputs. |
Source code in src/omnipy/util/helpers.py
ensure_non_str_byte_iterable
Wrap scalars in a tuple while leaving non-string iterables unchanged.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Iterable or scalar value.
|
| RETURNS | DESCRIPTION |
|---|---|
|
|
Notes
str and bytes are treated as scalar values and therefore wrapped.
Source code in src/omnipy/util/helpers.py
ensure_plain_type
Normalize a type form to its plain runtime representative when possible.
| PARAMETER | DESCRIPTION |
|---|---|
in_type
|
Type expression, including unions, generics, and forward refs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ForwardRef | TypeVar | type | _SpecialForm | NoneType
|
The generic origin for parametrized types, |
Source code in src/omnipy/util/helpers.py
evaluate_any_forward_refs_if_possible
evaluate_any_forward_refs_if_possible(
in_type: TypeForm, calling_module: str | None = None, **localns
) -> TypeForm
Resolve forward references recursively when enough namespace data exists.
| PARAMETER | DESCRIPTION |
|---|---|
in_type
|
Type expression that may contain forward references.
TYPE:
|
calling_module
|
Module name used as global namespace lookup.
TYPE:
|
**localns
|
Extra symbols available during forward-reference evaluation.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeForm
|
A resolved type expression when evaluation succeeds, otherwise the input expression or a partially resolved variant. |
Notes
Unresolvable names are left untouched instead of raising NameError.
Source code in src/omnipy/util/helpers.py
extract_newline
Return only the trailing newline sequence from a single line.
| PARAMETER | DESCRIPTION |
|---|---|
line
|
String expected to contain a single logical line.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The trailing newline sequence, or an empty string. |
Source code in src/omnipy/util/helpers.py
first_key_in_mapping
Return the first key yielded by a mapping.
| PARAMETER | DESCRIPTION |
|---|---|
mapping
|
Mapping to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
_KeyT
|
The first key produced by |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the mapping is empty. |
Source code in src/omnipy/util/helpers.py
first_value_in_mapping
Return the first value yielded by a mapping.
| PARAMETER | DESCRIPTION |
|---|---|
mapping
|
Mapping to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
_ValueT
|
The first value produced by |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the mapping is empty. |
Source code in src/omnipy/util/helpers.py
format_classname_with_params
Format a class name together with rendered generic parameters.
| PARAMETER | DESCRIPTION |
|---|---|
cls_name
|
Base class name.
TYPE:
|
params_str
|
Already rendered generic parameter string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string on the form |
Source code in src/omnipy/util/helpers.py
generate_run_slug
Generate a short human-readable slug for a specific job run.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Random three-word slug suitable for run naming. |
generic_aware_issubclass_ignore_args
Run issubclass while tolerating parametrized generic aliases.
| PARAMETER | DESCRIPTION |
|---|---|
cls
|
Candidate class or parametrized generic alias.
|
cls_or_tuple
|
Superclass or tuple of superclasses.
|
| RETURNS | DESCRIPTION |
|---|---|
|
|
Notes
When issubclass rejects a parametrized generic, the helper retries
with the generic origin and ignores its type arguments.
Source code in src/omnipy/util/helpers.py
get_calling_module_name
Return the first caller module name found above the helper frame.
Source code in src/omnipy/util/helpers.py
get_datetime_format
get_datetime_format(locale: LocaleType | None = None) -> str
Return the locale-dependent date-time format string.
| PARAMETER | DESCRIPTION |
|---|---|
locale
|
Locale passed to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The current locale's |
Notes
This function changes the process-wide locale as a side effect.
Source code in src/omnipy/util/helpers.py
get_default_if_typevar
Return a TypeVar default value, or the input type unchanged.
| PARAMETER | DESCRIPTION |
|---|---|
typ_
|
Concrete type, type form, or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
type[_ObjT] | TypeForm
|
The default bound value for a |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/omnipy/util/helpers.py
get_event_loop_and_check_if_loop_is_running
Get the current event loop and whether it is already running.
| RETURNS | DESCRIPTION |
|---|---|
tuple[asyncio.AbstractEventLoop | None, bool]
|
A tuple of |
Notes
RuntimeError from asyncio.get_event_loop() is handled and
converted to (None, False) semantics.
Source code in src/omnipy/util/helpers.py
get_first_item
Return the first iterated item from a non-empty iterable.
| PARAMETER | DESCRIPTION |
|---|---|
iterable
|
Iterable expected to contain at least one item.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The first yielded item. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If |
Source code in src/omnipy/util/helpers.py
get_full_job_slug
Build the full slug used to identify a concrete job run.
| PARAMETER | DESCRIPTION |
|---|---|
job_cls_name
|
Concrete job class name.
TYPE:
|
job_name
|
Configured human-readable job name.
TYPE:
|
job_run_slug
|
Run-specific slug, typically from :func:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Full job-run slug combining the job slug and run slug. |
Source code in src/omnipy/util/helpers.py
get_job_name_slug
Build a slug from a job class name and configured job name.
| PARAMETER | DESCRIPTION |
|---|---|
job_cls_name
|
Concrete job class name.
TYPE:
|
job_name
|
Configured human-readable job name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Dash-separated slug combining the class and job names. |
Source code in src/omnipy/util/helpers.py
get_parametrized_type
Return a generic instance's recorded type when available.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Object that may carry
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
|
Source code in src/omnipy/util/helpers.py
has_items
Return whether obj reports a positive length.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Value that may implement
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_iterable
Return whether obj can be iterated over.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Value to test.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeGuard[Iterable[_ObjT]]
|
|
Notes
Strings and bytes count as iterables here. Use
is_non_str_byte_iterable() when that is undesirable.
Source code in src/omnipy/util/helpers.py
is_literal_type
Return whether value is a typing.Literal specialization.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Type form or value to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_non_str_byte_iterable
Return whether obj is iterable but not str or bytes.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Value to test.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeGuard[Iterable[_ObjT]]
|
|
Source code in src/omnipy/util/helpers.py
is_optional
Return whether cls_or_type is a union that includes None.
| PARAMETER | DESCRIPTION |
|---|---|
cls_or_type
|
Type or object to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_package_editable
cached
Return whether an installed package is marked editable in metadata.
| PARAMETER | DESCRIPTION |
|---|---|
package_name
|
Distribution name resolved via
|
| RETURNS | DESCRIPTION |
|---|---|
|
|
Notes
Metadata lookup and JSON parsing failures are handled and return False.
Source code in src/omnipy/util/helpers.py
is_strict_subclass
cached
is_strict_subclass(
__cls: type, __class_or_tuple: type | UnionType | tuple[type | UnionType | tuple[Any, ...], ...]
) -> bool
Return whether __cls is a proper subclass of the given parent type.
| PARAMETER | DESCRIPTION |
|---|---|
__cls
|
Candidate subclass.
TYPE:
|
__class_or_tuple
|
Parent class or tuple accepted by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_type_specialization
Return whether value is a specialization of type[...].
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Type form or value to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_union
Return whether cls_or_type represents a union expression.
| PARAMETER | DESCRIPTION |
|---|---|
cls_or_type
|
Type or object to inspect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
is_unreserved_identifier
Return whether a string is a valid non-keyword Python identifier.
| PARAMETER | DESCRIPTION |
|---|---|
identifier
|
Candidate identifier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/omnipy/util/helpers.py
max_newline_stripped_width
Return the longest visible line width after stripping newlines.
| PARAMETER | DESCRIPTION |
|---|---|
lines
|
Lines that may include newline terminators.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The maximum character width excluding trailing newline sequences. |
Source code in src/omnipy/util/helpers.py
max_or_none
Return the largest non-None argument.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Numbers or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NumberT | None
|
The maximum non- |
Source code in src/omnipy/util/helpers.py
merge_dataclasses
Create a new dataclass instance with selected field overrides.
| PARAMETER | DESCRIPTION |
|---|---|
dataclass
|
Source dataclass instance.
TYPE:
|
other_dict
|
Replacement values keyed by dataclass field name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
_DataclassT
|
A new instance of the same dataclass type. |
Source code in src/omnipy/util/helpers.py
min_or_none
Return the smallest non-None argument.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Numbers or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NumberT | None
|
The minimum non- |
Source code in src/omnipy/util/helpers.py
recursive_module_import
recursive_module_import(
module: ModuleType, imported_modules: list[ModuleType] = []
) -> dict[str, object]
Recursively merge attributes from Omnipy base-class modules.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
Module whose class hierarchy should be inspected.
TYPE:
|
imported_modules
|
Visited modules used to avoid repeated imports.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, object]
|
A merged module namespace including relevant internal base-class modules. |
Source code in src/omnipy/util/helpers.py
recursive_module_import_new
recursive_module_import_new(
root_path: list[str], imported_modules: dict[str, ModuleType], excluded_set: set[str]
)
Import modules discovered under root_path into imported_modules.
| PARAMETER | DESCRIPTION |
|---|---|
root_path
|
Package search paths passed to
TYPE:
|
imported_modules
|
Destination mapping updated in place by module name.
TYPE:
|
excluded_set
|
Module names whose entire subtrees should be skipped.
TYPE:
|
Notes
Importing modules executes their import-time side effects.
Source code in src/omnipy/util/helpers.py
remove_forward_ref_notation
Strip ForwardRef('...') wrappers from a rendered type string.
| PARAMETER | DESCRIPTION |
|---|---|
type_str
|
String representation that may contain forward-ref notation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
The string with the wrapper removed. |
Source code in src/omnipy/util/helpers.py
remove_none_vals
Drop keyword arguments whose value is None.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Arbitrary keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[object, object]
|
A dictionary containing only non- |
Source code in src/omnipy/util/helpers.py
repr_max_len
Return repr(data) truncated to at most max_len characters.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Object to represent.
TYPE:
|
max_len
|
Maximum number of repr characters before truncation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
The full repr when short enough, otherwise a truncated repr with |
Source code in src/omnipy/util/helpers.py
resolve
async
Await obj when needed and otherwise return it unchanged.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
Awaitable or plain value.
|
| RETURNS | DESCRIPTION |
|---|---|
|
The awaited result for awaitables, or |
Source code in src/omnipy/util/helpers.py
sorted_dict_hash
Return a deterministic tuple representation of a dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
d
|
Dictionary whose keys must be sortable with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
A tuple of |
Source code in src/omnipy/util/helpers.py
split_all_content_to_lines
Split text into newline-preserving logical lines.
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Full text content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
A list like |
Notes
The extra empty string keeps line-oriented consumers aligned with repr-like content that omits the final newline character.
Source code in src/omnipy/util/helpers.py
split_to_union_variants
Return a union's direct variants or a singleton containing type_.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
Type expression to split.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeForm]
|
A tuple of union members, or |
Source code in src/omnipy/util/helpers.py
strip_and_split_newline
Split a line into its content and trailing newline sequence.
| PARAMETER | DESCRIPTION |
|---|---|
line
|
String expected to contain a single logical line.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, str]
|
A |
Source code in src/omnipy/util/helpers.py
strip_newline
Remove a trailing newline from a single line of text.
| PARAMETER | DESCRIPTION |
|---|---|
line
|
String expected to contain at most one logical line.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The line without its newline terminator. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If |
Source code in src/omnipy/util/helpers.py
strip_newlines
Strip trailing newlines from each input line.
| PARAMETER | DESCRIPTION |
|---|---|
lines
|
Iterable of single-line strings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
A list of newline-free line contents. |
Source code in src/omnipy/util/helpers.py
takes_input_params_from
takes_input_params_from(
func: Callable[Concatenate[Any, _P], Any],
) -> Callable[[Callable[Concatenate[Any, _P], _RetT]], Callable[Concatenate[Any, _P], _RetT]]
Copy a callable parameter signature for static typing purposes.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
Callable whose input parameters should be mirrored.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[Callable[Concatenate[Any, _P], _RetT]], Callable[Concatenate[Any, _P], _RetT]]
|
An identity decorator that preserves the input signature of |
Source code in src/omnipy/util/helpers.py
transfer_generic_args_to_cls
Apply generic arguments from one type to another class when possible.
| PARAMETER | DESCRIPTION |
|---|---|
to_cls
|
Target class or generic alias factory.
|
from_generic_type
|
Source type whose arguments should be reused.
|
| RETURNS | DESCRIPTION |
|---|---|
|
|