parasite.boolean

Brief

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

Usage

from parasite import p

schema = p.boolean()
schema.parse(True)  # True
schema.parse(False)  # False
schema.parse(1)  # ValidationError: Expected a boolean, got 1
...

schema = p.boolean().leaniant()
schema.parse(1)  # True
schema.parse("true")  # True
...

Member Reference

class parasite.boolean.K

Template type for the key in a dictionary.

alias of TypeVar(‘K’)

class parasite.boolean.Boolean[source]

Parasite type for representing boolean values.

Note

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

from parasite import p

schema = p.boolean()
...

Warning

When using the leaniant mode (see leaniant()), the regular expressions are case-insensitive. This means that you only need to handle lowercase variations of your regex. This also means that the regex can only handle case-insensitive cases.

Inheritance:

Inheritance diagram of parasite.boolean.Boolean

optional() Boolean[source]

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

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

from parasite import p

schema = p.obj({"name": p.boolean()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({})  # -> ValidationError: key "name" not found, but is required

schema = p.obj({"name": p.boolean().optional()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({})  # -> { }
required() Boolean[source]

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

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

from parasite import p

schema = p.obj({"name": p.boolean()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({})  # -> ValidationError: key "name" not found, but is required

schema = p.obj({"name": p.boolean().required()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({})  # -> ValidationError: key "name" not found, but is required
nullable() Boolean[source]

Set the value to be nullable.

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

from parasite import p

schema = p.obj({"name": p.boolean()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({"name": None})  # -> ValidationError: key "name" cannot be None

schema = p.obj({"name": p.boolean().nullable()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({"name": None})  # -> { "name": None }
not_nullable() Boolean[source]

Set the value to be not nullable.

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

from parasite import p

schema = p.obj({"name": p.boolean()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({"name": None})  # -> ValidationError: key "name" cannot be None

schema = p.obj({"name": p.boolean().not_nullable()})
schema.parse({"name": True})  # -> { "name": True }
schema.parse({"name": None})  # -> ValidationError: key "name" cannot be None
literal(value: bool) Boolean[source]

Set the literal value of the boolean.

Parameters:

value (bool) – The literal value of the boolean.

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

from parasite import p

schema = p.boolean()
schema.parse(True)  # -> True
schema.parse(False)  # -> False

schema = p.boolean().literal(True)
schema.parse(True)  # -> True
schema.parse(False)  # -> ValidationError: object has to be True, but is False
leaniant(re_true: str | None = None, re_false: str | None = None) Boolean[source]

Set the value to be leaniant. This allows the value to be read from a string or a number. As numbers only 0 (converts to: False) and 1 (converts to: True) are accepted.

Note

All string input in leaniant mode is converted to lowercase before, so you only need to handle lowercase variations of your regex. By default the following regular expressions are used:

re_true = r"^(true|1|yes|y)$"
re_false = r"^(false|0|no|n)$"
Parameters:
  • re_true (Optional[str]) – The regular expression for the true value. Default: None

  • re_false (Optional[str]) – The regular expression for the false value. Default: None

Returns:

The updated instance of the class.

Return type:

Boolean

Example usage:

Lets assume we have the following schemas:

from parasite import p

schema = p.boolean().leaniant()
schema2 = p.boolean()

The resulting schemas will parse the following objects:

>>> schema.parse(True)
True
>>> schema.parse("true")
True
>>> schema.parse("false")
False
>>> schema.parse(1)
True

>>> schema2.parse(True)
True
>>> schema2.parse("true")
ValidationError: object has to be a boolean, but is 'true'
>>> schema2.parse(1)
ValidationError: object has to be a boolean, but is '1'
parse(obj: Any) bool[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]