Skip to content

Commit

Permalink
Integrating attrs, and typing for all versions of Python
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Dec 17, 2017
1 parent ac8e6c2 commit fcc38c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
12 changes: 3 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@
README_PATH = os.path.join(base, "README.rst")

install_requires = [
'attrs',
'attrs>=17.3.0',
'cattrs>=0.5.0',
'jsonschema-extractor>=0.6.0',
'schematics>=2.0.0',
'six',
'swagger-schema>=0.4.0',
'pyyaml',
]

if sys.version_info >= (3, 5, 4):
# these libraries only support 3.5 reliably.
install_requires.append([
"cattrs >=0.4.0",
"jsonschema-extractor"
])


tests_require = []

data_files = []
Expand Down
38 changes: 19 additions & 19 deletions transmute_core/tests/test_attrs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import pytest
import sys

pytest.importorskip("cattr")

import attr
import cattr
import json
import pytest
import sys
import mock

from attr.validators import instance_of
from mock import Mock
from transmute_core.exceptions import SerializationException
from typing import List
from cattr import typed
from transmute_core.object_serializers.cattrs_serializer import CattrsSerializer
from transmute_core import describe, annotate, TransmuteFunction
from .utils import execute
Expand All @@ -27,18 +26,18 @@ def __init__(self, name, score):

@attr.s
class Card(object):
name = typed(str, default="")
price = typed(float, default=1.0)
name = attr.ib(type=str, default="")
price = attr.ib(type=float, default=1.0)

@attr.s
class Hand(object):
cards = typed(List[Card])
cards = attr.ib(type=List[Card])


@attr.s
class Person(object):
age = typed(int)
bio = typed(str, default="")
age = attr.ib(type=int)
bio = attr.ib(type=str, default="")

@age.validator
def greater_than_zero(self, attribute, value):
Expand All @@ -60,10 +59,11 @@ def test_attrs_integration_dump(cattrs_serializer, typ, inp, out):
assert cattrs_serializer.dump(typ, inp) == out


def test_attrs_integration_dump_exception(monkeypatch, cattrs_serializer):
def test_attrs_integration_dump_exception(cattrs_serializer):
def mock_return(inp):
raise ValueError("Random_Exception")
monkeypatch.setattr(cattrs_serializer._cattrs_converter, "unstructure", mock_return)
raise ValueError("Random Exception")
cattrs_serializer._cattrs_converter = Mock()
cattrs_serializer._cattrs_converter.unstructure = mock_return
with pytest.raises(SerializationException):
assert cattrs_serializer.dump(str, "random_str")

Expand Down Expand Up @@ -128,15 +128,15 @@ class UserAttrs(object):

@attr.s
class UserAttrs(object):
name = typed(str)
age = typed(int)
name = attr.ib(type=str)
age = attr.ib(type=int)


@attr.s
class ComplexModelAttrs(object):
user = typed(UserAttrs)
description = typed(str)
is_allowed = typed(bool)
user = attr.ib(type=UserAttrs)
description = attr.ib(type=str)
is_allowed = attr.ib(type=bool)


@describe(paths="/foo", body_parameters="body")
Expand Down
1 change: 1 addition & 0 deletions ubuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def main(build):
@uranium.task_requires("main")
def test(build):
""" execute the unit tests. """
build.packages.install("mock")
build.packages.install("pytest")
build.packages.install("pytest-benchmark")
build.packages.install("pytest-cov")
Expand Down

0 comments on commit fcc38c7

Please sign in to comment.