Rename add_XXX to no_XXX

Assume you want everything by default and be consistent with the no_XXX
attributes on Attribute.
This commit is contained in:
Hynek Schlawack 2015-02-07 09:15:59 +01:00
parent ddf0256527
commit 4e81b7b5e2
6 changed files with 35 additions and 35 deletions

View File

@ -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
<https://wiki.python.org/moin/DunderAlias>`_\ -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

View File

@ -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()

View File

@ -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!
<attr._make.C object at ...>

View File

@ -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,
)

View File

@ -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):

View File

@ -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("<attr._make.C object at 0x")
def test_catches_wrong_attrs_type(self):