MCPcopy Create free account
hub / github.com/Materials-Consortia/optimade-python-tools / StrictField

Function StrictField

optimade/models/utils.py:40–123  ·  view source on GitHub ↗

A wrapper around `pydantic.Field` that does the following: - Forbids any "extra" keys that would be passed to `pydantic.Field`, except those used elsewhere to modify the schema in-place, e.g. "uniqueItems", "pattern" and those added by OptimadeField, e.g. "unit", "queryable" a

(
    default: "Any" = PydanticUndefined,
    *,
    description: str | None = None,
    optimade_version: str | None = None,
    **kwargs: "Any",
)

Source from the content-addressed store, hash-verified

38
39
40def StrictField(
41 default: "Any" = PydanticUndefined,
42 *,
43 description: str | None = None,
44 optimade_version: str | None = None,
45 **kwargs: "Any",
46) -> Any:
47 """A wrapper around `pydantic.Field` that does the following:
48
49 - Forbids any "extra" keys that would be passed to `pydantic.Field`,
50 except those used elsewhere to modify the schema in-place,
51 e.g. "uniqueItems", "pattern" and those added by OptimadeField, e.g.
52 "unit", "queryable" and "sortable".
53 - Emits a warning when no description is provided.
54
55 Arguments:
56 default: The only non-keyword argument allowed for Field.
57 description: The description of the `Field`; if this is not
58 specified then a `UserWarning` will be emitted.
59 optimade_version: A PEP version specifier indicating which OPTIMADE API version
60 this field is required for.
61 **kwargs: Extra keyword arguments to be passed to `Field`.
62
63 Raises:
64 RuntimeError: If `**kwargs` contains a key not found in the
65 function signature of `Field`, or in the extensions used
66 by models in this package (see above).
67
68 Returns:
69 The pydantic `Field`.
70
71 """
72 allowed_schema_and_field_keys = ["pattern"]
73
74 allowed_keys = [
75 "pattern",
76 "uniqueItems",
77 ] + OPTIMADE_SCHEMA_EXTENSION_KEYS
78 _banned = [k for k in kwargs if k not in set(_PYDANTIC_FIELD_KWARGS + allowed_keys)]
79
80 if _banned:
81 raise RuntimeError(
82 f"Not creating StrictField({default!r}, **{kwargs!r}) with "
83 f"forbidden keywords {_banned}."
84 )
85
86 # Handle description
87 if description is None:
88 warnings.warn(
89 f"No description provided for StrictField specified by {default!r}, "
90 f"**{kwargs!r}."
91 )
92 else:
93 kwargs["description"] = description
94
95 # OPTIMADE schema extensions
96 json_schema_extra: dict[str, Any] = kwargs.pop("json_schema_extra", {})
97

Callers 15

OptimadeErrorClass · 0.90
WarningsClass · 0.90
ResponseMetaQueryClass · 0.90
ProviderClass · 0.90
ImplementationClass · 0.90
ResponseMetaClass · 0.90
SuccessClass · 0.90
RelationshipClass · 0.90
LinkClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected