wtypes.base

An extended type and trait system for python.

Notes

Module Contents

wtypes.base.__version__ = 0.0.1
wtypes.base.ValidationError
class wtypes.base._Implementation

An implementation of the pluggy wtypes spec.

Notes

The implementation needs to be registered with the plugin manager.

validate_type(type)
validate_object(object, schema)
wtypes.base.istype(object, cls)

instance(object, type) and issubclass(object, cls)

Examples

>>> assert istype(int, int)
>>> assert not istype(10, int)
class wtypes.base._NoTitle

A subclass suppresses the class name when combining schema

class wtypes.base._NoInit

A subclass to restrict initializing an object from the type.

class wtypes.base._ContextMeta

Bases: abc.ABCMeta

Meta operations for the context..

_context

The schema the object validates against.

Type:dict
_type

The schema the object validates against.

Type:type
_schema
Type:dict

Notes

A context type cannot be verified as it only describes, althrough some descriptors like SHACL can validate

_schema
_context
_type
_merge_args(cls)
_merge_context(cls)
_merge_annotations(cls)

Merge annotations from the module resolution order.

_merge_schema(cls)

Merge schema from the module resolution order.

_merge_types(cls)

Merge schema from the module resolution order.

__matmul__(cls, object)
validate(cls, object)

A context type does not validate.

__instancecheck__(cls, object)
create(cls, name: str, **schema)

Create a new schema type.

Parameters:
  • name (str) – The title of the new type/schema
  • **schema (dict) – Extra features to add include in the schema.
Returns:

Return type:

type

__add__(cls, object)
__neg__(cls)

The Not version of a type.

__pos__(cls)

The type.

__and__(cls, object)

AllOf the conditions

__sub__(cls, object)

AnyOf the conditions

__or__(cls, object)

OneOf the conditions

class wtypes.base._SchemaMeta

Bases: wtypes.base._ContextMeta

Meta operations for wtypes.

The _SchemaMeta ensures that a type’s extended schema is validate. Types cannot be generated with invalid schema.

_schema

The schema the object validates against.

Type:dict
__neg__(cls)

The Not version of a type.

__pos__(cls)

The type.

__and__(cls, object)

AllOf the conditions

__sub__(cls, object)

AnyOf the conditions

__or__(cls, object)

OneOf the conditions

validate(cls, object)

Validate an object against type’s schema.

Note

isinstance can used for validation, too.

Parameters:object – An object to validate.
Raises:jsonschema.ValidationError – The jsonschema module validation throws an exception on failure, otherwise the returns a None type.
class wtypes.base._ConstType

Bases: wtypes.base._SchemaMeta

ConstType permits bracketed syntax for defining complex types.

Note

The bracketed notebook should differeniate actions on types versus those on objects.

__getitem__(cls, object)
class wtypes.base._ContainerType

Bases: wtypes.base._ConstType

ContainerType extras schema from bracketed arguments to define complex types.

__getitem__(cls, object)
wtypes.base._python_to_wtype(object)
wtypes.base._get_schema_from_typeish(object, key='anyOf')

infer a schema from an object.

wtypes.base._lower_key(str)
wtypes.base._object_to_webtype(object)
wtypes.base._construct_title(cls)
class wtypes.base.Trait

A trait is an object validated by a validate jsonschema.

_schema
_context
classmethod _resolve_defaults(cls, *args, **kwargs)
wtypes.base.get_jawn(thing, key, object)
class wtypes.base.Description

Bases: wtypes.base._NoInit, wtypes.base.Trait, wtypes.base._NoTitle

An empty type with a description

Examples

>>> yo = Description['yo']
>>> yo._schema.toDict()
{'description': 'yo'}
class wtypes.base.Examples

Bases: wtypes.base._NoInit, wtypes.base.Trait

class wtypes.base.Default

Bases: wtypes.base._NoInit, wtypes.base.Trait

class wtypes.base.Title

Bases: wtypes.base._NoInit, wtypes.base.Trait, wtypes.base._NoTitle

An empty type with a title

Examples

>>> holla = Title['holla']
>>> holla._schema.toDict()
{'title': 'holla'}
class wtypes.base.Const

Bases: wtypes.base._NoInit, wtypes.base.Trait

A constant

Examples

>>> Const[10]._schema.toDict()
{'const': 10}
>>> assert isinstance('thing', Const['thing'])
>>> assert not isinstance('jawn', Const['thing']), "Because the compiler is from Philly."
class wtypes.base.Bool

Bases: wtypes.base.Trait

Boolean type

Examples

>>> Bool(), Bool(True), Bool(False)
(False, True, False)
>>> assert (Bool + Default[True])()

Note

It is not possible to base class bool so object creation is customized.

class wtypes.base.Null

Bases: wtypes.base.Trait

nil, none, null type

Examples

>>> Null(None)
>>> assert (Null + Default[None])() is None
class wtypes.base._NumericSchema

Bases: wtypes.base._SchemaMeta

Meta operations for numerical types

__rgt__
__rge__
__rlt__
__rle__
__ge__(cls, object)

Inclusive minimum

__gt__(cls, object)

Exclusive minimum

__le__(cls, object)

Inclusive maximum

__lt__(cls, object)

Exclusive maximum

__truediv__(cls, object)

multiple of a number

class wtypes.base.Integer

Bases: wtypes.base.Trait, int

integer type

Examples

>>> assert isinstance(10, Integer)
>>> assert not isinstance(10.1, Integer)
>>> (Integer+Default[9])(9)
9
>>> bounded = (10< Integer)< 100
>>> bounded._schema.toDict()
{'type': 'integer', 'exclusiveMinimum': 10, 'exclusiveMaximum': 100}
>>> assert isinstance(12, bounded)
>>> assert not isinstance(0, bounded)
>>> assert (Integer/3)(9) == 9
class wtypes.base.Float

Bases: wtypes.base.Trait, float

float type

>>> assert isinstance(10, Float)
>>> assert isinstance(10.1, Float)

Symbollic conditions.

>>> bounded = (10< Float)< 100
>>> bounded._schema.toDict()
{'type': 'number', 'exclusiveMinimum': 10, 'exclusiveMaximum': 100}
>>> assert isinstance(12.1, bounded)
>>> assert not isinstance(0.1, bounded)

Multiples

>>> assert (Float+MultipleOf[3])(9) == 9
class wtypes.base.MultipleOf

Bases: wtypes.base._NoInit, wtypes.base.Trait

A multipleof constraint for numeric types.

class wtypes.base.Minimum

Bases: wtypes.base._NoInit, wtypes.base.Trait

A minimum constraint for numeric types.

class wtypes.base.ExclusiveMinimum

Bases: wtypes.base._NoInit, wtypes.base.Trait

A exclusive minimum constraint for numeric types.

class wtypes.base.Maximum

Bases: wtypes.base._NoInit, wtypes.base.Trait

A exclusive maximum constraint for numeric types.

class wtypes.base.ExclusiveMaximum

Bases: wtypes.base._NoInit, wtypes.base.Trait

A exclusive maximum constraint for numeric types.

class wtypes.base.Properties

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Object properties.

class wtypes.base._ObjectSchema

Bases: wtypes.base._SchemaMeta

Meta operations for the object schema.

__getitem__(cls, object)

Examples

>>> Dict[wtypes.Forward[range], int].__annotations__
{'': typing.Union[abc.Forward, int]}
>>> Dict[wtypes.Forward[range], int]._schema.toDict()
{'type': 'object', 'properties': {}, 'additionalProperties': {'anyOf': [{'type': 'integer'}]}}
class wtypes.base._Object

Base class for validating object types.

classmethod __init_subclass__(cls, **schema)
classmethod from_config_file(cls, *object)
class wtypes.base.Dict

Bases: wtypes.base.Trait, dict, wtypes.base._Object

dict type

Examples

>>> assert istype(Dict, __import__('collections').abc.MutableMapping)
>>> assert (Dict + Default[{'b': 'foo'}])() == {'b': 'foo'}
>>> assert (Dict + Default[{'b': 'foo'}])({'a': 'bar'}) == {'b': 'foo', 'a': 'bar'}
>>> assert isinstance({}, Dict)
>>> assert not isinstance([], Dict)
>>> assert isinstance({'a': 1}, Dict + Required['a',])
>>> assert not isinstance({}, Dict + Required['a',])
>>> assert not isinstance({'a': 'b'}, Dict[Integer, Float])
>>> assert Dict[Integer]({'a': 1}) == {'a': 1}
>>> Dict[{'a': int}]._schema.toDict()
{'type': 'object', 'properties': {'a': {'type': 'integer'}}}
>>> Dict[{'a': int}]({'a': 1})
{'a': 1}
__setitem__(self, key, object)

Only test the key being set to avoid invalid state.

update(self, *args, **kwargs)
class wtypes.base.Bunch

Bases: wtypes.base.Dict, munch.Munch

Bunch type

Examples

>>> Bunch[{'a': int}]._schema.toDict()
{'type': 'object', 'properties': {'a': {'type': 'integer'}}}
>>> Bunch[{'a': int}]({'a': 1}).toDict()
{'a': 1}
class wtypes.base.AdditionalProperties

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Additional object properties.

class wtypes.base.Required

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Required properties.

class wtypes.base.minProperties

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Minimum number of properties.

class wtypes.base.maxProperties

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Maximum number of properties.

class wtypes.base.PropertyNames

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Propery name constraints.

class wtypes.base.Dependencies

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Properties dependencies.

class wtypes.base.PatternProperties

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Pattern properties names.

class wtypes.base._StringSchema

Bases: wtypes.base._SchemaMeta

Meta operations for strings types.

__mod__(cls, object)

A pattern string type.

__gt__(cls, object)

Minumum string length

__lt__(cls, object)

Maximum string length

class wtypes.base.String

Bases: wtypes.base.Trait, str

string type.

Examples

>>> assert isinstance('abc', String)
>>> assert (String+Default['abc'])() == 'abc'

String patterns

>>> assert isinstance('abc', String%"^a")
>>> assert not isinstance('abc', String%"^b")

String constraints

>>> assert isinstance('abc', (2<String)<10)
>>> assert not isinstance('a', (2<String)<10)
>>> assert not isinstance('a'*100, (2<String)<10)
class wtypes.base.MinLength

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Minimum length of a string type.

class wtypes.base.MaxLength

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Maximum length of a string type.

class wtypes.base.ContentMediaType

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Content type of a string.

class wtypes.base.Pattern

Bases: wtypes.base.Trait, wtypes.base._NoInit

A regular expression pattern.

class wtypes.base.Format

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

class wtypes.base._ListSchema

Bases: wtypes.base._SchemaMeta

Meta operations for list types.

__getitem__(cls, object)

List meta operations for bracketed type notation.

Examples

>>> List[wtypes.Forward[range], int]._type
typing.List[typing.Union[abc.Forward, int]]
>>> Tuple[wtypes.Forward[range], int]._type
typing.Tuple[abc.Forward, int]
__gt__(cls, object)

Minumum array length

__lt__(cls, object)

Maximum array length

class wtypes.base.List

Bases: wtypes.base.Trait, list

List type

Examples

List

>>> assert isinstance([], List)
>>> assert not isinstance({}, List)

Typed list

>>> assert List[Integer]([1, 2, 3])
>>> assert isinstance([1], List[Integer])
>>> assert not isinstance([1.1], List[Integer])
>>> List[Integer, String]._schema.toDict()
{'type': 'array', 'items': {'anyOf': [{'type': 'integer'}, {'type': 'string'}]}}

Tuple

>>> assert List[Integer, String]([1, 'abc', 2])
>>> assert isinstance([1, '1'], List[Integer, String])
>>> assert not isinstance([1, {}], List[Integer, String])
_verify_item(self, object, id=None)

Elemental verification for interactive type checking.

__setitem__(self, id, object)
append(self, object)
insert(self, id, object)
extend(self, object)
pop(self, index=-1)
class wtypes.base.Unique

Bases: wtypes.base.List

Unique list type

Examples

>>> assert Unique(list('abc'))
>>> assert isinstance([1,2], Unique)
>>> assert not isinstance([1,1], Unique)
class wtypes.base.Tuple

Bases: wtypes.base.List

tuple type

Note

There are no tuples in json, they are typed lists.

>>> assert Tuple._schema == List._schema

Examples

>>> assert isinstance([1,2], Tuple)
>>> assert Tuple[Integer, String]([1, 'abc'])
>>> Tuple[Integer, String]._schema.toDict()
{'type': 'array', 'items': [{'type': 'integer'}, {'type': 'string'}]}
>>> assert isinstance([1,'1'], Tuple[Integer, String])
>>> assert not isinstance([1,1], Tuple[Integer, String])
class wtypes.base.UniqueItems

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Schema for unique items in a list.

class wtypes.base.Contains

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

class wtypes.base.Items

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

class wtypes.base.AdditionalItems

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

class wtypes.base.MinItems

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Minimum length of an array.

class wtypes.base.MaxItems

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

Maximum length of an array.

class wtypes.base.Enum

Bases: wtypes.base.Trait

An enumerate type that is restricted to its inputs.

Examples

>>> assert Enum['cat', 'dog']('cat')
>>> assert isinstance('cat', Enum['cat', 'dog'])
>>> assert not isinstance('🐢', Enum['cat', 'dog'])
class wtypes.base.ContentEncoding

Bases: Enum['7bit 8bit binary quoted-printable base64'.split()], wtypes.base._NoInit, wtypes.base._NoTitle

Content encodings for a string.

class wtypes.base.If

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

if condition type

class wtypes.base.Then

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

then condition type

class wtypes.base.Else

Bases: wtypes.base.Trait, wtypes.base._NoInit, wtypes.base._NoTitle

else condition type