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 expanding altreps with attributes #46

Merged
merged 3 commits into from
Oct 11, 2024
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
7 changes: 6 additions & 1 deletion rdata/parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,16 @@ def parse_R_object( # noqa: N802, C901, PLR0912, PLR0915
)

if self.expand_altrep:
is_object = info.object
info, value = self.expand_altrep_to_object(
info=altrep_info,
state=altrep_state,
)
attributes = altrep_attr
if altrep_attr.info.type != RObjectType.NILVALUE:
info.object = is_object
info.attributes = True
attributes_read = True
attributes = altrep_attr
else:
value = (altrep_info, altrep_state, altrep_attr)

Expand Down
Binary file not shown.
Binary file not shown.
46 changes: 45 additions & 1 deletion rdata/tests/test_rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,58 @@ def test_altrep_deferred_string(self) -> None:

def test_altrep_wrap_real(self) -> None:
"""Test alternative representation of wrap_real."""
data = rdata.read_rda(
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real.rda",
)
obj = parsed.object.value[0] # taking value as it's an rda file
assert obj.info.type == rdata.parser.RObjectType.REAL # sanity check
assert not obj.info.object
assert not obj.info.attributes
assert obj.attributes is None

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, {
"test_altrep_wrap_real": [3],
})

def test_altrep_wrap_real_attributes(self) -> None:
"""Test alternative representation of wrap_real with attributes."""
# File created in R with
# a = .Internal(wrap_meta(c(1, 2, 3), 0, 0)); attr(a, "foo") = "bar"; saveRDS(a, file="test_altrep_wrap_real_attributes.rds") # noqa: E501
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real_attributes.rds",
)
obj = parsed.object
assert obj.info.type == rdata.parser.RObjectType.REAL # sanity check
assert not obj.info.object
assert obj.info.attributes
assert obj.attributes is not None
assert obj.attributes.tag is not None
assert obj.attributes.tag.value.value == b"foo"
assert obj.attributes.value[0].value[0].value == b"bar"

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, [1., 2., 3.])

def test_altrep_wrap_real_class_attribute(self) -> None:
"""Test alternative representation of wrap_real with class attribute."""
# File created in R with
# a = .Internal(wrap_meta(c(1, 2, 3), 0, 0)); attr(a, "class") = "Date"; saveRDS(a, file="test_altrep_wrap_real_class_attribute.rds") # noqa: E501
parsed = rdata.parser.parse_file(
TESTDATA_PATH / "test_altrep_wrap_real_class_attribute.rds",
)
obj = parsed.object
assert obj.info.type == rdata.parser.RObjectType.REAL # sanity check
assert obj.info.object
assert obj.info.attributes
assert obj.attributes is not None
assert obj.attributes.tag is not None
assert obj.attributes.tag.value.value == b"class"
assert obj.attributes.value[0].value[0].value == b"Date"

data = rdata.conversion.convert(parsed)
np.testing.assert_equal(data, [1., 2., 3.])

def test_altrep_wrap_string(self) -> None:
"""Test alternative representation of wrap_string."""
data = rdata.read_rda(TESTDATA_PATH / "test_altrep_wrap_string.rda")
Expand Down
Loading