fqr.objects.fields.obj module

Field module.

class Field(class_as_dict: dict[string[snake_case], Any] | None = None, /, *, type_: type | type[Type] | type[AnyType] = None, default: Any | Type | AnyType = None, required: bool = False, enum: Array[Immutable] | lib.enum.EnumMeta = None, min_length: int = None, max_length: int = None, minimum: float = None, exclusive_minimum: bool = False, maximum: float = None, exclusive_maximum: bool = False, multiple_of: float = None, pattern: str = None, min_items: int = None, max_items: int = None, unique_items: bool = False, read_only: bool = False, write_only: bool = False, **kwargs: Any)

Bases: Object, Generic[AnyType]

Simple field object.

Querying

Queries for Objects can be generated from their fields using the following comparison operators:

  • field_1_eq_filter = Object.field_1 == 'test_value_123'

  • field_1_ne_filter = Object.field_1 != 'test_value_123'

  • field_1_ge_filter = Object.field_1 >= 'test_value_123'

  • field_1_gt_filter = Object.field_1 > 'test_value_123'

  • field_1_le_filter = Object.field_1 <= 'test_value_123'

  • field_1_lt_filter = Object.field_1 < 'test_value_123'

And the following special operators:

  • field_1_contains_filter = Object.field_1 << 'test_value_123'

  • field_1_similarity_filter = Object.field_1 % 'test_value_123'

  • field_1_similarity_filter_with_threshold = Object.field_1 % ('test_value_123', 0.8)

Queries may be chained together using the & and | bitwise operators, corresponding to and and or clauses respectively.

Additionally, the invert (~) operator may be prefixed to any Query to match the opposite of any conditions specified instead.

Queries also support optional result limiting and sorting:

  • Result limits can be specified by setting the limit field.

  • Results can be sorted any number of times using the += and -= operators.

Example

query: Query = (
    (
        (Object.integer_field >= 1)
        | (Object.string_field % ('test', 0.75))
        )
    & ~(Object.list_field << 'test')
    ) += 'string_field' -= 'integer_field'

In the example above, the query would match any Object for which the string 'test' is not a member of list_field and for which either the value for integer_field is greater than or equal to 1 or the value for string_field is at least 75% similar to 'test'. Results would then be sorted first in ascending order on string_field, then in descending order on integer_field.

Parameters

Specify parameters to constrain values allowed for the field and control its behavior.

name: str = None

Field Name. Sourced from / overwritten by attribute name.

type: type[lib.t.Any] = None

Type of value. Sourced from / overwritten by type annotation.

default: lib.t.Any = None

Default value for field. Sourced from / overwritten by attribute value. MUST be an instance of field type or None.

required: bool = False

Whether or not the field SHOULD be required. Default behavior changes to assume True if no attribute value is specified for the field.

enum: deque | frozenset | list | tuple | set | Enum = None

Sequence of which field value SHOULD be a member, unless "*" is included in the sequence, in which case ANY value MAY be allowed, in addition to those explicitly specified. None is assumed to be allowed if the field is nullable.

min_length: int = None

Specify len(value) SHOULD be >= minimum. Field type MUST be str if specified.

max_length: int = None

Specify len(value) SHOULD be <= maximum. Field type MUST be str if specified.

minimum: float = None

Specify value SHOULD be >= minimum. Field type MUST be numeric if specified.

exclusive_minimum: bool = False

Set True to specify value SHOULD be > minimum. Field minimum MUST also be specified.

maximum: float = None

Specify value SHOULD be <= maximum. Field type MUST be numeric if specified.

exclusive_maximum: bool = False

Set True to specify value SHOULD be < maximum. Field maximum MUST also be specified.

multiple_of: float = None

Specify value % multiple_of SHOULD be 0. Field type MUST be numeric if specified.

pattern: str = None

Specify a Regex pattern for which the value SHOULD match. Field type MUST be str if specified.

min_items: int = None

Specify len(value) SHOULD be >= min_items. Field type MUST be deque | frozenset | list | tuple | set if specified.

max_items: int = None

Specify len(value) SHOULD be <= max_items. Field type MUST be deque | frozenset | list | tuple | set if specified.

unique_items: bool = False

Specify all elements of value SHOULD be unique. Field type MUST be deque | frozenset | list | tuple | set if specified.

read_only: bool = False

Specify this field SHOULD only be available to read operations (like GET http calls).

write_only: bool = False

Specify this field SHOULD only be available to write operations (like PATCH, POST, or PUT http calls).

name: Field[str]
type_: Field[type[typ.AnyType]]
default: Field[typ.AnyType]
required: Field[bool]
enum: Field[typ.Enum]
min_length: Field[int]
max_length: Field[int]
minimum: Field[float]
exclusive_minimum: Field[bool]
maximum: Field[float]
exclusive_maximum: Field[bool]
multiple_of: Field[float]
pattern: Field[str]
min_items: Field[int]
max_items: Field[int]
unique_items: Field[bool]
read_only: Field[bool]
write_only: Field[bool]
enumerations: lib.t.ClassVar[dict[str, tuple[typ.Primitive, ...]]] = {}
fields: lib.t.ClassVar[typ.FieldsTuple] = ('default', 'enum', 'exclusive_maximum', 'exclusive_minimum', 'max_items', 'max_length', 'maximum', 'min_items', 'min_length', 'minimum', 'multiple_of', 'name', 'pattern', 'read_only', 'required', 'type_', 'unique_items', 'write_only')
hash_fields: lib.t.ClassVar[typ.FieldsTuple] = ('name',)
class_as_dict: lib.t.Final[lib.t.Optional[dict[typ.string[typ.snake_case], lib.t.Any]]] = {}

Instantiate class directly from passed dict (assumed to be version of class in dict form).

_validate_comparison(value: Any) Never | None
_validate_iterable_comparison(value: Any) Never | None
parse(value: Any, raise_validation_error: bool = True) AnyType | None | Never

Return correctly typed value if possible, None otherwise, or [optionally] raise an error if an invalid value is passed, the method’s default behavior.

property factory: Callable[[], AnyType]

Return callable returning default value for field.