diff --git a/attr/_make.py b/attr/_make.py index dc1e7c6b..7148b6e4 100644 --- a/attr/_make.py +++ b/attr/_make.py @@ -110,29 +110,30 @@ def _transform_attrs(cl): setattr(cl, attr_name, a) -def attributes(maybe_cl=None, add_repr=True, add_cmp=True, add_hash=True, - add_init=True): +def attributes(maybe_cl=None, no_repr=False, no_cmp=False, no_hash=False, + no_init=False): """ A class decorator that adds `dunder `_\ -methods according to the specified attributes using :func:`attr.ib`. - :param add_repr: Create a ``__repr__`` method with a human readable + :param no_repr: Don't create a ``__repr__`` method with a human readable represantation of ``attrs`` attributes.. - :type add_repr: bool + :type no_repr: bool - :param add_cmp: Create ``__eq__``, ``__ne__``, ``__lt__``, ``__le__``, + :param no_cmp: Don't create ``__eq__``, ``__ne__``, ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` methods that compare the class as if it were a tuple of its ``attrs`` attributes. But the attributes are *only* compared, if the type of both classes is *identical*! - :type add_cmp: bool + :type no_cmp: bool - :param add_hash: Add a ``__hash__`` method that returns the :func:`hash` of - a tuple of all ``attrs`` attribute values. - :type add_hash: bool + :param no_hash: Don't add a ``__hash__`` method that returns the + :func:`hash` of a tuple of all ``attrs`` attribute values. + :type no_hash: bool - :param add_init: Add a ``__init__`` method that initialiazes the ``attrs`` - attributes. Leading underscores are stripped for the argument name:. + :param no_init: Don't add a ``__init__`` method that initialiazes the + ``attrs`` attributes. Leading underscores are stripped for the + argument name:. .. doctest:: @@ -142,17 +143,17 @@ def attributes(maybe_cl=None, add_repr=True, add_cmp=True, add_hash=True, ... _private = attr.ib() >>> C(private=42) C(_private=42) - :type add_init: bool + :type no_init: bool """ def wrap(cl): _transform_attrs(cl) - if add_repr is True: + if not no_repr: cl = _add_repr(cl) - if add_cmp is True: + if not no_cmp: cl = _add_cmp(cl) - if add_hash is True: + if not no_hash: cl = _add_hash(cl) - if add_init is True: + if not no_init: cl = _add_init(cl) return cl diff --git a/attr/validators.py b/attr/validators.py index dbc7a1c6..5f938abf 100644 --- a/attr/validators.py +++ b/attr/validators.py @@ -5,7 +5,7 @@ from __future__ import absolute_import, division, print_function from ._make import attr, attributes -@attributes(add_repr=False) +@attributes(no_repr=True) class _InstanceOfValidator(object): type_ = attr() @@ -45,7 +45,7 @@ def instance_of(type_): return _InstanceOfValidator(type_) -@attributes(add_repr=False) +@attributes(no_repr=True) class _ProvidesValidator(object): interface = attr() diff --git a/docs/examples.rst b/docs/examples.rst index 7acfd1d9..feef21e2 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -259,7 +259,7 @@ You can still have power over the attributes if you pass a dictionary of name: ` >>> C = attr.make_class("C", {"x": attr.ib(default=42), ... "y": attr.ib(default=attr.Factory(list))}, - ... add_repr=False) + ... no_repr=True) >>> i = C() >>> i # no repr added! diff --git a/tests/__init__.py b/tests/__init__.py index b816c771..79c17586 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,14 +6,13 @@ from attr import Attribute from attr._make import NOTHING, make_class -def simple_class(add_cmp=False, add_repr=False, add_hash=False): +def simple_class(no_cmp=True, no_repr=True, no_hash=True): """ Return a new simple class. """ return make_class( "C", ["a", "b"], - add_cmp=add_cmp, add_repr=add_repr, add_hash=add_hash, - add_init=True, + no_cmp=no_cmp, no_repr=no_repr, no_hash=no_hash, no_init=False, ) diff --git a/tests/test_dunders.py b/tests/test_dunders.py index aea11398..142a722d 100644 --- a/tests/test_dunders.py +++ b/tests/test_dunders.py @@ -24,9 +24,9 @@ from attr._make import ( from attr.validators import instance_of -CmpC = simple_class(add_cmp=True) -ReprC = simple_class(add_repr=True) -HashC = simple_class(add_hash=True) +CmpC = simple_class(no_cmp=False) +ReprC = simple_class(no_repr=False) +HashC = simple_class(no_hash=False) class InitC(object): diff --git a/tests/test_make.py b/tests/test_make.py index 90934f08..8394008b 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -154,10 +154,10 @@ class TestAttributes(object): assert sentinel != getattr(C2, method_name) @pytest.mark.parametrize("arg_name, method_name", [ - ("add_repr", "__repr__"), - ("add_cmp", "__eq__"), - ("add_hash", "__hash__"), - ("add_init", "__init__"), + ("no_repr", "__repr__"), + ("no_cmp", "__eq__"), + ("no_hash", "__hash__"), + ("no_init", "__init__"), ]) def test_respects_add_arguments(self, arg_name, method_name): """ @@ -168,12 +168,12 @@ class TestAttributes(object): sentinel = object() am_args = { - "add_repr": True, - "add_cmp": True, - "add_hash": True, - "add_init": True + "no_repr": False, + "no_cmp": False, + "no_hash": False, + "no_init": False } - am_args[arg_name] = False + am_args[arg_name] = True class C(object): x = attr() @@ -246,7 +246,7 @@ class TestMakeClass(object): """ attributes_arguments are passed to attributes """ - C = make_class("C", ["x"], add_repr=False) + C = make_class("C", ["x"], no_repr=True) assert repr(C(1)).startswith("