Type Validation#
Oblate provides robust type validation of basic type expressions. This page documents the machinery of type validation system.
Type validation example#
Type validation is provided by various fields. The most common examples are data structure fields
such as fields.TypedDict or fields.Dict etc. The fields.TypeExpr field
is used to validate raw type expressions.
Apart from fields, the oblate.validate_types() function is used to perform type
validation on a set of given values:
import typing
import oblate
types = {
'name': str,
'id': typing.Union[int, str],
}
oblate.validate_types(types, {'name': 'John', 'id': 2}) # no error
In case the validation fails, a special exception, oblate.TypeValidationError is
raised. The errors attribute is the dictionary including
the detail of error:
try:
oblate.validate_types(types, {'name': 1})
except oblate.TypeValidationError as e:
print(e.errors)
Error:
{
'name': ['Must be of type str'],
'id': ['This key is missing.']
}
Few parameters can be passed to this function to modify its behaviour. These are:
ignore_extra: Whether to ignore any “extra” keys passed to value mapping.ignore_missing: Whether to ignore keys that are missing (such asidin above example).
All these parameters are, by default, False.
Supported types and limitations#
Currently, only following types are supported:
typing.Uniontyping.Optionaltyping.Literaltyping.Anytyping.Sequencetyping.Listtyping.Settyping.Tupletyping.Dicttyping.Mappingtyping.TypedDicttyping.Required(with typed dicts only)typing.NotRequired(with typed dicts only)
Type expressions involving only these types will be validated fully. If any type expression involves an unsupported type, it will not be validated. A warning will be issued by the library for usage of an unsupported type.
To suppress unsupported type warning, set GlobalConfig.warn_unsupported_type warning to False.