parasite.any

Brief

Reference for the any submodule of the parasite package. This submodule contains the parasite.any.Any_ class, which is a generic container for any Python object.

Usage

from parasite import p

schema = p.any()
schema.parse(1)  # 1
schema.parse('hello')  # 'hello'
...

schema = p.obj({ "payload": p.any().optional() })
schema.parse({ "payload": 1 })  # { "payload": 1 }
schema.parse({ })  # { }
...

Member Reference

class parasite.any.K

Template type for the key in a dictionary.

alias of TypeVar(‘K’)

class parasite.any.Any_[source]

Parasite type for representing any values. This is the default type, when no other type is specified.

Note

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

from parasite import p

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

Inheritance diagram of parasite.any.Any_

optional() Any_[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:

Any_

Example usage:

Lets assume we have the following schemas:

from parasite import p

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

The resulting schemas will parse the following objects:

>>> schema.parse({ "name": "John" })
{ "name": "John" }
>>> schema.parse({ })
{ }

>>> schema2.parse({ "name": "John" })
{ "name": "John" }
>>> schema2.parse({ })
ValidationError: key "name" not found, but is required
required() Any_[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:

Any_

Example usage:

Lets assume we have the following schemas:

from parasite import p

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

The resulting schemas will parse the following objects:

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

>>> schema2.parse({ "name": "John" })
{ "name": "John" }
>>> schema2.parse({ })
ValidationError: key "name" not found, but is required
parse(obj: Any) Any[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]