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

Fix deepcopy of TaggedList #788

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Fix bug preventing diff of files containing ndarray-1.0.0
objects in simplified form. [#786]

- Fix bug causing duplicate elements to appear when calling
``copy.deepcopy`` on a ``TaggedList``. [#788]

2.6.0 (2020-04-22)
------------------

Expand Down
17 changes: 17 additions & 0 deletions asdf/tagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"""

from collections import UserDict, UserList, UserString
from copy import deepcopy, copy


__all__ = ['tag_object', 'get_tag']
Expand Down Expand Up @@ -63,6 +64,14 @@ def __eq__(self, other):
self.data == other.data and
self._tag == other._tag)

def __deepcopy__(self, memo):
data_copy = deepcopy(self.data, memo)
return TaggedDict(data_copy, self._tag)

def __copy__(self):
data_copy = copy(self.data)
return TaggedDict(data_copy, self._tag)


class TaggedList(Tagged, UserList, list):
"""
Expand All @@ -81,6 +90,14 @@ def __eq__(self, other):
self.data == other.data and
self._tag == other._tag)

def __deepcopy__(self, memo):
data_copy = deepcopy(self.data, memo)
return TaggedList(data_copy, self._tag)

def __copy__(self):
data_copy = copy(self.data)
return TaggedList(data_copy, self._tag)


class TaggedString(Tagged, UserString, str):
"""
Expand Down
82 changes: 82 additions & 0 deletions asdf/tests/test_tagged.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from copy import deepcopy, copy

from asdf.tagged import TaggedList, TaggedDict, TaggedString


def test_tagged_list_deepcopy():
original = TaggedList([0, 1, 2, ["foo"]], "tag:nowhere.org:custom/foo-1.0.0")
result = deepcopy(original)
assert result == original
assert result.data == original.data
assert result._tag == original._tag
original.append(4)
assert len(result) == 4
original[3].append("bar")
assert len(result[3]) == 1


def test_tagged_list_copy():
original = TaggedList([0, 1, 2, ["foo"]], "tag:nowhere.org:custom/foo-1.0.0")
result = copy(original)
assert result == original
assert result.data == original.data
assert result._tag == original._tag
original.append(4)
assert len(result) == 4
original[3].append("bar")
assert len(result[3]) == 2


def test_tagged_list_isinstance():
value = TaggedList([0, 1, 2, ["foo"]], "tag:nowhere.org:custom/foo-1.0.0")
assert isinstance(value, list)


def test_tagged_dict_deepcopy():
original = TaggedDict({"a": 0, "b": 1, "c": 2, "nested": {"d": 3}}, "tag:nowhere.org:custom/foo-1.0.0")
result = deepcopy(original)
assert result == original
assert result.data == original.data
assert result._tag == original._tag
original["e"] = 4
assert len(result) == 4
original["nested"]["f"] = 5
assert len(result["nested"]) == 1


def test_tagged_dict_copy():
original = TaggedDict({"a": 0, "b": 1, "c": 2, "nested": {"d": 3}}, "tag:nowhere.org:custom/foo-1.0.0")
result = copy(original)
assert result == original
assert result.data == original.data
assert result._tag == original._tag
original["e"] = 4
assert len(result) == 4
original["nested"]["f"] = 5
assert len(result["nested"]) == 2


def test_tagged_dict_isinstance():
value = TaggedDict({"a": 0, "b": 1, "c": 2, "nested": {"d": 3}}, "tag:nowhere.org:custom/foo-1.0.0")
assert isinstance(value, dict)


def test_tagged_string_deepcopy():
original = TaggedString("You're it!")
original._tag = "tag:nowhere.org:custom/foo-1.0.0"
result = deepcopy(original)
assert result == original
assert result._tag == original._tag


def test_tagged_string_copy():
original = TaggedString("You're it!")
original._tag = "tag:nowhere.org:custom/foo-1.0.0"
result = copy(original)
assert result == original
assert result._tag == original._tag


def test_tagged_string_isinstance():
value = TaggedString("You're it!")
assert isinstance(value, str)