Skip to content

Commit

Permalink
Ensure that bare attributes with default None are removed too
Browse files Browse the repository at this point in the history
Fixes #523
  • Loading branch information
hynek committed Jul 21, 2019
1 parent b5e825d commit 0e41137
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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 0e41137

Please sign in to comment.