myst_parser.config.dc_validators#

Validators for dataclasses, mirroring those of python-attrs/attrs.

1.  Module Contents#

1.1.  Classes#

1.2.  Functions#

validate_field

Validate the field of a dataclass, according to a validator function set in the field.metadata.

validate_fields

Validate the fields of a dataclass, according to validator functions set in the field metadata.

any_

A validator that does not perform any validation.

instance_of

A validator that raises a TypeError if the initializer is called with a wrong type for this particular attribute (checks are performed using isinstance therefore it’s also valid to pass a tuple of types).

optional

A validator that makes an attribute optional. An optional attribute is one which can be set to None in addition to satisfying the requirements of the sub-validator.

is_callable

A validator that raises a TypeError if the initializer is called with a value for this particular attribute that is not callable.

in_

A validator that raises a ValueError if the initializer is called with a value that does not belong in the options provided. The check is performed using value in options.

deep_iterable

A validator that performs deep validation of an iterable.

deep_mapping

A validator that performs deep validation of a dictionary.

1.3.  API#

myst_parser.config.dc_validators.validate_field(inst: Any, field: dataclasses.Field, value: Any) None[source]#

Validate the field of a dataclass, according to a validator function set in the field.metadata.

The validator function should take as input (inst, field, value) and raise an exception if the value is invalid.

myst_parser.config.dc_validators.validate_fields(inst: Any) None[source]#

Validate the fields of a dataclass, according to validator functions set in the field metadata.

This function should be called in the __post_init__ of the dataclass.

The validator function should take as input (inst, field, value) and raise an exception if the value is invalid.

class myst_parser.config.dc_validators.ValidatorType[source]#

Bases: typing.Protocol

myst_parser.config.dc_validators.any_(inst, field, value, suffix='')[source]#

A validator that does not perform any validation.

myst_parser.config.dc_validators.instance_of(type_: type[Any] | tuple[type[Any], ...]) myst_parser.config.dc_validators.ValidatorType[source]#

A validator that raises a TypeError if the initializer is called with a wrong type for this particular attribute (checks are performed using isinstance therefore it’s also valid to pass a tuple of types).

Parameters:

type – The type to check for.

myst_parser.config.dc_validators.optional(validator: myst_parser.config.dc_validators.ValidatorType) myst_parser.config.dc_validators.ValidatorType[source]#

A validator that makes an attribute optional. An optional attribute is one which can be set to None in addition to satisfying the requirements of the sub-validator.

myst_parser.config.dc_validators.is_callable(inst, field, value, suffix='')[source]#

A validator that raises a TypeError if the initializer is called with a value for this particular attribute that is not callable.

myst_parser.config.dc_validators.in_(options: collections.abc.Sequence) myst_parser.config.dc_validators.ValidatorType[source]#

A validator that raises a ValueError if the initializer is called with a value that does not belong in the options provided. The check is performed using value in options.

Parameters:

options – Allowed options.

myst_parser.config.dc_validators.deep_iterable(member_validator: myst_parser.config.dc_validators.ValidatorType, iterable_validator: myst_parser.config.dc_validators.ValidatorType | None = None) myst_parser.config.dc_validators.ValidatorType[source]#

A validator that performs deep validation of an iterable.

Parameters:
  • member_validator – Validator to apply to iterable members

  • iterable_validator – Validator to apply to iterable itself

myst_parser.config.dc_validators.deep_mapping(key_validator: myst_parser.config.dc_validators.ValidatorType, value_validator: myst_parser.config.dc_validators.ValidatorType, mapping_validator: myst_parser.config.dc_validators.ValidatorType | None = None) myst_parser.config.dc_validators.ValidatorType[source]#

A validator that performs deep validation of a dictionary.

Parameters:
  • key_validator – Validator to apply to dictionary keys

  • value_validator – Validator to apply to dictionary values

  • mapping_validator – Validator to apply to top-level mapping attribute (optional)