wtypes¶
extended python types for the web and json
Notes
-
wtypes.simpleTypes¶ The limited set of base types provide by jsonschema.
Type: list
Subpackages¶
Submodules¶
Package Contents¶
-
wtypes.__version__= 0.0.2¶
-
wtypes.specification¶
-
wtypes.implementation¶
-
wtypes.manager¶
-
class
wtypes.spec¶ -
validate_type(type)¶ A hook to validate types.
-
validate_object(object, schema)¶ A hook to validate types.
-
-
wtypes.istype(object, cls)¶ instance(object, type) and issubclass(object, cls)
Examples
>>> assert istype(int, int) >>> assert not istype(10, int)
-
wtypes.validate_schema(object: object, schema: dict) → None¶
-
wtypes.validate_generic(object, cls)¶
-
wtypes.__version__= 0.0.1
-
wtypes.ValidationError¶
-
class
wtypes._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.istype(object, cls) instance(object, type) and issubclass(object, cls)
Examples
>>> assert istype(int, int) >>> assert not istype(10, int)
-
class
wtypes._NoTitle¶ A subclass suppresses the class name when combining schema
-
class
wtypes._NoInit¶ A subclass to restrict initializing an object from the type.
-
class
wtypes._ContextMeta¶ Bases:
abc.ABCMetaMeta 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._SchemaMeta¶ Bases:
wtypes.base._ContextMetaMeta operations for wtypes.
The
_SchemaMetaensures 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
isinstancecan used for validation, too.Parameters: object – An object to validate. Raises: jsonschema.ValidationError– Thejsonschemamodule validation throws an exception on failure, otherwise the returns a None type.
-
-
class
wtypes._ConstType¶ Bases:
wtypes.base._SchemaMetaConstType 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._ContainerType¶ Bases:
wtypes.base._ConstTypeContainerType extras schema from bracketed arguments to define complex types.
-
__getitem__(cls, object)¶
-
-
wtypes._python_to_wtype(object)¶
-
wtypes._get_schema_from_typeish(object, key='anyOf')¶ infer a schema from an object.
-
wtypes._lower_key(str)¶
-
wtypes._object_to_webtype(object)¶
-
wtypes._construct_title(cls)¶
-
class
wtypes.Trait¶ A trait is an object validated by a validate
jsonschema.-
_schema¶
-
_context¶
-
classmethod
_resolve_defaults(cls, *args, **kwargs)¶
-
-
wtypes.get_jawn(thing, key, object)¶
-
class
wtypes.Description¶ Bases:
wtypes.base._NoInit,wtypes.base.Trait,wtypes.base._NoTitleAn empty type with a description
Examples
>>> yo = Description['yo'] >>> yo._schema.toDict() {'description': 'yo'}
-
class
wtypes.Examples¶ Bases:
wtypes.base._NoInit,wtypes.base.Trait
-
class
wtypes.Default¶ Bases:
wtypes.base._NoInit,wtypes.base.Trait
-
class
wtypes.Title¶ Bases:
wtypes.base._NoInit,wtypes.base.Trait,wtypes.base._NoTitleAn empty type with a title
Examples
>>> holla = Title['holla'] >>> holla._schema.toDict() {'title': 'holla'}
-
class
wtypes.Const¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA 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.Bool¶ Bases:
wtypes.base.TraitBoolean type
Examples
>>> Bool(), Bool(True), Bool(False) (False, True, False) >>> assert (Bool + Default[True])()
Note
It is not possible to base class
boolso object creation is customized.
-
class
wtypes.Null¶ Bases:
wtypes.base.Traitnil, none, null type
Examples
>>> Null(None) >>> assert (Null + Default[None])() is None
-
class
wtypes._NumericSchema¶ Bases:
wtypes.base._SchemaMetaMeta 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.Integer¶ Bases:
wtypes.base.Trait,intinteger 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.Float¶ Bases:
wtypes.base.Trait,floatfloat 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.MultipleOf¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA multipleof constraint for numeric types.
-
class
wtypes.Minimum¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA minimum constraint for numeric types.
-
class
wtypes.ExclusiveMinimum¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA exclusive minimum constraint for numeric types.
-
class
wtypes.Maximum¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA exclusive maximum constraint for numeric types.
-
class
wtypes.ExclusiveMaximum¶ Bases:
wtypes.base._NoInit,wtypes.base.TraitA exclusive maximum constraint for numeric types.
-
class
wtypes.Properties¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleObject properties.
-
class
wtypes._ObjectSchema¶ Bases:
wtypes.base._SchemaMetaMeta 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._Object¶ Base class for validating object types.
-
classmethod
__init_subclass__(cls, **schema)¶
-
classmethod
from_config_file(cls, *object)¶
-
classmethod
-
class
wtypes.Dict¶ Bases:
wtypes.base.Trait,dict,wtypes.base._Objectdict 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.Bunch¶ Bases:
wtypes.base.Dict,munch.MunchBunch type
Examples
>>> Bunch[{'a': int}]._schema.toDict() {'type': 'object', 'properties': {'a': {'type': 'integer'}}} >>> Bunch[{'a': int}]({'a': 1}).toDict() {'a': 1}
-
class
wtypes.AdditionalProperties¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleAdditional object properties.
-
class
wtypes.Required¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleRequired properties.
-
class
wtypes.minProperties¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMinimum number of properties.
-
class
wtypes.maxProperties¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMaximum number of properties.
-
class
wtypes.PropertyNames¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitlePropery name constraints.
-
class
wtypes.Dependencies¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleProperties dependencies.
-
class
wtypes.PatternProperties¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitlePattern properties names.
-
class
wtypes._StringSchema¶ Bases:
wtypes.base._SchemaMetaMeta 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.String¶ Bases:
wtypes.base.Trait,strstring 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.MinLength¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMinimum length of a string type.
-
class
wtypes.MaxLength¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMaximum length of a string type.
-
class
wtypes.ContentMediaType¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleContent type of a string.
-
class
wtypes.Pattern¶ Bases:
wtypes.base.Trait,wtypes.base._NoInitA regular expression pattern.
-
class
wtypes.Format¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes._ListSchema¶ Bases:
wtypes.base._SchemaMetaMeta 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.List¶ Bases:
wtypes.base.Trait,listList 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.Unique¶ Bases:
wtypes.base.ListUnique list type
Examples
>>> assert Unique(list('abc')) >>> assert isinstance([1,2], Unique) >>> assert not isinstance([1,1], Unique)
-
class
wtypes.Tuple¶ Bases:
wtypes.base.Listtuple 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.UniqueItems¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleSchema for unique items in a list.
-
class
wtypes.Contains¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes.Items¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes.AdditionalItems¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes.MinItems¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMinimum length of an array.
-
class
wtypes.MaxItems¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleMaximum length of an array.
-
class
wtypes.Enum¶ Bases:
wtypes.base.TraitAn 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.ContentEncoding¶ Bases:
Enum['7bit 8bit binary quoted-printable base64'.split()],wtypes.base._NoInit,wtypes.base._NoTitleContent encodings for a string.
-
class
wtypes.If¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleif condition type
-
class
wtypes.Then¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitlethen condition type
-
class
wtypes.Else¶ Bases:
wtypes.base.Trait,wtypes.base._NoInit,wtypes.base._NoTitleelse condition type
-
class
wtypes._NotType¶ Bases:
wtypes.base._ConstType-
validate(cls, object)¶
-
__getitem__(cls, object)¶
-
-
class
wtypes.Not¶ Bases:
wtypes.base.Traitnot schema.
Examples
>>> assert Not[wtypes.String](100) == 100 >>> assert not isinstance('abc', Not[wtypes.String])
Note
See the __neg__ method for symbollic not composition.
-
class
wtypes._AnyOfType¶ Bases:
wtypes.base._ConstType-
validate(cls, object)¶
-
__getitem__(cls, object)¶
-
-
class
wtypes.AnyOf¶ Bases:
wtypes.base.TraitanyOf combined schema.
Examples
>>> assert AnyOf[wtypes.Integer, wtypes.String]('abc') >>> assert isinstance(10, AnyOf[wtypes.Integer, wtypes.String]) >>> assert not isinstance([], AnyOf[wtypes.Integer, wtypes.String])
-
class
wtypes._AllOfType¶ Bases:
wtypes.base._ConstType-
validate(cls, object)¶
-
__getitem__(cls, object)¶
-
-
class
wtypes.AllOf¶ Bases:
wtypes.base.TraitallOf combined schema.
Examples
>>> assert AllOf[wtypes.Float>0, wtypes.Integer/3](9) >>> assert isinstance(9, AllOf[wtypes.Float>0, wtypes.Integer/3]) >>> assert not isinstance(-9, AllOf[wtypes.Float>0, wtypes.Integer/3])
-
class
wtypes._OneOfType¶ Bases:
wtypes.base._ConstType-
validate(cls, object)¶
-
__getitem__(cls, object)¶
-
-
class
wtypes.OneOf¶ Bases:
wtypes.base.TraitoneOf combined schema.
Examples
>>> assert OneOf[wtypes.Float>0, wtypes.Integer/3](-9) >>> assert isinstance(-9, OneOf[wtypes.Float>0, wtypes.Integer/3]) >>> assert not isinstance(9, OneOf[wtypes.Float>0, wtypes.Integer/3])
-
class
wtypes.Setter¶ -
__setattr__(self, key, object)¶ Only test the attribute being set to avoid invalid state.
-
-
class
wtypes.DataClass¶ Bases:
wtypes.dataclass.Setter,wtypes.Trait,wtypes.base._ObjectValidating dataclass type
Examples
>>> class q(DataClass): a: int >>> q._schema.toDict() {'type': 'object', 'properties': {'a': {'type': 'integer'}}, 'required': ['a']}
>>> q(a=10) q(a=10)
>>> assert not isinstance({}, q)
-
classmethod
__init_subclass__(cls, **kwargs)¶
-
classmethod
-
class
wtypes._NoType¶
-
class
wtypes._ForwardSchema¶ Bases:
wtypes.base._ContextMetaA forward reference to an object, the object must exist in sys.modules.
Notes
Python types live on the __annotations__ attribute.
-
_type_args¶
-
_type_kwargs¶
-
__getitem__(cls, object)¶
-
validate(cls, object)¶
-
eval(cls)¶
-
__add__(cls, object)¶
-
-
class
wtypes._ArgumentSchema¶ Bases:
wtypes.python_types._ForwardSchema-
__getitem__(cls, object)¶
-
-
class
wtypes.Args¶ Bases:
wtypes.python_types._NoType,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes.Kwargs¶ Bases:
wtypes.python_types._NoType,wtypes.base._NoInit,wtypes.base._NoTitle
-
class
wtypes.Forward¶ Create type using objects or forward references.
Examples
>>> assert Forward['builtins.range']() is range
-
class
wtypes.Class¶ Bases:
wtypes.python_types.ForwardCreate type using objects or forward references.
Examples
>>> assert isinstance(range, Class['builtins.range'])
-
classmethod
validate(cls, object)¶
-
classmethod
-
class
wtypes.Instance¶ Bases:
wtypes.python_types.ForwardCreate an instance of a type using objects or forward references.
Examples
>>> assert (Instance[range] + Args[10, 20])() == range(10, 20) >>> assert (Instance['builtins.range'] + Args[10, 20])() == range(10, 20) >>> assert isinstance(range(10), Instance['builtins.range'])
Deffered references.
>>> assert not isinstance(1, Instance['pandas.DataFrame']) >>> assert 'pandas' not in __import__('sys').modules
-
classmethod
validate(cls, object)¶
-
classmethod
-
class
wtypes.Color¶ Bases:
wtypes.String
-
class
wtypes.Datetime¶ Bases:
wtypes.String
-
class
wtypes.Time¶ Bases:
wtypes.String
-
class
wtypes.Date¶ Bases:
wtypes.String
-
class
wtypes.Email¶ Bases:
wtypes.String
-
class
wtypes.Idnemail¶ Bases:
wtypes.String
-
class
wtypes.Hostname¶ Bases:
wtypes.String
-
class
wtypes.Idnhostname¶ Bases:
wtypes.String
-
class
wtypes.Ipv4¶ Bases:
wtypes.String
-
class
wtypes.Ipv6¶ Bases:
wtypes.String
-
class
wtypes.Uri¶ Bases:
wtypes.String-
get¶
-
post¶
-
_httpx_method(self, method, *args, **kwargs)¶
-
-
class
wtypes.Urireference¶ Bases:
wtypes.String
-
class
wtypes.Iri¶ Bases:
wtypes.string_formats.Uri
-
class
wtypes.Irireference¶ Bases:
wtypes.String
-
class
wtypes.Uritemplate¶ Bases:
wtypes.String-
expand(self, **kwargs)¶
-
URITemplate(self)¶
-
-
class
wtypes.Jsonpointer¶ Bases:
wtypes.String-
resolve_pointer(self, doc, default=None)¶
-
-
class
wtypes.Relativejsonpointer¶ Bases:
wtypes.String
-
class
wtypes.Regex¶ Bases:
wtypes.String