Ejemplo n.º 1
0
 def test_eq(self):
     """
     All instances are equal.
     """
     assert _Nothing() == _Nothing() == NOTHING
     assert not (_Nothing() != _Nothing())
     assert 1 != _Nothing()
Ejemplo n.º 2
0
 def test_eq(self):
     """
     All instances are equal.
     """
     assert _Nothing() == _Nothing() == NOTHING
     assert not (_Nothing() != _Nothing())
     assert 1 != _Nothing()
Ejemplo n.º 3
0
 def test_deepcopy(self):
     """
     __deepcopy__ returns the same object.
     """
     n = _Nothing()
     assert n is copy.deepcopy(n)
Ejemplo n.º 4
0
 def test_deepcopy(self):
     """
     __deepcopy__ returns the same object.
     """
     n = _Nothing()
     assert n is copy.deepcopy(n)
from attr._make import _Nothing
NO_ATTR_DEFAULT = _Nothing()

JSON2ATTR_TYPES = {
    'boolean': [bool],
    'array': [list],
    'number': [float, int],
    'string': [str],
}
KNOWN_ATTR_TYPES = set(sum(JSON2ATTR_TYPES.values(), []))

def make_json_schema(attr_class, title, attr_subclasses=None, description=None, schema_url='http://json-schema.org/draft-04/schema#'):
    properties = {}
    required = []
    subclasses_to_define = set()
    for attribute in attr_class.__attrs_attrs__:
        prop = properties[attribute.name] = {}
        if attribute.convert in KNOWN_ATTR_TYPES:
            prop["type"] = next(type for type, validators in JSON2ATTR_TYPES.items() if attribute.convert in validators)
        elif attribute.validator:
            if attribute.validator.type in KNOWN_ATTR_TYPES:
                prop["type"] = next(type for type, validators in JSON2ATTR_TYPES.items() if attribute.validator.type in validators)
            elif attribute.validator.type in attr_subclasses:
                prop = properties[attribute.name] = {"$ref": "#/definitions/" + attribute.validator.type.__name__}
                subclasses_to_define.add(attribute.validator.type)
        if attribute.default == NO_ATTR_DEFAULT:
            required.append(attribute.name)
        else:
            prop["default"] = attribute.default
    schema = {
        "type": "object",