Skip to content

Commit

Permalink
Use new serious biz names in internal code (#257)
Browse files Browse the repository at this point in the history
* Use new serious biz names

* Use offical API in tests where possible

* Fix flake8
  • Loading branch information
hynek committed Oct 2, 2017
1 parent 6b74a4b commit 03c10ad
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 176 deletions.
8 changes: 4 additions & 4 deletions conftest.py
Expand Up @@ -9,12 +9,12 @@ def C():
"""
Return a simple but fully featured attrs class with an x and a y attribute.
"""
from attr import attributes, attr
import attr

@attributes
@attr.s
class C(object):
x = attr()
y = attr()
x = attr.ib()
y = attr.ib()

return C

Expand Down
8 changes: 4 additions & 4 deletions src/attr/__init__.py
Expand Up @@ -11,8 +11,8 @@
Attribute,
Factory,
NOTHING,
attr,
attributes,
attrib,
attrs,
fields,
make_class,
validate,
Expand Down Expand Up @@ -41,8 +41,8 @@
__copyright__ = "Copyright (c) 2015 Hynek Schlawack"


s = attrs = attributes
ib = attrib = attr
s = attributes = attrs
ib = attr = attrib

__all__ = [
"Attribute",
Expand Down
37 changes: 22 additions & 15 deletions src/attr/_make.py
Expand Up @@ -59,9 +59,9 @@ def __hash__(self):
"""


def attr(default=NOTHING, validator=None,
repr=True, cmp=True, hash=None, init=True,
convert=None, metadata={}, type=None):
def attrib(default=NOTHING, validator=None,
repr=True, cmp=True, hash=None, init=True,
convert=None, metadata={}, type=None):
"""
Create a new attribute on a class.
Expand Down Expand Up @@ -263,9 +263,9 @@ def _frozen_delattrs(self, name):
raise FrozenInstanceError()


def attributes(maybe_cls=None, these=None, repr_ns=None,
repr=True, cmp=True, hash=None, init=True,
slots=False, frozen=False, str=False):
def attrs(maybe_cls=None, these=None, repr_ns=None,
repr=True, cmp=True, hash=None, init=True,
slots=False, frozen=False, str=False):
r"""
A class decorator that adds `dunder
<https://wiki.python.org/moin/DunderAlias>`_\ -methods according to the
Expand Down Expand Up @@ -417,14 +417,21 @@ def wrap(cls):

return cls

# attrs_or class type depends on the usage of the decorator. It's a class
# if it's used as `@attributes` but ``None`` if used # as `@attributes()`.
# maybe_cls's type depends on the usage of the decorator. It's a class
# if it's used as `@attrs` but ``None`` if used as `@attrs()`.
if maybe_cls is None:
return wrap
else:
return wrap(maybe_cls)


_attrs = attrs
"""
Internal alias so we can use it in functions that take an argument called
*attrs*.
"""


if PY2:
def _has_frozen_superclass(cls):
"""
Expand Down Expand Up @@ -1010,7 +1017,7 @@ def default(self, meth):
_CountingAttr = _add_cmp(_add_repr(_CountingAttr))


@attributes(slots=True, init=False, hash=True)
@attrs(slots=True, init=False, hash=True)
class Factory(object):
"""
Stores a factory callable.
Expand All @@ -1025,8 +1032,8 @@ class Factory(object):
.. versionadded:: 17.1.0 *takes_self*
"""
factory = attr()
takes_self = attr()
factory = attrib()
takes_self = attrib()

def __init__(self, factory, takes_self=False):
"""
Expand Down Expand Up @@ -1060,12 +1067,12 @@ def make_class(name, attrs, bases=(object,), **attributes_arguments):
if isinstance(attrs, dict):
cls_dict = attrs
elif isinstance(attrs, (list, tuple)):
cls_dict = dict((a, attr()) for a in attrs)
cls_dict = dict((a, attrib()) for a in attrs)
else:
raise TypeError("attrs argument must be a dict or a list.")

post_init = cls_dict.pop("__attrs_post_init__", None)
return attributes(
return _attrs(
these=cls_dict, **attributes_arguments
)(type(
name,
Expand All @@ -1078,12 +1085,12 @@ def make_class(name, attrs, bases=(object,), **attributes_arguments):
# import into .validators.


@attributes(slots=True, hash=True)
@attrs(slots=True, hash=True)
class _AndValidator(object):
"""
Compose many validators to a single one.
"""
_validators = attr()
_validators = attrib()

def __call__(self, inst, attr, value):
for v in self._validators:
Expand Down
18 changes: 9 additions & 9 deletions src/attr/validators.py
Expand Up @@ -4,7 +4,7 @@

from __future__ import absolute_import, division, print_function

from ._make import attr, attributes, and_, _AndValidator
from ._make import attrib, attrs, and_, _AndValidator


__all__ = [
Expand All @@ -16,9 +16,9 @@
]


@attributes(repr=False, slots=True, hash=True)
@attrs(repr=False, slots=True, hash=True)
class _InstanceOfValidator(object):
type = attr()
type = attrib()

def __call__(self, inst, attr, value):
"""
Expand Down Expand Up @@ -56,9 +56,9 @@ def instance_of(type):
return _InstanceOfValidator(type)


@attributes(repr=False, slots=True, hash=True)
@attrs(repr=False, slots=True, hash=True)
class _ProvidesValidator(object):
interface = attr()
interface = attrib()

def __call__(self, inst, attr, value):
"""
Expand Down Expand Up @@ -95,9 +95,9 @@ def provides(interface):
return _ProvidesValidator(interface)


@attributes(repr=False, slots=True, hash=True)
@attrs(repr=False, slots=True, hash=True)
class _OptionalValidator(object):
validator = attr()
validator = attrib()

def __call__(self, inst, attr, value):
if value is None:
Expand Down Expand Up @@ -130,9 +130,9 @@ def optional(validator):
return _OptionalValidator(validator)


@attributes(repr=False, slots=True, hash=True)
@attrs(repr=False, slots=True, hash=True)
class _InValidator(object):
options = attr()
options = attrib()

def __call__(self, inst, attr, value):
if value not in self.options:
Expand Down
36 changes: 17 additions & 19 deletions tests/test_annotations.py
Expand Up @@ -6,11 +6,7 @@

import pytest

from attr._make import (
attr,
attributes,
fields
)
import attr

import typing

Expand All @@ -24,34 +20,36 @@ def test_basic_annotations(self):
"""
Sets the `Attribute.type` attr from basic type annotations.
"""
@attributes
@attr.s
class C(object):
x: int = attr()
y = attr(type=str)
z = attr()
assert int is fields(C).x.type
assert str is fields(C).y.type
assert None is fields(C).z.type
x: int = attr.ib()
y = attr.ib(type=str)
z = attr.ib()

assert int is attr.fields(C).x.type
assert str is attr.fields(C).y.type
assert None is attr.fields(C).z.type

def test_catches_basic_type_conflict(self):
"""
Raises ValueError type is specified both ways.
"""
with pytest.raises(ValueError) as e:
@attributes
@attr.s
class C:
x: int = attr(type=int)
x: int = attr.ib(type=int)

assert ("Type annotation and type argument cannot "
"both be present",) == e.value.args

def test_typing_annotations(self):
"""
Sets the `Attribute.type` attr from typing annotations.
"""
@attributes
@attr.s
class C(object):
x: typing.List[int] = attr()
y = attr(type=typing.Optional[str])
x: typing.List[int] = attr.ib()
y = attr.ib(type=typing.Optional[str])

assert typing.List[int] is fields(C).x.type
assert typing.Optional[str] is fields(C).y.type
assert typing.List[int] is attr.fields(C).x.type
assert typing.Optional[str] is attr.fields(C).y.type

0 comments on commit 03c10ad

Please sign in to comment.