类型注解

class litestar.typing.FieldDefinition

基类:object

表示函数参数或类型注解。

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[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

检查该字段是否具有默认值。 Returns: 如果默认值不是 Empty 或 Ellipsis,则返回 True,否则返回 False。

property is_non_string_iterable: bool

检查字段类型是否为可迭代对象。 如果 self.annotation 是可选联合类型,则仅评估联合类型中的非可选成员。 参见:https://github.com/litestar-org/litestar/issues/1106

property is_non_string_sequence: bool

检查字段类型是否为非字符串序列。 如果 self.annotation 是可选联合类型,则仅评估联合类型中的非可选成员。 参见:https://github.com/litestar-org/litestar/issues/1106

property is_any: bool

检查字段类型是否为 Any。

property is_generic: bool

检查字段类型是否为扩展 Generic 的自定义类。

property is_simple_type: bool

检查字段类型是否为单例值(例如 int、str 等)。

property is_parameter_field: bool

检查字段类型是否为参数 kwarg 值。

property is_const: bool

检查该字段是否被定义为常量值。

property is_required: bool

检查该字段是否应标记为必需参数。

property is_annotated: bool

检查字段类型是否为 Annotated。

property is_literal: bool

检查字段类型是否为 Literal。

property is_forward_ref: bool

是否为前向引用。

property is_mapping: bool

是否为映射的注解。

property is_tuple: bool

是否为 tuple 类型的注解。

property is_type_alias_type: bool

是否注解为 TypeAliasType

property is_type_var: bool

是否为 TypeVar 注解。

property is_union: bool

是否为联合类型。

property is_optional: bool

是否将注解为 Optional。

property is_none_type: bool

是否为 NoneType 类型。

property is_collection: bool

是否为集合类型的注解。

property is_non_string_collection: bool

是否为非字符串集合类型的注解。

__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[FieldDefinition, ...], default: Any, extra: dict[str, Any], kwarg_definition: KwargDefinition | DependencyKwarg | None, name: str) None
property bound_types: tuple[FieldDefinition, ...] | None

如果注解是带有绑定类型的 TypeVar,则返回绑定类型的元组;否则为 None。

property generic_types: tuple[FieldDefinition, ...] | None

如果其是泛型,则传入注解的泛型类型元组。

property is_dataclass_type: bool

是否为 dataclass 类型的注解。

property is_typeddict_type: bool

是否为 TypedDict 类型。

property type_: Any

移除所有包装器(包括泛型类型)后的注解类型。

is_subclass_of(cl: type[Any] | tuple[type[Any], ...]) bool

检查该注解是否为给定类型的子类。 当 self.annotation 为联合类型时,如果联合类型中的所有成员都是 cl 的子类型,则此方法返回 True,否则返回 False。 Args: cl: 要检查的类型,或类型元组。作为 issubclass() 的第 2 个参数传入。 Returns: 该注解是否为给定类型(或类型集合)的子类型。

has_inner_subclass_of(cl: type[Any] | tuple[type[Any], ...]) bool

检查任何泛型参数是否为给定类型的子类。 Args: cl: 要检查的类型,或类型元组。作为第二个参数传递给 issubclass()。 Returns: 该类型的任何泛型参数是否为给定类型的子类。

get_type_hints(*, include_extras: bool = False, resolve_generics: bool = False) dict[str, Any]

获取注解的类型提示。 Args: include_extras: 标志,用于指示是否包含 Annotated[T, ...]。 resolve_generics: 标志,用于指示是否解析类型提示中的泛型类型。 Returns: 类型提示。

classmethod from_annotation(annotation: Any, **kwargs: Any) FieldDefinition

初始化 FieldDefinition。 Args: annotation: 类型注解。该注解应从 get_type_hints(..., include_extras=True) 的返回值中提取,以便解析前向引用并展平递归的 Annotated 类型。 **kwargs: 传递给 FieldDefinition 构造函数的其他关键字参数。 Returns: FieldDefinition

classmethod from_kwarg(annotation: Any, name: str, default: Any = _EmptyEnum.EMPTY, inner_types: tuple[FieldDefinition, ...] | None = None, kwarg_definition: KwargDefinition | DependencyKwarg | None = None, extra: dict[str, Any] | None = None) FieldDefinition

创建一个新的 FieldDefinition 实例。 Args: annotation: 关键字参数的类型。 name: 字段名称。 default: 默认值。 inner_types: 一个包含 FieldDefinition 实例的元组,用于表示内部类型(如果有的话)。 kwarg_definition: 关键字参数参数。 extra: 额外值的映射。 Returns: FieldDefinition 实例。

classmethod from_parameter(parameter: Parameter, fn_type_hints: dict[str, Any]) FieldDefinition

初始化 ParsedSignatureParameter。 Args: parameter: inspect.Parameter fn_type_hints: 名称到类型的映射。应为 get_type_hints() 的结果,最好通过 :attr:get_fn_type_hints() <.utils.signature_parsing.get_fn_type_hints> 辅助函数获取。 Returns: ParsedSignatureParameter。

match_predicate_recursively(predicate: Callable[[FieldDefinition], bool]) bool

递归地对传入的字段及其任何内部字段测试给定的谓词。 Args: predicate: 一个可调用对象,接收一个字段定义实例作为参数,并返回一个布尔值。 Returns: 一个布尔值。