Skip to content

Commit

Permalink
Ensure that bare attributes with default None are removed too (#556)
Browse files Browse the repository at this point in the history
Fixes #523
  • Loading branch information
hynek authored and ambv committed Jul 23, 2019
1 parent 4a1b3a1 commit dc1b5a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/523.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When collecting attributes using ``@attr.s(auto_attribs=True)``, attributes with a default of ``None`` are now deleted too.
1 change: 1 addition & 0 deletions changelog.d/556.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When collecting attributes using ``@attr.s(auto_attribs=True)``, attributes with a default of ``None`` are now deleted too.
5 changes: 4 additions & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

_empty_metadata_singleton = metadata_proxy({})

# Unique object for unequivocal getattr() defaults.
_sentinel = object()


class _Nothing(object):
"""
Expand Down Expand Up @@ -504,7 +507,7 @@ def _patch_original_class(self):
for name in self._attr_names:
if (
name not in base_names
and getattr(cls, name, None) is not None
and getattr(cls, name, _sentinel) != _sentinel
):
try:
delattr(cls, name)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,20 @@ class C(Base):
x: int

assert 1 == C(1).x

def test_removes_none_too(self):
"""
Regression test for #523: make sure defaults that are set to None are
removed too.
"""

@attr.s(auto_attribs=True)
class C:
x: int = 42
y: typing.Any = None

with pytest.raises(AttributeError):
C.x

with pytest.raises(AttributeError):
C.y

0 comments on commit dc1b5a0

Please sign in to comment.