本文整理汇总了Python中attr.make_class方法的典型用法代码示例。如果您正苦于以下问题:Python attr.make_class方法的具体用法?Python attr.make_class怎么用?Python attr.make_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr
的用法示例。
在下文中一共展示了attr.make_class方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bases
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_bases(self):
"""
Parameter bases default to (object,) and subclasses correctly
"""
class D(object):
pass
cls = make_class("C", {})
assert cls.__mro__[-1] == object
cls = make_class("C", {}, bases=(D,))
assert D in cls.__mro__
assert isinstance(cls(), D)
示例2: test_convert_property
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_convert_property(self, val, init):
"""
Property tests for attributes using converter.
"""
C = make_class(
"C",
{
"y": attr.ib(),
"x": attr.ib(
init=init, default=val, converter=lambda v: v + 1
),
},
)
c = C(2)
assert c.x == val + 1
assert c.y == 2
示例3: test_converter_factory_property
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_converter_factory_property(self, val, init):
"""
Property tests for attributes with converter, and a factory default.
"""
C = make_class(
"C",
ordered_dict(
[
("y", attr.ib()),
(
"x",
attr.ib(
init=init,
default=Factory(lambda: val),
converter=lambda v: v + 1,
),
),
]
),
)
c = C(2)
assert c.x == val + 1
assert c.y == 2
示例4: test_convert_before_validate
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_convert_before_validate(self):
"""
Validation happens after conversion.
"""
def validator(inst, attr, val):
raise RuntimeError("foo")
C = make_class(
"C",
{
"x": attr.ib(validator=validator, converter=lambda v: 1 / 0),
"y": attr.ib(),
},
)
with pytest.raises(ZeroDivisionError):
C(1, 2)
示例5: test_run_validators
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_run_validators(self):
"""
Setting `_run_validators` to False prevents validators from running.
"""
_config._run_validators = False
obj = object()
def raiser(_, __, ___):
raise Exception(obj)
C = make_class("C", {"x": attr.ib(validator=raiser)})
c = C(1)
validate(c)
assert 1 == c.x
_config._run_validators = True
with pytest.raises(Exception):
validate(c)
with pytest.raises(Exception) as e:
C(1)
assert (obj,) == e.value.args
示例6: test_metadata_present
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_metadata_present(self, list_of_attrs):
"""
Assert dictionaries are copied and present.
"""
C = make_class("C", dict(zip(gen_attr_names(), list_of_attrs)))
for hyp_attr, class_attr in zip(list_of_attrs, fields(C)):
if hyp_attr.metadata is None:
# The default is a singleton empty dict.
assert class_attr.metadata is not None
assert len(class_attr.metadata) == 0
else:
assert hyp_attr.metadata == class_attr.metadata
# Once more, just to assert getting items and iteration.
for k in class_attr.metadata:
assert hyp_attr.metadata[k] == class_attr.metadata[k]
assert hyp_attr.metadata.get(k) == class_attr.metadata.get(
k
)
示例7: __attrs_post_init__
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def __attrs_post_init__(self):
""" Build the constructor that can create feature structures of this type """
name = _string_to_valid_classname(self.name)
fields = {feature.name: attr.ib(default=None, repr=(feature.name != "sofa")) for feature in self.all_features}
fields["type"] = attr.ib(default=self.name)
self._constructor = attr.make_class(name, fields, bases=(FeatureStructure,), slots=True, eq=False, order=False)
示例8: test_simple
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_simple(self, ls):
"""
Passing a list of strings creates attributes with default args.
"""
C1 = make_class("C1", ls(["a", "b"]))
@attr.s
class C2(object):
a = attr.ib()
b = attr.ib()
assert C1.__attrs_attrs__ == C2.__attrs_attrs__
示例9: test_dict
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_dict(self):
"""
Passing a dict of name: _CountingAttr creates an equivalent class.
"""
C1 = make_class(
"C1", {"a": attr.ib(default=42), "b": attr.ib(default=None)}
)
@attr.s
class C2(object):
a = attr.ib(default=42)
b = attr.ib(default=None)
assert C1.__attrs_attrs__ == C2.__attrs_attrs__
示例10: test_attr_args
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_attr_args(self):
"""
attributes_arguments are passed to attributes
"""
C = make_class("C", ["x"], repr=False)
assert repr(C(1)).startswith("<tests.test_make.C object at 0x")
示例11: test_catches_wrong_attrs_type
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_catches_wrong_attrs_type(self):
"""
Raise `TypeError` if an invalid type for attrs is passed.
"""
with pytest.raises(TypeError) as e:
make_class("C", object())
assert ("attrs argument must be a dict or a list.",) == e.value.args
示例12: test_missing_sys_getframe
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_missing_sys_getframe(self, monkeypatch):
"""
`make_class()` does not fail when `sys._getframe()` is not available.
"""
monkeypatch.delattr(sys, "_getframe")
C = make_class("C", ["x"])
assert 1 == len(C.__attrs_attrs__)
示例13: test_make_class_ordered
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_make_class_ordered(self):
"""
If `make_class()` is passed ordered attrs, their order is respected
instead of the counter.
"""
b = attr.ib(default=2)
a = attr.ib(default=1)
C = attr.make_class("C", ordered_dict([("a", a), ("b", b)]))
assert "C(a=1, b=2)" == repr(C())
示例14: test_convert
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_convert(self):
"""
Return value of converter is used as the attribute's value.
"""
C = make_class(
"C", {"x": attr.ib(converter=lambda v: v + 1), "y": attr.ib()}
)
c = C(1, 2)
assert c.x == 2
assert c.y == 2
示例15: test_frozen
# 需要导入模块: import attr [as 别名]
# 或者: from attr import make_class [as 别名]
def test_frozen(self):
"""
Converters circumvent immutability.
"""
C = make_class(
"C", {"x": attr.ib(converter=lambda v: int(v))}, frozen=True
)
C("1")