Primitives

Primitive Types represent the types that are found all over the Internet, like in JSON, and implements the basics:

  • bool

  • str

  • int

  • float.

Here is a boolean validator.

>>> true_or_false = types.BooleanType()
>>> true_or_false.validate(True)
True
>>> true_or_false.validate(1)
True
>>> true_or_false.validate("")
False

Here is an integer validator.

>>> int_type = types.IntegerType()
>>> int_type.validate(123)
>>> int_type.validate("foo")
...
typerighter.exceptions.ValidationException: Value doesnt match type format foo
class typerighter.types.primitives.BooleanType(*a, **kw)[source]

This validator implements booleans as falsy values. This is done by passing the value directly into Python’s bool and using Python’s native behavior.

NATIVE

alias of bool

to_primitive(value)[source]

Attempts to convert the input value into a Python bool.

validate_boolean(value)[source]

Confirms the value is compatible with bool.

class typerighter.types.primitives.FloatType(*a, **kw)[source]

A Number implementation based on Python float.

NATIVE

alias of float

class typerighter.types.primitives.IntegerType(*a, **kw)[source]

A Number implementation based on Python int.

NATIVE

alias of int

class typerighter.types.primitives.Number(*a, **kw)[source]

Used for tracking the functionality common to numbers. The current implementation simply supports ranges of numbers.

class typerighter.types.primitives.Primitive(*a, **kw)[source]

A Primitive is the first Type that validates data by looking at its contents. It is intended as a common base class among primitive types and is not intended for regular use.

It extends validation by checking if input values match a list of possible choices.

It inherits object as its native type, allowing any data to pass validation.

validate_choices(value)[source]

Checks if a choices list has been set and then if value is in that list.

class typerighter.types.primitives.StringType(*a, **kw)[source]

A type that captures the core needs for validating strings, which can then be extended in subclasses for validating specific types of strings.

Validation can be extended by using min_length or max_length, or by providing a regular expression compatible with Python’s re module.

NATIVE

alias of str

is_falsy(value)[source]

Checks a value and responds saying whether the Type considers it falsy.

Parameters

value (object) – The value to inspect

Returns

True or False

to_primitive(value)[source]

Converts the value to str.