Skip to main content

backend.typed

Handles the definitions of arguments types for Spock (backend: attrs)

SavePath Objects#

class SavePath(str)

Spock special key for saving the Spock config to file

Defines a special key use to save the current Spock config to file

_extract_base_type#

def _extract_base_type(typed)

Extracts the name of the type from a _GenericAlias

Assumes that the derived types are only of length 1 as the args are [0] recursed... this is not true for tuples

Arguments:

  • typed - the type of the parameter

Returns:

name of type

_recursive_generic_validator#

def _recursive_generic_validator(typed)

Recursively assembles the validators for nested generic types

Walks through the nested type structure and determines whether to recurse all the way to a base type. Once it hits the base type it bubbles up the correct validator that is nested within the upper validator

Arguments:

  • typed - input type

Returns:

  • return_type - recursively built deep_iterable validators

_generic_alias_katra#

def _generic_alias_katra(typed, default=None, optional=False)

Private interface to create a subscripted generic_alias katra

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Handles: List[type], Tuple[type], Dict[type]

Arguments:

  • typed - the type of the parameter to define
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

_check_enum_props#

def _check_enum_props(typed)

Handles properties of enums

Checks if all types of the enum are the same and assembles a list of allowed values

Arguments:

  • typed - the type of parameter (Enum)

Returns:

  • base_type - the base type of the Enum
  • allowed - List of allowed values of the Enum

_enum_katra#

def _enum_katra(typed, default=None, optional=False)

Private interface to create a Enum typed katra

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Arguments:

  • typed - the type of the parameter to define
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

_cast_enum_default#

def _cast_enum_default(default)

Allows the enum default to be the specific value or the Enum structured value

Checks if enum type and extracts the value from the Enum

Arguments:

  • default - the default value to assign if given

Returns:

default value or the Enum extracted value

_enum_base_katra#

def _enum_base_katra(typed, base_type, allowed, default=None, optional=False)

Private interface to create a base Enum typed katra

Here we handle the base types of enums that allows us to force a type check on the instance

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Arguments:

  • typed - the type of the parameter to define
  • base_type - underlying base type
  • allowed - set of allowed values
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

_enum_class_katra#

def _enum_class_katra(typed, allowed, default=None, optional=False)

Private interface to create a base Enum typed katra

Here we handle the class based types of enums. Seeing as these classes are generated dynamically we cannot force type checking of a specific instance however the in_ validator will catch an incorrect instance type

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Arguments:

  • typed - the type of the parameter to define
  • allowed - set of allowed values
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

_type_katra#

def _type_katra(typed, default=None, optional=False)

Private interface to create a simple typed katra

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Handles: bool, string, float, int, List, and Tuple

Arguments:

  • typed - the type of the parameter to define
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

_handle_optional_typing#

def _handle_optional_typing(typed)

Handles when a type hint is Optional

Handles Optional[type] typing and strips out the base type to pass back to the creation of a katra which needs base typing

Arguments:

  • typed - type

Returns:

  • typed - type (modified if Optional)
  • optional - boolean for katra creation

_callable_katra#

def _callable_katra(typed, default=None, optional=False)

Private interface to create a Callable katra

Here we handle the callable type katra that allows us to force a callable check on the value provided

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Handles: callable

Arguments:

  • typed - the type of the parameter to define
  • default - the default value to assign if given
  • optional - whether to make the parameter optional or not (thus allowing None)

Returns:

  • x - Attribute from attrs

katra#

def katra(typed, default=None, inherit_optional=False)

Public interface to create a katra

A 'katra' is the basic functional unit of spock. It defines a parameter using attrs as the backend, type checks both simple types and subscripted GenericAlias types (e.g. lists and tuples), handles setting default parameters, and deals with parameter optionality

Arguments:

  • typed - the type of the parameter to define
  • default - the default value to assign if given
  • inherit_optional - optionality from inheritance

Returns:

  • x - Attribute from attrs