typing#

class litestar.typing.FieldDefinition#

Bases: object

Represents a function parameter or type annotation.

raw: Any#

The annotation exactly as received.

annotation: Any#

The annotation with any “wrapper” types removed, e.g. Annotated.

type_wrappers: tuple[type, ...]#

A set of all “wrapper” types, e.g. Annotated.

origin: Any#

The result of calling get_origin(annotation) after unwrapping Annotated, e.g. list.

args: tuple[Any, ...]#

The result of calling get_args(annotation) after unwrapping Annotated, e.g. (int,).

metadata: tuple[Any, ...]#

Any metadata associated with the annotation via Annotated.

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.

default: Any#

Default value of the field.

extra: dict[str, Any]#

A mapping of extra values.

kwarg_definition: KwargDefinition | DependencyKwarg | None#

Kwarg Parameter.

name: str#

Field name.

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.

See: litestar-org/litestar#1106

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.

See: litestar-org/litestar#1106

property is_any: bool#

Check if the field type is Any.

property is_generic: bool#

Check if the field type is a custom class extending Generic.

property is_simple_type: bool#

Check if the field type is a singleton value (e.g. int, str etc.).

property is_parameter_field: bool#

Check if the field type is a parameter kwarg value.

property is_const: bool#

Check if the field is defined as constant value.

property is_required: bool#

Check if the field should be marked as a required parameter.

property is_annotated: bool#

Check if the field type is Annotated.

property is_literal: bool#

Check if the field type is Literal.

property is_forward_ref: bool#

Whether the annotation is a forward reference or not.

property is_mapping: bool#

Whether the annotation is a mapping or not.

property is_tuple: bool#

Whether the annotation is a tuple or not.

property is_type_alias_type: bool#

Whether the annotation is a TypeAliasType

property is_type_var: bool#

Whether the annotation is a TypeVar or not.

property is_union: bool#

Whether the annotation is a union type or not.

property is_optional: bool#

Whether the annotation is Optional or not.

property is_none_type: bool#

Whether the annotation is NoneType or not.

property is_collection: bool#

Whether the annotation is a collection type or not.

__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 is_dataclass_type: bool#

Whether the annotation is a dataclass type or not.

property is_typeddict_type: bool#

Whether the type is TypedDict or not.

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 return True when all members of the union are a subtype of cl, 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 recursive Annotated 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.