feat: new alternate syntax and switch to black + isort (yeah, maybe not the best time, but that is done).
This commit is contained in:
@ -1 +1 @@
|
||||
foo = 'bar'
|
||||
foo = "bar"
|
||||
|
||||
@ -16,49 +16,49 @@ from bonobo.util.bags import BagType
|
||||
### Named Tuples
|
||||
################################################################################
|
||||
|
||||
TBag = BagType('TBag', ('x', 'y', 'z')) # type used for pickle tests
|
||||
TBag = BagType("TBag", ("x", "y", "z")) # type used for pickle tests
|
||||
|
||||
|
||||
class TestBagType(unittest.TestCase):
|
||||
def _create(self, *fields, typename='abc'):
|
||||
def _create(self, *fields, typename="abc"):
|
||||
bt = BagType(typename, fields)
|
||||
assert bt._fields == fields
|
||||
assert len(bt._fields) == len(bt._attrs)
|
||||
return bt
|
||||
|
||||
def test_factory(self):
|
||||
Point = BagType('Point', ('x', 'y'))
|
||||
self.assertEqual(Point.__name__, 'Point')
|
||||
Point = BagType("Point", ("x", "y"))
|
||||
self.assertEqual(Point.__name__, "Point")
|
||||
self.assertEqual(Point.__slots__, ())
|
||||
self.assertEqual(Point.__module__, __name__)
|
||||
self.assertEqual(Point.__getitem__, tuple.__getitem__)
|
||||
assert Point._fields == ('x', 'y')
|
||||
assert Point._attrs == ('x', 'y')
|
||||
assert Point._fields == ("x", "y")
|
||||
assert Point._attrs == ("x", "y")
|
||||
|
||||
self.assertRaises(ValueError, BagType, 'abc%', ('efg', 'ghi')) # type has non-alpha char
|
||||
self.assertRaises(ValueError, BagType, 'class', ('efg', 'ghi')) # type has keyword
|
||||
self.assertRaises(ValueError, BagType, '9abc', ('efg', 'ghi')) # type starts with digit
|
||||
self.assertRaises(ValueError, BagType, "abc%", ("efg", "ghi")) # type has non-alpha char
|
||||
self.assertRaises(ValueError, BagType, "class", ("efg", "ghi")) # type has keyword
|
||||
self.assertRaises(ValueError, BagType, "9abc", ("efg", "ghi")) # type starts with digit
|
||||
|
||||
assert self._create('efg', 'g%hi')._attrs == ('efg', 'g_hi')
|
||||
assert self._create('abc', 'class')._attrs == ('abc', '_class')
|
||||
assert self._create('8efg', '9ghi')._attrs == ('_8efg', '_9ghi')
|
||||
assert self._create('_efg', 'ghi')._attrs == ('_efg', 'ghi')
|
||||
assert self._create("efg", "g%hi")._attrs == ("efg", "g_hi")
|
||||
assert self._create("abc", "class")._attrs == ("abc", "_class")
|
||||
assert self._create("8efg", "9ghi")._attrs == ("_8efg", "_9ghi")
|
||||
assert self._create("_efg", "ghi")._attrs == ("_efg", "ghi")
|
||||
|
||||
self.assertRaises(ValueError, BagType, 'abc', ('efg', 'efg', 'ghi')) # duplicate field
|
||||
self.assertRaises(ValueError, BagType, "abc", ("efg", "efg", "ghi")) # duplicate field
|
||||
|
||||
self._create('x1', 'y2', typename='Point0') # Verify that numbers are allowed in names
|
||||
self._create('a', 'b', 'c', typename='_') # Test leading underscores in a typename
|
||||
self._create("x1", "y2", typename="Point0") # Verify that numbers are allowed in names
|
||||
self._create("a", "b", "c", typename="_") # Test leading underscores in a typename
|
||||
|
||||
bt = self._create('a!', 'a?')
|
||||
assert bt._attrs == ('a0', 'a1')
|
||||
x = bt('foo', 'bar')
|
||||
assert x.get('a!') == 'foo'
|
||||
assert x.a0 == 'foo'
|
||||
assert x.get('a?') == 'bar'
|
||||
assert x.a1 == 'bar'
|
||||
bt = self._create("a!", "a?")
|
||||
assert bt._attrs == ("a0", "a1")
|
||||
x = bt("foo", "bar")
|
||||
assert x.get("a!") == "foo"
|
||||
assert x.a0 == "foo"
|
||||
assert x.get("a?") == "bar"
|
||||
assert x.a1 == "bar"
|
||||
|
||||
# check unicode output
|
||||
bt = self._create('the', 'quick', 'brown', 'fox')
|
||||
bt = self._create("the", "quick", "brown", "fox")
|
||||
assert "u'" not in repr(bt._fields)
|
||||
|
||||
self.assertRaises(TypeError, Point._make, [11]) # catch too few args
|
||||
@ -66,33 +66,33 @@ class TestBagType(unittest.TestCase):
|
||||
|
||||
@unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above")
|
||||
def test_factory_doc_attr(self):
|
||||
Point = BagType('Point', ('x', 'y'))
|
||||
self.assertEqual(Point.__doc__, 'Point(x, y)')
|
||||
Point = BagType("Point", ("x", "y"))
|
||||
self.assertEqual(Point.__doc__, "Point(x, y)")
|
||||
|
||||
@unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above")
|
||||
def test_doc_writable(self):
|
||||
Point = BagType('Point', ('x', 'y'))
|
||||
Point = BagType("Point", ("x", "y"))
|
||||
self.assertEqual(Point.x.__doc__, "Alias for 'x'")
|
||||
Point.x.__doc__ = 'docstring for Point.x'
|
||||
self.assertEqual(Point.x.__doc__, 'docstring for Point.x')
|
||||
Point.x.__doc__ = "docstring for Point.x"
|
||||
self.assertEqual(Point.x.__doc__, "docstring for Point.x")
|
||||
|
||||
def test_name_fixer(self):
|
||||
for spec, renamed in [
|
||||
[('efg', 'g%hi'), ('efg', 'g_hi')], # field with non-alpha char
|
||||
[('abc', 'class'), ('abc', '_class')], # field has keyword
|
||||
[('8efg', '9ghi'), ('_8efg', '_9ghi')], # field starts with digit
|
||||
[('abc', '_efg'), ('abc', '_efg')], # field with leading underscore
|
||||
[('abc', '', 'x'), ('abc', '_0', 'x')], # fieldname is a space
|
||||
[('&', '¨', '*'), ('_0', '_1', '_2')], # Duplicate attrs, in theory
|
||||
[("efg", "g%hi"), ("efg", "g_hi")], # field with non-alpha char
|
||||
[("abc", "class"), ("abc", "_class")], # field has keyword
|
||||
[("8efg", "9ghi"), ("_8efg", "_9ghi")], # field starts with digit
|
||||
[("abc", "_efg"), ("abc", "_efg")], # field with leading underscore
|
||||
[("abc", "", "x"), ("abc", "_0", "x")], # fieldname is a space
|
||||
[("&", "¨", "*"), ("_0", "_1", "_2")], # Duplicate attrs, in theory
|
||||
]:
|
||||
assert self._create(*spec)._attrs == renamed
|
||||
|
||||
def test_module_parameter(self):
|
||||
NT = BagType('NT', ['x', 'y'], module=collections)
|
||||
NT = BagType("NT", ["x", "y"], module=collections)
|
||||
self.assertEqual(NT.__module__, collections)
|
||||
|
||||
def test_instance(self):
|
||||
Point = self._create('x', 'y', typename='Point')
|
||||
Point = self._create("x", "y", typename="Point")
|
||||
p = Point(11, 22)
|
||||
self.assertEqual(p, Point(x=11, y=22))
|
||||
self.assertEqual(p, Point(11, y=22))
|
||||
@ -101,12 +101,12 @@ class TestBagType(unittest.TestCase):
|
||||
self.assertEqual(p, Point(**dict(x=11, y=22)))
|
||||
self.assertRaises(TypeError, Point, 1) # too few args
|
||||
self.assertRaises(TypeError, Point, 1, 2, 3) # too many args
|
||||
self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
|
||||
self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
|
||||
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
||||
self.assertNotIn('__weakref__', dir(p))
|
||||
self.assertRaises(TypeError, eval, "Point(XXX=1, y=2)", locals()) # wrong keyword argument
|
||||
self.assertRaises(TypeError, eval, "Point(x=1)", locals()) # missing keyword argument
|
||||
self.assertEqual(repr(p), "Point(x=11, y=22)")
|
||||
self.assertNotIn("__weakref__", dir(p))
|
||||
self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
|
||||
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
|
||||
self.assertEqual(p._fields, ("x", "y")) # test _fields attribute
|
||||
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
|
||||
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
|
||||
|
||||
@ -115,13 +115,13 @@ class TestBagType(unittest.TestCase):
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self._fail('Did not detect an incorrect fieldname')
|
||||
self._fail("Did not detect an incorrect fieldname")
|
||||
|
||||
p = Point(x=11, y=22)
|
||||
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
||||
self.assertEqual(repr(p), "Point(x=11, y=22)")
|
||||
|
||||
def test_tupleness(self):
|
||||
Point = BagType('Point', ('x', 'y'))
|
||||
Point = BagType("Point", ("x", "y"))
|
||||
p = Point(11, 22)
|
||||
|
||||
self.assertIsInstance(p, tuple)
|
||||
@ -137,29 +137,29 @@ class TestBagType(unittest.TestCase):
|
||||
|
||||
self.assertEqual(p.x, x)
|
||||
self.assertEqual(p.y, y)
|
||||
self.assertRaises(AttributeError, eval, 'p.z', locals())
|
||||
self.assertRaises(AttributeError, eval, "p.z", locals())
|
||||
|
||||
def test_odd_sizes(self):
|
||||
Zero = BagType('Zero', ())
|
||||
Zero = BagType("Zero", ())
|
||||
self.assertEqual(Zero(), ())
|
||||
self.assertEqual(Zero._make([]), ())
|
||||
self.assertEqual(repr(Zero()), 'Zero()')
|
||||
self.assertEqual(repr(Zero()), "Zero()")
|
||||
self.assertEqual(Zero()._asdict(), {})
|
||||
self.assertEqual(Zero()._fields, ())
|
||||
|
||||
Dot = BagType('Dot', ('d', ))
|
||||
self.assertEqual(Dot(1), (1, ))
|
||||
self.assertEqual(Dot._make([1]), (1, ))
|
||||
Dot = BagType("Dot", ("d",))
|
||||
self.assertEqual(Dot(1), (1,))
|
||||
self.assertEqual(Dot._make([1]), (1,))
|
||||
self.assertEqual(Dot(1).d, 1)
|
||||
self.assertEqual(repr(Dot(1)), 'Dot(d=1)')
|
||||
self.assertEqual(Dot(1)._asdict(), {'d': 1})
|
||||
self.assertEqual(Dot(1)._replace(d=999), (999, ))
|
||||
self.assertEqual(Dot(1)._fields, ('d', ))
|
||||
self.assertEqual(repr(Dot(1)), "Dot(d=1)")
|
||||
self.assertEqual(Dot(1)._asdict(), {"d": 1})
|
||||
self.assertEqual(Dot(1)._replace(d=999), (999,))
|
||||
self.assertEqual(Dot(1)._fields, ("d",))
|
||||
|
||||
n = 5000 if sys.version_info >= (3, 7) else 254
|
||||
names = list(set(''.join([choice(string.ascii_letters) for j in range(10)]) for i in range(n)))
|
||||
names = list(set("".join([choice(string.ascii_letters) for j in range(10)]) for i in range(n)))
|
||||
n = len(names)
|
||||
Big = BagType('Big', names)
|
||||
Big = BagType("Big", names)
|
||||
b = Big(*range(n))
|
||||
self.assertEqual(b, tuple(range(n)))
|
||||
self.assertEqual(Big._make(range(n)), tuple(range(n)))
|
||||
@ -178,14 +178,14 @@ class TestBagType(unittest.TestCase):
|
||||
|
||||
def test_pickle(self):
|
||||
p = TBag(x=10, y=20, z=30)
|
||||
for module in (pickle, ):
|
||||
loads = getattr(module, 'loads')
|
||||
dumps = getattr(module, 'dumps')
|
||||
for module in (pickle,):
|
||||
loads = getattr(module, "loads")
|
||||
dumps = getattr(module, "dumps")
|
||||
for protocol in range(-1, module.HIGHEST_PROTOCOL + 1):
|
||||
q = loads(dumps(p, protocol))
|
||||
self.assertEqual(p, q)
|
||||
self.assertEqual(p._fields, q._fields)
|
||||
self.assertNotIn(b'OrderedDict', dumps(p, protocol))
|
||||
self.assertNotIn(b"OrderedDict", dumps(p, protocol))
|
||||
|
||||
def test_copy(self):
|
||||
p = TBag(x=10, y=20, z=30)
|
||||
@ -197,7 +197,7 @@ class TestBagType(unittest.TestCase):
|
||||
def test_name_conflicts(self):
|
||||
# Some names like "self", "cls", "tuple", "itemgetter", and "property"
|
||||
# failed when used as field names. Test to make sure these now work.
|
||||
T = BagType('T', ('itemgetter', 'property', 'self', 'cls', 'tuple'))
|
||||
T = BagType("T", ("itemgetter", "property", "self", "cls", "tuple"))
|
||||
t = T(1, 2, 3, 4, 5)
|
||||
self.assertEqual(t, (1, 2, 3, 4, 5))
|
||||
newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50)
|
||||
@ -206,28 +206,194 @@ class TestBagType(unittest.TestCase):
|
||||
# Broader test of all interesting names taken from the code, old
|
||||
# template, and an example
|
||||
words = {
|
||||
'Alias', 'At', 'AttributeError', 'Build', 'Bypass', 'Create', 'Encountered', 'Expected', 'Field', 'For',
|
||||
'Got', 'Helper', 'IronPython', 'Jython', 'KeyError', 'Make', 'Modify', 'Note', 'OrderedDict', 'Point',
|
||||
'Return', 'Returns', 'Type', 'TypeError', 'Used', 'Validate', 'ValueError', 'Variables', 'a', 'accessible',
|
||||
'add', 'added', 'all', 'also', 'an', 'arg_list', 'args', 'arguments', 'automatically', 'be', 'build',
|
||||
'builtins', 'but', 'by', 'cannot', 'class_namespace', 'classmethod', 'cls', 'collections', 'convert',
|
||||
'copy', 'created', 'creation', 'd', 'debugging', 'defined', 'dict', 'dictionary', 'doc', 'docstring',
|
||||
'docstrings', 'duplicate', 'effect', 'either', 'enumerate', 'environments', 'error', 'example', 'exec', 'f',
|
||||
'f_globals', 'field', 'field_names', 'fields', 'formatted', 'frame', 'function', 'functions', 'generate',
|
||||
'getter', 'got', 'greater', 'has', 'help', 'identifiers', 'indexable', 'instance', 'instantiate',
|
||||
'interning', 'introspection', 'isidentifier', 'isinstance', 'itemgetter', 'iterable', 'join', 'keyword',
|
||||
'keywords', 'kwds', 'len', 'like', 'list', 'map', 'maps', 'message', 'metadata', 'method', 'methods',
|
||||
'module', 'module_name', 'must', 'name', 'named', 'namedtuple', 'namedtuple_', 'names', 'namespace',
|
||||
'needs', 'new', 'nicely', 'num_fields', 'number', 'object', 'of', 'operator', 'option', 'p', 'particular',
|
||||
'pickle', 'pickling', 'plain', 'pop', 'positional', 'property', 'r', 'regular', 'rename', 'replace',
|
||||
'replacing', 'repr', 'repr_fmt', 'representation', 'result', 'reuse_itemgetter', 's', 'seen', 'sequence',
|
||||
'set', 'side', 'specified', 'split', 'start', 'startswith', 'step', 'str', 'string', 'strings', 'subclass',
|
||||
'sys', 'targets', 'than', 'the', 'their', 'this', 'to', 'tuple_new', 'type', 'typename', 'underscore',
|
||||
'unexpected', 'unpack', 'up', 'use', 'used', 'user', 'valid', 'values', 'variable', 'verbose', 'where',
|
||||
'which', 'work', 'x', 'y', 'z', 'zip'
|
||||
"Alias",
|
||||
"At",
|
||||
"AttributeError",
|
||||
"Build",
|
||||
"Bypass",
|
||||
"Create",
|
||||
"Encountered",
|
||||
"Expected",
|
||||
"Field",
|
||||
"For",
|
||||
"Got",
|
||||
"Helper",
|
||||
"IronPython",
|
||||
"Jython",
|
||||
"KeyError",
|
||||
"Make",
|
||||
"Modify",
|
||||
"Note",
|
||||
"OrderedDict",
|
||||
"Point",
|
||||
"Return",
|
||||
"Returns",
|
||||
"Type",
|
||||
"TypeError",
|
||||
"Used",
|
||||
"Validate",
|
||||
"ValueError",
|
||||
"Variables",
|
||||
"a",
|
||||
"accessible",
|
||||
"add",
|
||||
"added",
|
||||
"all",
|
||||
"also",
|
||||
"an",
|
||||
"arg_list",
|
||||
"args",
|
||||
"arguments",
|
||||
"automatically",
|
||||
"be",
|
||||
"build",
|
||||
"builtins",
|
||||
"but",
|
||||
"by",
|
||||
"cannot",
|
||||
"class_namespace",
|
||||
"classmethod",
|
||||
"cls",
|
||||
"collections",
|
||||
"convert",
|
||||
"copy",
|
||||
"created",
|
||||
"creation",
|
||||
"d",
|
||||
"debugging",
|
||||
"defined",
|
||||
"dict",
|
||||
"dictionary",
|
||||
"doc",
|
||||
"docstring",
|
||||
"docstrings",
|
||||
"duplicate",
|
||||
"effect",
|
||||
"either",
|
||||
"enumerate",
|
||||
"environments",
|
||||
"error",
|
||||
"example",
|
||||
"exec",
|
||||
"f",
|
||||
"f_globals",
|
||||
"field",
|
||||
"field_names",
|
||||
"fields",
|
||||
"formatted",
|
||||
"frame",
|
||||
"function",
|
||||
"functions",
|
||||
"generate",
|
||||
"getter",
|
||||
"got",
|
||||
"greater",
|
||||
"has",
|
||||
"help",
|
||||
"identifiers",
|
||||
"indexable",
|
||||
"instance",
|
||||
"instantiate",
|
||||
"interning",
|
||||
"introspection",
|
||||
"isidentifier",
|
||||
"isinstance",
|
||||
"itemgetter",
|
||||
"iterable",
|
||||
"join",
|
||||
"keyword",
|
||||
"keywords",
|
||||
"kwds",
|
||||
"len",
|
||||
"like",
|
||||
"list",
|
||||
"map",
|
||||
"maps",
|
||||
"message",
|
||||
"metadata",
|
||||
"method",
|
||||
"methods",
|
||||
"module",
|
||||
"module_name",
|
||||
"must",
|
||||
"name",
|
||||
"named",
|
||||
"namedtuple",
|
||||
"namedtuple_",
|
||||
"names",
|
||||
"namespace",
|
||||
"needs",
|
||||
"new",
|
||||
"nicely",
|
||||
"num_fields",
|
||||
"number",
|
||||
"object",
|
||||
"of",
|
||||
"operator",
|
||||
"option",
|
||||
"p",
|
||||
"particular",
|
||||
"pickle",
|
||||
"pickling",
|
||||
"plain",
|
||||
"pop",
|
||||
"positional",
|
||||
"property",
|
||||
"r",
|
||||
"regular",
|
||||
"rename",
|
||||
"replace",
|
||||
"replacing",
|
||||
"repr",
|
||||
"repr_fmt",
|
||||
"representation",
|
||||
"result",
|
||||
"reuse_itemgetter",
|
||||
"s",
|
||||
"seen",
|
||||
"sequence",
|
||||
"set",
|
||||
"side",
|
||||
"specified",
|
||||
"split",
|
||||
"start",
|
||||
"startswith",
|
||||
"step",
|
||||
"str",
|
||||
"string",
|
||||
"strings",
|
||||
"subclass",
|
||||
"sys",
|
||||
"targets",
|
||||
"than",
|
||||
"the",
|
||||
"their",
|
||||
"this",
|
||||
"to",
|
||||
"tuple_new",
|
||||
"type",
|
||||
"typename",
|
||||
"underscore",
|
||||
"unexpected",
|
||||
"unpack",
|
||||
"up",
|
||||
"use",
|
||||
"used",
|
||||
"user",
|
||||
"valid",
|
||||
"values",
|
||||
"variable",
|
||||
"verbose",
|
||||
"where",
|
||||
"which",
|
||||
"work",
|
||||
"x",
|
||||
"y",
|
||||
"z",
|
||||
"zip",
|
||||
}
|
||||
sorted_words = tuple(sorted(words))
|
||||
T = BagType('T', sorted_words)
|
||||
T = BagType("T", sorted_words)
|
||||
# test __new__
|
||||
values = tuple(range(len(words)))
|
||||
t = T(*values)
|
||||
@ -252,27 +418,39 @@ class TestBagType(unittest.TestCase):
|
||||
self.assertEqual(t.__getnewargs__(), values)
|
||||
|
||||
def test_repr(self):
|
||||
A = BagType('A', ('x', ))
|
||||
self.assertEqual(repr(A(1)), 'A(x=1)')
|
||||
A = BagType("A", ("x",))
|
||||
self.assertEqual(repr(A(1)), "A(x=1)")
|
||||
|
||||
# repr should show the name of the subclass
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
self.assertEqual(repr(B(1)), 'B(x=1)')
|
||||
self.assertEqual(repr(B(1)), "B(x=1)")
|
||||
|
||||
def test_namedtuple_subclass_issue_24931(self):
|
||||
class Point(BagType('_Point', ['x', 'y'])):
|
||||
class Point(BagType("_Point", ["x", "y"])):
|
||||
pass
|
||||
|
||||
a = Point(3, 4)
|
||||
self.assertEqual(a._asdict(), OrderedDict([('x', 3), ('y', 4)]))
|
||||
self.assertEqual(a._asdict(), OrderedDict([("x", 3), ("y", 4)]))
|
||||
|
||||
a.w = 5
|
||||
self.assertEqual(a.__dict__, {'w': 5})
|
||||
self.assertEqual(a.__dict__, {"w": 5})
|
||||
|
||||
def test_annoying_attribute_names(self):
|
||||
self._create(
|
||||
'__slots__', '__getattr__', '_attrs', '_fields', '__new__', '__getnewargs__', '__repr__', '_make', 'get',
|
||||
'_replace', '_asdict', '_cls', 'self', 'tuple'
|
||||
"__slots__",
|
||||
"__getattr__",
|
||||
"_attrs",
|
||||
"_fields",
|
||||
"__new__",
|
||||
"__getnewargs__",
|
||||
"__repr__",
|
||||
"_make",
|
||||
"get",
|
||||
"_replace",
|
||||
"_asdict",
|
||||
"_cls",
|
||||
"self",
|
||||
"tuple",
|
||||
)
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from bonobo.util import sortedlist, ensure_tuple
|
||||
from bonobo.util.collections import tuplize, cast
|
||||
from bonobo.util import ensure_tuple, sortedlist
|
||||
from bonobo.util.collections import cast, tuplize
|
||||
|
||||
|
||||
def test_sortedlist():
|
||||
@ -14,20 +14,20 @@ def test_sortedlist():
|
||||
|
||||
|
||||
def test_ensure_tuple():
|
||||
assert ensure_tuple('a') == ('a', )
|
||||
assert ensure_tuple(('a', )) == ('a', )
|
||||
assert ensure_tuple("a") == ("a",)
|
||||
assert ensure_tuple(("a",)) == ("a",)
|
||||
assert ensure_tuple(()) is ()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('tuplize', [tuplize, cast(tuple)])
|
||||
@pytest.mark.parametrize("tuplize", [tuplize, cast(tuple)])
|
||||
def test_tuplize(tuplize):
|
||||
tuplized_lambda = tuplize(lambda: [1, 2, 3])
|
||||
assert tuplized_lambda() == (1, 2, 3)
|
||||
|
||||
@tuplize
|
||||
def some_generator():
|
||||
yield 'c'
|
||||
yield 'b'
|
||||
yield 'a'
|
||||
yield "c"
|
||||
yield "b"
|
||||
yield "a"
|
||||
|
||||
assert some_generator() == ('c', 'b', 'a')
|
||||
assert some_generator() == ("c", "b", "a")
|
||||
|
||||
@ -17,7 +17,7 @@ def test_deprecated_alias():
|
||||
def foo():
|
||||
pass
|
||||
|
||||
foo = deprecated_alias('bar', foo)
|
||||
foo = deprecated_alias("bar", foo)
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
foo()
|
||||
|
||||
@ -2,7 +2,7 @@ import operator
|
||||
|
||||
import pytest
|
||||
|
||||
from bonobo.util.objects import Wrapper, get_name, ValueHolder, get_attribute_or_create
|
||||
from bonobo.util.objects import ValueHolder, Wrapper, get_attribute_or_create, get_name
|
||||
from bonobo.util.testing import optional_contextmanager
|
||||
|
||||
|
||||
@ -11,31 +11,31 @@ class foo:
|
||||
|
||||
|
||||
class bar:
|
||||
__name__ = 'baz'
|
||||
__name__ = "baz"
|
||||
|
||||
|
||||
def test_get_name():
|
||||
assert get_name(42) == 'int'
|
||||
assert get_name('eat at joe.') == 'str'
|
||||
assert get_name(str) == 'str'
|
||||
assert get_name(object) == 'object'
|
||||
assert get_name(get_name) == 'get_name'
|
||||
assert get_name(foo) == 'foo'
|
||||
assert get_name(foo()) == 'foo'
|
||||
assert get_name(bar) == 'bar'
|
||||
assert get_name(bar()) == 'baz'
|
||||
assert get_name(42) == "int"
|
||||
assert get_name("eat at joe.") == "str"
|
||||
assert get_name(str) == "str"
|
||||
assert get_name(object) == "object"
|
||||
assert get_name(get_name) == "get_name"
|
||||
assert get_name(foo) == "foo"
|
||||
assert get_name(foo()) == "foo"
|
||||
assert get_name(bar) == "bar"
|
||||
assert get_name(bar()) == "baz"
|
||||
|
||||
|
||||
def test_wrapper_name():
|
||||
assert get_name(Wrapper(42)) == 'int'
|
||||
assert get_name(Wrapper('eat at joe.')) == 'str'
|
||||
assert get_name(Wrapper(str)) == 'str'
|
||||
assert get_name(Wrapper(object)) == 'object'
|
||||
assert get_name(Wrapper(foo)) == 'foo'
|
||||
assert get_name(Wrapper(foo())) == 'foo'
|
||||
assert get_name(Wrapper(bar)) == 'bar'
|
||||
assert get_name(Wrapper(bar())) == 'baz'
|
||||
assert get_name(Wrapper(get_name)) == 'get_name'
|
||||
assert get_name(Wrapper(42)) == "int"
|
||||
assert get_name(Wrapper("eat at joe.")) == "str"
|
||||
assert get_name(Wrapper(str)) == "str"
|
||||
assert get_name(Wrapper(object)) == "object"
|
||||
assert get_name(Wrapper(foo)) == "foo"
|
||||
assert get_name(Wrapper(foo())) == "foo"
|
||||
assert get_name(Wrapper(bar)) == "bar"
|
||||
assert get_name(Wrapper(bar())) == "baz"
|
||||
assert get_name(Wrapper(get_name)) == "get_name"
|
||||
|
||||
|
||||
def test_valueholder():
|
||||
@ -65,10 +65,7 @@ def test_valueholder_notequal():
|
||||
assert not (x != 42)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('rlo,rhi', [
|
||||
(1, 2),
|
||||
('a', 'b'),
|
||||
])
|
||||
@pytest.mark.parametrize("rlo,rhi", [(1, 2), ("a", "b")])
|
||||
def test_valueholder_ordering(rlo, rhi):
|
||||
vlo, vhi = ValueHolder(rlo), ValueHolder(rhi)
|
||||
|
||||
@ -97,15 +94,15 @@ def test_valueholders_containers():
|
||||
assert 5 in x
|
||||
assert 42 not in x
|
||||
|
||||
y = ValueHolder({'foo': 'bar', 'corp': 'acme'})
|
||||
y = ValueHolder({"foo": "bar", "corp": "acme"})
|
||||
|
||||
assert 'foo' in y
|
||||
assert y['foo'] == 'bar'
|
||||
assert "foo" in y
|
||||
assert y["foo"] == "bar"
|
||||
with pytest.raises(KeyError):
|
||||
y['no']
|
||||
y['no'] = 'oh, wait'
|
||||
assert 'no' in y
|
||||
assert 'oh, wait' == y['no']
|
||||
y["no"]
|
||||
y["no"] = "oh, wait"
|
||||
assert "no" in y
|
||||
assert "oh, wait" == y["no"]
|
||||
|
||||
|
||||
def test_get_attribute_or_create():
|
||||
@ -117,27 +114,39 @@ def test_get_attribute_or_create():
|
||||
with pytest.raises(AttributeError):
|
||||
x.foo
|
||||
|
||||
foo = get_attribute_or_create(x, 'foo', 'bar')
|
||||
assert foo == 'bar'
|
||||
assert x.foo == 'bar'
|
||||
foo = get_attribute_or_create(x, "foo", "bar")
|
||||
assert foo == "bar"
|
||||
assert x.foo == "bar"
|
||||
|
||||
foo = get_attribute_or_create(x, 'foo', 'baz')
|
||||
assert foo == 'bar'
|
||||
assert x.foo == 'bar'
|
||||
foo = get_attribute_or_create(x, "foo", "baz")
|
||||
assert foo == "bar"
|
||||
assert x.foo == "bar"
|
||||
|
||||
|
||||
unsupported_operations = {
|
||||
int: {operator.matmul},
|
||||
str: {
|
||||
operator.sub, operator.mul, operator.matmul, operator.floordiv, operator.truediv, operator.mod, divmod,
|
||||
operator.pow, operator.lshift, operator.rshift, operator.and_, operator.xor, operator.or_
|
||||
operator.sub,
|
||||
operator.mul,
|
||||
operator.matmul,
|
||||
operator.floordiv,
|
||||
operator.truediv,
|
||||
operator.mod,
|
||||
divmod,
|
||||
operator.pow,
|
||||
operator.lshift,
|
||||
operator.rshift,
|
||||
operator.and_,
|
||||
operator.xor,
|
||||
operator.or_,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('x,y', [(5, 3), (0, 10), (0, 0), (1, 1), ('foo', 'bar'), ('', 'baz!')])
|
||||
@pytest.mark.parametrize("x,y", [(5, 3), (0, 10), (0, 0), (1, 1), ("foo", "bar"), ("", "baz!")])
|
||||
@pytest.mark.parametrize(
|
||||
'operation,inplace_operation', [
|
||||
"operation,inplace_operation",
|
||||
[
|
||||
(operator.add, operator.iadd),
|
||||
(operator.sub, operator.isub),
|
||||
(operator.mul, operator.imul),
|
||||
@ -152,14 +161,14 @@ unsupported_operations = {
|
||||
(operator.and_, operator.iand),
|
||||
(operator.xor, operator.ixor),
|
||||
(operator.or_, operator.ior),
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_valueholder_integer_operations(x, y, operation, inplace_operation):
|
||||
v = ValueHolder(x)
|
||||
|
||||
is_supported = operation not in unsupported_operations.get(type(x), set())
|
||||
|
||||
isdiv = ('div' in operation.__name__) or ('mod' in operation.__name__)
|
||||
isdiv = ("div" in operation.__name__) or ("mod" in operation.__name__)
|
||||
|
||||
# forward...
|
||||
with optional_contextmanager(pytest.raises(TypeError), ignore=is_supported):
|
||||
|
||||
@ -3,16 +3,16 @@ from bonobo.util.resolvers import _parse_option, _resolve_options, _resolve_tran
|
||||
|
||||
|
||||
def test_parse_option():
|
||||
assert _parse_option('foo=bar') == ('foo', 'bar')
|
||||
assert _parse_option('foo="bar"') == ('foo', 'bar')
|
||||
assert _parse_option('sep=";"') == ('sep', ';')
|
||||
assert _parse_option('foo') == ('foo', True)
|
||||
assert _parse_option("foo=bar") == ("foo", "bar")
|
||||
assert _parse_option('foo="bar"') == ("foo", "bar")
|
||||
assert _parse_option('sep=";"') == ("sep", ";")
|
||||
assert _parse_option("foo") == ("foo", True)
|
||||
|
||||
|
||||
def test_resolve_options():
|
||||
assert _resolve_options(('foo=bar', 'bar="baz"')) == {'foo': 'bar', 'bar': 'baz'}
|
||||
assert _resolve_options(("foo=bar", 'bar="baz"')) == {"foo": "bar", "bar": "baz"}
|
||||
assert _resolve_options() == {}
|
||||
|
||||
|
||||
def test_resolve_transformations():
|
||||
assert _resolve_transformations(('PrettyPrinter', )) == (bonobo.PrettyPrinter, )
|
||||
assert _resolve_transformations(("PrettyPrinter",)) == (bonobo.PrettyPrinter,)
|
||||
|
||||
@ -3,12 +3,9 @@ from bonobo.util.statistics import WithStatistics
|
||||
|
||||
class MyThingWithStats(WithStatistics):
|
||||
def get_statistics(self, *args, **kwargs):
|
||||
return (
|
||||
('foo', 42),
|
||||
('bar', 69),
|
||||
)
|
||||
return (("foo", 42), ("bar", 69))
|
||||
|
||||
|
||||
def test_with_statistics():
|
||||
o = MyThingWithStats()
|
||||
assert o.get_statistics_as_string() == 'foo=42 bar=69'
|
||||
assert o.get_statistics_as_string() == "foo=42 bar=69"
|
||||
|
||||
Reference in New Issue
Block a user