Skip to content

Commit

Permalink
Learning about attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
adomokos committed Sep 4, 2020
1 parent a2fdaf6 commit 8ce4808
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 15 deletions.
49 changes: 34 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Attila Domokos <adomokos@gmail.com>"]
[tool.poetry.dependencies]
python = "^3.5"
mamba = "^0.11.0"
attrs = "^20.1.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
Expand Down
102 changes: 102 additions & 0 deletions spec/attrs_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from mamba import description, it
from attr import attrs, attrib
import attr


@attr.s
class Empty(object):
pass


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


@attrs
class SeriousCoordinates(object):
x = attrib()
y = attrib()

# Subclassing is bad for you...
@attr.s
class A(object):
a = attr.ib()

def get_a(self):
return self.a


@attr.s
class B(object):
b = attr.ib()


@attr.s
class C(A, B):
c = attr.ib()


@attr.s
class SomeClass(object):
a_number = attr.ib(default=42)
list_of_numbers = attr.ib(factory=list)

def hard_math(self, another_number):
return self.a_number + sum(self.list_of_numbers) * another_number

# asdict can accept a lambda
@attr.s
class UserList(object):
users = attr.ib()


@attr.s
class User(object):
email = attr.ib()
password = attr.ib()


with description('Attrs') as self:
with it("works with Empty objects"):
empty1 = Empty()
empty2 = Empty()
assert empty1 == empty2
assert (empty1 is empty2) is False

with it("works with coordinates"):
c1 = Coordinates(x=2, y=1)
c2 = Coordinates(x=2, y=1)
assert c1 == c2
c3 = Coordinates(1, 2)
assert (c2 != c3)

with it("works with SeriousCoordinates"):
a = Coordinates(1, 2)
b = SeriousCoordinates(1, 2)
assert attr.fields(Coordinates) == attr.fields(SeriousCoordinates)

with it("works with inheritance"):
i = C(1, 2, 3)
assert i == C(1, 2, 3)

with it("can have various attributes, even lists"):
sc = SomeClass(1, [1, 2, 3])
assert sc.hard_math(3) == 19
assert sc == SomeClass(1, [1, 2, 3])

with it("can convert to dictionaries"):
d1 = attr.asdict(Coordinates(x=1, y=2))
assert [*d1.keys()] == ['x', 'y']

with it("asdict can accept lambda to filter"):
user_list = UserList(
[User("jane@doe.invalid", "s33kred"),
User("joe@doe.invalid", "p4ssw0rd")]
)
d1 = attr.asdict(
user_list,
filter=lambda attr, value: attr.name != "password"
)
assert len(d1['users']) == 2

0 comments on commit 8ce4808

Please sign in to comment.