Skip to content

Context

faststream.utils.context.types.Context #

Context(
    real_name: str = "",
    *,
    cast: bool = False,
    default: Any = _empty,
    prefix: str = ""
)

Bases: CustomField

A class to represent a context.

METHOD DESCRIPTION
__init__

constructor method

use

method to use the context

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Initialize the object.

PARAMETER DESCRIPTION
real_name

The real name of the object.

TYPE: str DEFAULT: ''

cast

Whether to cast the object.

TYPE: bool DEFAULT: False

default

The default value of the object.

TYPE: Any DEFAULT: _empty

RAISES DESCRIPTION
TypeError

If the default value is not provided.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/utils/context/types.py
def __init__(
    self,
    real_name: str = "",
    *,
    cast: bool = False,
    default: Any = _empty,
    prefix: str = "",
):
    """Initialize the object.

    Args:
        real_name: The real name of the object.
        cast: Whether to cast the object.
        default: The default value of the object.

    Raises:
        TypeError: If the default value is not provided.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    self.name = real_name
    self.default = default
    self.prefix = prefix
    super().__init__(
        cast=cast,
        required=(default is _empty),
    )

cast instance-attribute #

cast: bool = cast

default instance-attribute #

default = default

name instance-attribute #

name = real_name

param_name instance-attribute #

param_name: str

prefix instance-attribute #

prefix = prefix

required instance-attribute #

required: bool = required

set_param_name #

set_param_name(name: str) -> Cls
Source code in fast_depends/library/model.py
def set_param_name(self: Cls, name: str) -> Cls:
    self.param_name = name
    return self

use #

use(**kwargs: Any) -> AnyDict

Use the given keyword arguments.

PARAMETER DESCRIPTION
**kwargs

Keyword arguments to be used

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
AnyDict

A dictionary containing the updated keyword arguments

RAISES DESCRIPTION
KeyError

If the parameter name is not found in the keyword arguments

AttributeError

If the parameter name is not a valid attribute

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/utils/context/types.py
def use(self, **kwargs: Any) -> AnyDict:
    """Use the given keyword arguments.

    Args:
        **kwargs: Keyword arguments to be used

    Returns:
        A dictionary containing the updated keyword arguments

    Raises:
        KeyError: If the parameter name is not found in the keyword arguments
        AttributeError: If the parameter name is not a valid attribute

    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    name = f"{self.prefix}{self.name or self.param_name}"

    try:
        kwargs[self.param_name] = resolve_context(name)
    except (KeyError, AttributeError):
        if self.required is False:
            kwargs[self.param_name] = self.default

    return kwargs

Last update: 2023-11-13