Skip to content

Commit 7201b35

Browse files
committed
metadata with non-string objects
1 parent b3ba223 commit 7201b35

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "affinity"
7-
version = "0.8.1"
7+
version = "0.8.2"
88
description = "Module for creating well-documented datasets, with types and annotations."
99
authors = [
1010
{ name = "Alex Kislukhin" }

test_affinity.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22

3+
import ast
34
import duckdb
45
import numpy as np
56
import pandas as pd
@@ -11,7 +12,7 @@
1112
duckdb.sql("SET python_scan_all_frames=true")
1213

1314
try:
14-
import polars
15+
import polars # noqa: F401
1516

1617
NO_POLARS = False
1718
except ImportError:
@@ -328,6 +329,32 @@ class cDataset(af.Dataset):
328329
cDataset().sql("SELECT v2 FROM df") # "df" != last test's data_a.df
329330

330331

332+
@pytest.mark.skipif(NO_PYARROW, reason="pyarrow is not installed")
333+
def test_objects_as_metadata():
334+
class aDataset(af.Dataset):
335+
"""Objects other than strings can go into metadata."""
336+
337+
v1 = af.VectorBool(comment={"x": 1, "y": "z"})
338+
v2 = af.VectorF32(comment=list("abc"))
339+
340+
def try_ast_literal_eval(x: str):
341+
try:
342+
return ast.literal_eval(x)
343+
except (SyntaxError, ValueError):
344+
return x
345+
346+
data = aDataset(v1=[True], v2=[1 / 2], v3=[3])
347+
test_file_arrow = Path("test_arrow.parquet")
348+
data.to_parquet(test_file_arrow, engine="arrow")
349+
pf = pyarrow.parquet.ParquetFile(test_file_arrow)
350+
pf_metadata = pf.schema_arrow.metadata
351+
decoded_metadata = {
352+
k.decode(): try_ast_literal_eval(v.decode()) for k, v in pf_metadata.items()
353+
}
354+
assert decoded_metadata.get("v1") == aDataset.v1.comment
355+
assert decoded_metadata.get("v2") == aDataset.v2.comment
356+
357+
331358
@pytest.mark.skipif(NO_POLARS, reason="polars is not installed")
332359
@pytest.mark.skipif(NO_PYARROW, reason="pyarrow is not installed")
333360
def test_to_parquet_with_metadata():

0 commit comments

Comments
 (0)