Skip to content

Module dewret.utils

Utility module.

General types and functions to centralize common logic.

Variables

BasicType
FirmType
RawType

Functions

ensure_raw

def ensure_raw(
    value: Any
) -> Union[str, float, bool, bytes, int, NoneType, list['RawType'], dict[str, 'RawType']]

Check if a variable counts as "raw".

This works around a checking issue that isinstance of a union of types assigned to a variable, such as RawType, may throw errors even though Python 3.11+ does not. Instead, we explicitly make the full union in the statement below.

flatten

def flatten(
    value: Any
) -> Union[str, float, bool, bytes, int, NoneType, list['RawType'], dict[str, 'RawType']]

Takes a Raw-like structure and makes it RawType.

Particularly useful for squashing any TypedDicts.

Parameters:

Name Type Description Default
value None value to squash None

hasher

def hasher(
    construct: str | float | bool | bytes | int | None | list['FirmType'] | dict[str, 'FirmType'] | tuple['FirmType', ...]
) -> str

Consistently hash a RawType or tuple structure.

Turns a possibly nested structure of basic types, dicts, lists and tuples into a consistent hash.

Parameters:

Name Type Description Default
construct None structure to hash. None

Returns:

Type Description
None Hash string that should be unique to the construct. The limits of this uniqueness
have not yet been explicitly calculated.

is_raw

def is_raw(
    value: Any
) -> bool

Check if a variable counts as "raw".

This works around a checking issue that isinstance of a union of types assigned to a variable, such as RawType, may throw errors even though Python 3.11+ does not. Instead, we explicitly make the full union in the statement below.

make_traceback

def make_traceback(
    skip: int = 2
) -> traceback | None

Creates a traceback for the current frame.

Necessary to allow tracebacks to be prepped for potential errors in lazy-evaluated functions.

Parameters:

Name Type Description Default
skip None number of frames to skip before starting traceback. None

Classes

DataclassProtocol

class DataclassProtocol(
    *args,
    **kwargs
)

Format of a dataclass.

Since dataclasses do not expose a proper type, we use this to represent them.

Ancestors (in MRO)

  • typing.Protocol
  • typing.Generic