typing#
- class litestar.typing.FieldDefinition#
Bases:
object
Represents a function parameter or type annotation.
- args: tuple[Any, ...]#
The result of calling
get_args(annotation)
after unwrapping Annotated, e.g. (int,).
- instantiable_origin: Any#
An equivalent type to
origin
that can be safely instantiated. E.g.,Sequence
->list
.
- safe_generic_origin: Any#
An equivalent type to
origin
that can be safely used as a generic type across all supported Python versions.This is to serve safely rebuilding a generic outer type with different args at runtime.
- inner_types: tuple[litestar.typing.FieldDefinition, ...]#
The type’s generic args parsed as
FieldDefinition
, if applicable.
- kwarg_definition: KwargDefinition | DependencyKwarg | None#
Kwarg Parameter.
- property has_default: bool#
Check if the field has a default value.
- Returns:
True if the default is not Empty or Ellipsis otherwise False.
- property is_non_string_iterable: bool#
Check if the field type is an Iterable.
If
self.annotation
is an optional union, only the non-optional members of the union are evaluated.
- property is_non_string_sequence: bool#
Check if the field type is a non-string Sequence.
If
self.annotation
is an optional union, only the non-optional members of the union are evaluated.
- __init__(raw: Any, annotation: Any, type_wrappers: tuple[type, ...], origin: Any, args: tuple[Any, ...], metadata: tuple[Any, ...], instantiable_origin: Any, safe_generic_origin: Any, inner_types: tuple[litestar.typing.FieldDefinition, ...], default: Any, extra: dict[str, Any], kwarg_definition: KwargDefinition | DependencyKwarg | None, name: str) None #
- property is_non_string_collection: bool#
Whether the annotation is a non-string collection type or not.
- property bound_types: tuple[litestar.typing.FieldDefinition, ...] | None#
A tuple of bound types - if the annotation is a TypeVar with bound types, otherwise None.
- property generic_types: tuple[litestar.typing.FieldDefinition, ...] | None#
A tuple of generic types passed into the annotation - if its generic.
- property type_: Any#
The type of the annotation with all the wrappers removed, including the generic types.
- is_subclass_of(cl: type[Any] | tuple[type[Any], ...]) bool #
Whether the annotation is a subclass of the given type.
Where
self.annotation
is a union type, this method will returnTrue
when all members of the union are a subtype ofcl
, otherwise,False
.- Parameters:
cl – The type to check, or tuple of types. Passed as 2nd argument to
issubclass()
.- Returns:
Whether the annotation is a subtype of the given type(s).
- has_inner_subclass_of(cl: type[Any] | tuple[type[Any], ...]) bool #
Whether any generic args are a subclass of the given type.
- Parameters:
cl – The type to check, or tuple of types. Passed as 2nd argument to
issubclass()
.- Returns:
Whether any of the type’s generic args are a subclass of the given type.
- get_type_hints(*, include_extras: bool = False, resolve_generics: bool = False) dict[str, Any] #
Get the type hints for the annotation.
- Parameters:
include_extras – Flag to indicate whether to include
Annotated[T, ...]
or not.resolve_generics – Flag to indicate whether to resolve the generic types in the type hints or not.
- Returns:
The type hints.
- classmethod from_annotation(annotation: Any, **kwargs: Any) FieldDefinition #
Initialize FieldDefinition.
- Parameters:
annotation – The type annotation. This should be extracted from the return of
get_type_hints(..., include_extras=True)
so that forward references are resolved and recursiveAnnotated
types are flattened.**kwargs – Additional keyword arguments to pass to the
FieldDefinition
constructor.
- Returns:
FieldDefinition
- classmethod from_kwarg(annotation: Any, name: str, default: Any = _EmptyEnum.EMPTY, inner_types: tuple[litestar.typing.FieldDefinition, ...] | None = None, kwarg_definition: KwargDefinition | DependencyKwarg | None = None, extra: dict[str, Any] | None = None) FieldDefinition #
Create a new FieldDefinition instance.
- Parameters:
annotation – The type of the kwarg.
name – Field name.
default – A default value.
inner_types – A tuple of FieldDefinition instances representing the inner types, if any.
kwarg_definition – Kwarg Parameter.
extra – A mapping of extra values.
- Returns:
FieldDefinition instance.
- classmethod from_parameter(parameter: Parameter, fn_type_hints: dict[str, Any]) FieldDefinition #
Initialize ParsedSignatureParameter.
- Parameters:
parameter – inspect.Parameter
fn_type_hints – mapping of names to types. Should be result of
get_type_hints()
, preferably via the :attr:get_fn_type_hints() <.utils.signature_parsing.get_fn_type_hints>
helper.
- Returns:
ParsedSignatureParameter.
- match_predicate_recursively(predicate: Callable[[FieldDefinition], bool]) bool #
Recursively test the passed in predicate against the field and any of its inner fields.
- Parameters:
predicate – A callable that receives a field definition instance as an arg and returns a boolean.
- Returns:
A boolean.