Context(
real_name="",
*,
default=EMPTY,
initial=None,
cast=False,
prefix="",
)
Bases: CustomField
A class to represent a context.
METHOD | DESCRIPTION |
__init__ | |
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),
)
|
param_name instance-attribute
default instance-attribute
prefix instance-attribute
initial instance-attribute
required instance-attribute
use
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
|
set_param_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
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.")
|