state#

class starlite.datastructures.state.ImmutableState#

Bases: Mapping[str, Any]

An object meant to store arbitrary state.

It can be accessed using dot notation while exposing dict like functionalities.

__init__(state: Union[ImmutableState, Dict[str, Any], Iterable[Tuple[str, Any]]], deep_copy: bool = True) None#

Initialize an ImmutableState instance.

Parameters:
  • state – An object to initialize the state from. Can be a dict, an instance of ‘ImmutableState’, or a tuple of key value paris.

  • deep_copy – Whether to ‘deepcopy’ the passed in state.

Examples

__bool__() bool#

Return a boolean indicating whether the wrapped dict instance has values.

__getitem__(key: str) Any#

Get the value for the corresponding key from the wrapped state object using subscription notation.

Parameters:

key – Key to access.

Raises:

KeyError

Returns:

A value from the wrapped state instance.

__iter__() Iterator[str]#

Return an iterator iterating the wrapped state dict.

Returns:

An iterator of strings

__len__() int#

Return length of the wrapped state dict.

Returns:

An integer

__getattr__(key: str) Any#

Get the value for the corresponding key from the wrapped state object using attribute notation.

Parameters:

key – Key to retrieve

Raises:

AttributeError – if the given attribute is not set.

Returns:

The retrieved value

__copy__() ImmutableState#

Return a shallow copy of the given state object.

Customizes how the builtin “copy” function will work.

mutable_copy() State#

Return a mutable copy of the state object.

Returns:

A State

dict() Dict[str, Any]#

Return a shallow copy of the wrapped dict.

Returns:

A dict

classmethod __get_validators__() Generator[Callable[[Union[ImmutableState, Dict[str, Any], Iterable[Tuple[str, Any]]]], ImmutableState], None, None]#

Pydantic compatible method to allow custom parsing of state instances in a SignatureModel.

classmethod validate(value: Union[ImmutableState, Dict[str, Any], Iterable[Tuple[str, Any]]]) ImmutableState#

Parse a value and instantiate state inside a SignatureModel. This allows us to use custom subclasses of state, as well as allows users to decide whether state is mutable or immutable.

Parameters:

value – The value from which to initialize the state instance.

Returns:

An ImmutableState instance

class starlite.datastructures.state.State#

Bases: ImmutableState, MutableMapping[str, Any]

An object meant to store arbitrary state.

It can be accessed using dot notation while exposing dict like functionalities.

__init__(state: Optional[Union[ImmutableState, Dict[str, Any], Iterable[Tuple[str, Any]]]] = None, deep_copy: bool = False) None#

Initialize a State instance with an optional value.

Parameters:
  • state – An object to initialize the state from. Can be a dict, an instance of ‘ImmutableState’, or a tuple of key value paris.

  • deep_copy – Whether to ‘deepcopy’ the passed in state.

Examples: .. code-block: python

from starlite import State

state_dict = {“first”: 1, “second”: 2, “third”: 3, “fourth”: 4} state = State(state_dict)

# state can be accessed using ‘.’ notation assert state.fourth == 4 del state.fourth

# state implements the Mapping type: assert len(state) == 3 assert “first” in state assert not “fourth” in state assert state[“first”] == 1 assert [(k, v) for k, v in state.items()] == [(“first”, 1), (“second”, 2), (“third”, 3)]

state[“fourth”] = 4 assert “fourth” in state del state[“fourth”]

# state implements __bool__ assert state # state is true when it has values. assert not State() # state is empty when it has no values.

# it has shallow copy copied_state = state.copy() del copied_state.first assert state.first

# it has a ‘dict’ method to retrieve a shallow copy of the underlying dict inner_dict = state.dict() assert inner_dict == state_dict

# you can get an immutable copy of the state by calling ‘immutable_immutable_copy’ immutable_copy = state.immutable_copy() del immutable_copy.first # raises AttributeError

__delitem__(key: str) None#

Delete the value from the key from the wrapped state object using subscription notation.

Parameters:

key – Key to delete

Raises:

KeyError – if the given attribute is not set.

Returns:

None

__setitem__(key: str, value: Any) None#

Set an item in the state using subscription notation.

Parameters:
  • key – Key to set.

  • value – Value to set.

Returns:

None

__setattr__(key: str, value: Any) None#

Set an item in the state using attribute notation.

Parameters:
  • key – Key to set.

  • value – Value to set.

Returns:

None

__delattr__(key: str) None#

Delete the value from the key from the wrapped state object using attribute notation.

Parameters:

key – Key to delete

Raises:

AttributeError – if the given attribute is not set.

Returns:

None

copy() State#

Return a shallow copy of the state object.

Returns:

A State

immutable_copy() ImmutableState#

Return a shallow copy of the state object, setting it to be frozen.

Returns:

A State