Skip to content

Context

faststream.utils.context.types.Context #

Context(real_name='', *, default=EMPTY, initial=None, cast=False, prefix='')

Bases: CustomField

A class to represent a context.

ATTRIBUTE DESCRIPTION
param_name

name of the parameter

METHOD DESCRIPTION
__init__

constructor method

use

method to use the context

Initialize the object.

PARAMETER DESCRIPTION
real_name

The real name of the object.

TYPE: str DEFAULT: ''

default

The default value of the object.

TYPE: Any DEFAULT: EMPTY

initial

The initial value builder.

TYPE: Optional[Callable[..., Any]] DEFAULT: None

cast

Whether to cast the object.

TYPE: bool DEFAULT: False

prefix

The prefix to be added to the name of the object.

TYPE: str DEFAULT: ''

RAISES DESCRIPTION
TypeError

If the default value is not provided.

Source code in faststream/utils/context/types.py
def __init__(
    self,
    real_name: str = "",
    *,
    default: Any = EMPTY,
    initial: Optional[Callable[..., Any]] = None,
    cast: bool = False,
    prefix: str = "",
) -> None:
    """Initialize the object.

    Args:
        real_name: The real name of the object.
        default: The default value of the object.
        initial: The initial value builder.
        cast: Whether to cast the object.
        prefix: The prefix to be added to the name of the object.

    Raises:
        TypeError: If the default value is not provided.
    """
    self.name = real_name
    self.default = default
    self.prefix = prefix
    self.initial = initial
    super().__init__(
        cast=cast,
        required=(default is EMPTY),
    )

cast instance-attribute #

cast = cast

required instance-attribute #

required = required

field instance-attribute #

field = False

param_name instance-attribute #

param_name

name instance-attribute #

name = real_name

default instance-attribute #

default = default

prefix instance-attribute #

prefix = prefix

initial instance-attribute #

initial = initial

set_param_name #

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

use_field #

use_field(kwargs)
Source code in fast_depends/library/model.py
def use_field(self, kwargs: Dict[str, Any]) -> None:
    raise NotImplementedError("You should implement `use_field` method.")

use #

use(**kwargs)

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

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
    """
    name = f"{self.prefix}{self.name or self.param_name}"

    if EMPTY != (  # noqa: SIM300
        v := resolve_context_by_name(
            name=name,
            default=self.default,
            initial=self.initial,
        )
    ):
        kwargs[self.param_name] = v

    else:
        kwargs.pop(self.param_name, None)

    return kwargs