Skip to content
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
2 changes: 2 additions & 0 deletions changelog.d/ITL-1591.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
The check follows nested Python types and stored fields of user classes. Two distinct
values can narrow to one stored value and lose an entry. Declare the key or element
as `float` to retain double precision.
- The Arrow source compatibility tests use the same synthetic class identities
across supported Python versions.

### Known limitations

Expand Down
24 changes: 14 additions & 10 deletions tests/test_semantic_types/test_narrow_float_arrow_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def _ipc_storage_round_trip(table: pa.Table) -> pa.Table:
return pa.ipc.open_file(buffer.getvalue()).read_all()


def _snapshot_dataclass(name: str, fields: list[tuple[str, object]]) -> type:
"""Create a class with the module name embedded in the main snapshot."""
cls = make_dataclass(name, fields)
# make_dataclass handles namespace["__module__"] differently on 3.11 and
# newer Python versions. Set the identity explicitly after construction.
cls.__module__ = "tests.test_semantic_types.test_narrow_float_arrow_compatibility"
return cls


def _arrow_input(case: str) -> tuple[pa.Table, contexts.DataContext]:
context = _context()
if case.startswith("custom"):
Expand Down Expand Up @@ -128,14 +137,11 @@ def _arrow_input(case: str) -> tuple[pa.Table, contexts.DataContext]:
# The writer owns the original class registrations. The reader either
# registers an incompatible class of the same name or has neither class.
writer = _context().type_converter
namespace = {"__module__": "itl1591_arrow_compatibility"}
if case == "newer-class":
original = make_dataclass(
"Entries", [("entries", dict[str, int])], namespace=namespace
)
original = _snapshot_dataclass("Entries", [("entries", dict[str, int])])
item = original(entries={"string": 4})
replacement = make_dataclass(
"Entries", [("entries", dict[np.float32, int])], namespace=namespace
replacement = _snapshot_dataclass(
"Entries", [("entries", dict[np.float32, int])]
)
assert (replacement.__module__, replacement.__qualname__) == (
original.__module__,
Expand All @@ -149,10 +155,8 @@ def _arrow_input(case: str) -> tuple[pa.Table, contexts.DataContext]:
set[np.float32] if case.endswith("set") else dict[np.float32, int]
)
value = {np.float32(0.5)} if case.endswith("set") else {np.float32(0.5): 4}
inner = make_dataclass(
"Inner", [("entries", annotation)], namespace=namespace
)
outer = make_dataclass("Outer", [("nested", inner)], namespace=namespace)
inner = _snapshot_dataclass("Inner", [("entries", annotation)])
outer = _snapshot_dataclass("Outer", [("nested", inner)])
item = outer(nested=inner(entries=value))
table = writer.python_dicts_to_arrow_table([{"id": 1, "k": item}])
return _ipc_storage_round_trip(table), context
Expand Down
Loading