typedcols module

Simple typed collections library.

Defines TypedDict and TypedList, which enforce inserted types based on simple type definition.

exception typedcols.Discard[source]

Bases: Exception

Sentinel class to signal expected dropping of the key. Can be returned or raised from type enforcing callable, and can itself be used as type enforcing callable.

exception typedcols.KeyNotAllowed[source]

Bases: LookupError

Raised when untyped key is inserted on type, which does not allow for untyped keys.

exception typedcols.KeysRequired[source]

Bases: LookupError

Raised when required keys are missing in dictionary (usually on the call of checkRequired method.

class typedcols.TypedDictMetaclass(name, bases, dct)[source]

Bases: abc.ABCMeta

Metaclass for TypedDict, allowing simplified typedefs - if typedef is callable, simple type object is assumed and correct dict is created. Metaclassed to be run just once for the class, not for each instance.

class typedcols.TypedList(iterable)[source]

Bases: collections.abc.MutableSequence

List type abstract class, which supports checking of inserted items type.

Must be subclassed, and subclass must populate ‘item_type’ class variable, and may also reassign list_class class attributes.

item_type: type enforcing callable, wich must take one argument, and
return value, coerced to expected type. Coercion may even be conversion, for example arbitrary date string, converted to DateTime. Because defined within class, Python authomatically makes it object method, so it must be wrapped in staticmethod(…) explicitly.

list_class: class or factory for underlying list implementation

insert(idx, val)[source]
static item_type(v)

alias of list

typedcols.typed_dict(name, allow_unknown, typedef)[source]

Helper for oneshot type definition

typedcols.typed_list(name, item_type)[source]

Helper for oneshot type definition