Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that bare attributes with default None are removed too #556

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use is not

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