parasite.null

Brief

Reference for the null submodule of the parasite package. This submodule contains the parasite.null.Null class, which is a generic container for a Python None object.

Usage

from parasite import p

schema = p.null()
schema.parse(None)  # -> None
...

Member Reference

class parasite.null.K

Template type for the key in a dictionary.

alias of TypeVar(‘K’)

class parasite.null.Null[source]

parasite schema for None values.

Note

Please use p.null() instead of instantiating this class directly. p can be imported with:

from parasite import p

schema = p.null()
...
Inheritance:

Inheritance diagram of parasite.null.Null

optional() Null[source]

Makes the value optional, when parsing with _find_and_parse(). Has no effect on parse(). Inverse of required().

Warning

This function has no effect if the value is parsed as a standalone value.

Returns:

The updated instance of the class.

Return type:

Null

Example usage:

Lets assume we have the following schemas:

from parasite import p

schema = p.obj({ "null": p.null().optional() })
schema2 = p.obj({ "null": p.null() })

The resulting schemas will parse the following objects:

>>> schema.parse({ "null": None })
{ "null": None }
>>> schema.parse({ })
{ }

>>> schema2.parse({ "null": None })
{ "null": None }
>>> schema2.parse({ })
ValidationError: key "null" not found, but is required
required() Null[source]

Makes the value required, when parsing with _find_and_parse(). Has no effect on parse(). Inverse of optional(). Default behavior.

Note

This function is default behavior for the class and therefore only has an effect if the function optional() may have been called before.

Warning

This function has no effect if the value is parsed as a standalone value.

Returns:

The updated instance of the class.

Return type:

Null

Example usage:

Lets assume we have the following schemas:

from parasite import p

schema = p.obj({ "null": p.null().optional().required() })
schema2 = p.obj({ "null": p.null() })

The resulting schemas will parse the following objects:

>>> schema.parse({ "null": None })
{ "null": None }
>>> schema.parse({ })
ValidationError: key "null" not found, but is required

>>> schema2.parse({ "null": None })
{ "null": None }
>>> schema2.parse({ })
ValidationError: key "null" not found, but is required
parse(obj: Any) None[source]

Default method for parsing a value. This method should be overridden by subclasses.

Throws:

ValidationError: if the value could not be parsed or was invalid

Parameters:

obj (Any) – value to parse

Returns:

parsed destination value

Return type:

T

parse_safe(obj: Any) Result[T, ValidationError]

Converts the result of parse() into a rusttypes.result.Result type. Should be used when safe parsing is required.

Note

Will only catch parasite.errors.ValidationError exceptions!!!

Parameters:

obj (Any) – value to parse

Returns:

parsed destination value or an error

Return type:

Result[T, ValidationError]