diff --git a/changelog.d/ITL-1591.md b/changelog.d/ITL-1591.md index d0f798265..77d98cdc8 100644 --- a/changelog.d/ITL-1591.md +++ b/changelog.d/ITL-1591.md @@ -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 diff --git a/tests/test_semantic_types/test_narrow_float_arrow_compatibility.py b/tests/test_semantic_types/test_narrow_float_arrow_compatibility.py index f4407c939..91fdb3e02 100644 --- a/tests/test_semantic_types/test_narrow_float_arrow_compatibility.py +++ b/tests/test_semantic_types/test_narrow_float_arrow_compatibility.py @@ -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"): @@ -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__, @@ -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