Skip to content
Snippets Groups Projects
Commit 6a6bd5b5 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Removed OpenTypedDict support. Would need more TypedDict methods decoupling to...

Removed OpenTypedDict support. Would need more TypedDict methods decoupling to avoid too much state keeping issues; also makes class/instance distinction too obscure to get users to use it correctly
parent 7a9bf04b
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# Copyright (c) 2016, CESNET, z. s. p. o. # Copyright (c) 2016, CESNET, z. s. p. o.
# Use of this source is governed by an ISC license, see LICENSE file. # Use of this source is governed by an ISC license, see LICENSE file.
from typedcols import TypedDict, OpenTypedDict, TypedList, KeyNotAllowed, KeysRequired, Discard, Any from typedcols import TypedDict, TypedList, KeyNotAllowed, KeysRequired, Discard, Any
from sys import version_info from sys import version_info
import unittest import unittest
...@@ -135,19 +135,6 @@ class TestTypedDict(unittest.TestCase): ...@@ -135,19 +135,6 @@ class TestTypedDict(unittest.TestCase):
]) ])
class TestOpenTypedDict(TestTypedDict):
def setUp(self):
open_address_dict = OpenTypedDict(typedef=address_typedef.copy(), allow_unknown=True)
open_person_typedef = person_typedef.copy()
open_person_typedef["address"] = open_address_dict
open_person_dict = OpenTypedDict(typedef=open_person_typedef)
self.person = open_person_dict(person_init_data)
class IntList(TypedList): class IntList(TypedList):
item_type = int item_type = int
......
...@@ -10,7 +10,7 @@ Defines TypedDict and TypedList, which enforce inserted types based on simple ...@@ -10,7 +10,7 @@ Defines TypedDict and TypedList, which enforce inserted types based on simple
type definition. type definition.
""" """
__version__ = '0.1.12' __version__ = '0.1.13'
__author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>' __author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>'
import collections import collections
...@@ -61,7 +61,7 @@ class TypedDictMetaclass(abc.ABCMeta): ...@@ -61,7 +61,7 @@ class TypedDictMetaclass(abc.ABCMeta):
dictify_typedef(cls.typedef) dictify_typedef(cls.typedef)
class TypedDictBase(collections.MutableMapping): class TypedDict(collections.MutableMapping):
""" Dictionary type abstract class, which supports checking of inserted """ Dictionary type abstract class, which supports checking of inserted
types, based on simple type definition. types, based on simple type definition.
...@@ -151,7 +151,7 @@ class TypedDictBase(collections.MutableMapping): ...@@ -151,7 +151,7 @@ class TypedDictBase(collections.MutableMapping):
for key, tdef in self.typedef.items(): for key, tdef in self.typedef.items():
if tdef.get("required", False) and not key in self.data: if tdef.get("required", False) and not key in self.data:
missing = missing + ((key,),) missing = missing + ((key,),)
elif recursive and (isinstance(tdef["type"], TypedDictBase) or issubclass(tdef["type"], TypedDictBase)): elif recursive and issubclass(tdef["type"], TypedDict):
try: try:
self.data[key].checkRequired(recursive) self.data[key].checkRequired(recursive)
except KeysRequired as e: except KeysRequired as e:
...@@ -219,70 +219,7 @@ class TypedDictBase(collections.MutableMapping): ...@@ -219,70 +219,7 @@ class TypedDictBase(collections.MutableMapping):
# Py 2 requires metaclassing by __metaclass__ attribute, whereas Py 3 # Py 2 requires metaclassing by __metaclass__ attribute, whereas Py 3
# needs metaclass argument. What actually happens is the following, # needs metaclass argument. What actually happens is the following,
# so we will do it explicitly, to be compatible with both versions. # so we will do it explicitly, to be compatible with both versions.
TypedDict = TypedDictMetaclass("TypedDict", (TypedDictBase,), {}) TypedDict = TypedDictMetaclass("TypedDict", (TypedDict,), {})
class TypedefSetter(object):
""" Setter for OpenTypedDict.typedef value, which forces typedef canonicalisation.
Implemented as setter only class, as it does not intercept and slow down read
access.
"""
def __set__(self, obj, value):
dictify_typedef(value)
obj.__dict__["typedef"] = value
class OpenTypedDict(TypedDictBase):
""" Dictionary type class, which supports checking of inserted types, based on
simple type definition, which must be provided in constructor and is changeable
by assigning instance.typedef variable.
Note however that changing already populated OpenTypedDict's typedef to
incompatible definition may lead to undefined results and data inconsistent
with definition.
"""
def __init__(self, init_data=None, typedef=None, allow_unknown=False, dict_class=dict):
""" init_data: initial values
typedef: dictionary with keys and their type definitions. Type definition
may be simple callable (int, string, check_func,
AnotherTypedDict), or dict with the following members:
"type":
type enforcing callable. If callable returns, raises
or is Discard, key will be silently discarded
"default":
new TypedDict subclass will be initialized with keys
with this value; deleted keys will also revert to it
"required":
bool, checkRequired method will report the key if not present
"description":
string, explaining field type in human readable terms
(will be used in exception explanations)
Type enforcing callable must take one argument, and return value,
coerced to expected type. Coercion may even be conversion, for example
arbitrary date string, converted to DateTime.
allow_unknown: boolean, specifies whether dictionary allows unknown keys,
that means keys, which are not defined in 'typedef'
dict_class: class or factory for underlying dict implementation
"""
self.allow_unknown = allow_unknown
self.dict_class = dict_class
self.typedef = typedef or {}
super(OpenTypedDict, self).__init__(init_data)
typedef = TypedefSetter()
def __call__(self, data):
""" Instances are made callable so they can be used in nested "type"
definitions. Note however that these classes are mutable, so
assigning new values replaces old ones.
"""
super(OpenTypedDict, self).__init__(data)
return self
class TypedList(collections.MutableSequence): class TypedList(collections.MutableSequence):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment