Skip to content

Commit 6a9e2d5

Browse files
GH-38575: [Python] Include metadata when creating pa.schema from PyCapsule (#41538)
### Rationale for this change Fixes the dropped `pa.schema` metadata reported in #38575, which was introduced in #37797. ### What changes are included in this PR? Passes through the `metadata` to the short-circuited `Schema` created with `_import_from_c_capsule`. ### Are these changes tested? Yes - added `metadata` to the existing test. ### Are there any user-facing changes? I'm not sure this quite rises to the `(b) a bug that caused incorrect or invalid data to be produced,` condition, but I added that note to be safe since the resulting schema is "incorrect" (and broke some round-trip tests on my end after a pyarrow update): **This PR contains a "Critical Fix".** * GitHub Issue: #38575 Lead-authored-by: Jacob Hayes <jacob.r.hayes@gmail.com> Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
1 parent dc973c2 commit 6a9e2d5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

python/pyarrow/tests/test_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,10 +1331,13 @@ def __init__(self, schema):
13311331
def __arrow_c_schema__(self):
13321332
return self.schema.__arrow_c_schema__()
13331333

1334-
schema = pa.schema([pa.field("field_name", pa.int32())])
1334+
schema = pa.schema([pa.field("field_name", pa.int32())], metadata={"a": "b"})
1335+
assert schema.metadata == {b"a": b"b"}
13351336
wrapped_schema = Wrapper(schema)
13361337

13371338
assert pa.schema(wrapped_schema) == schema
1339+
assert pa.schema(wrapped_schema).metadata == {b"a": b"b"}
1340+
assert pa.schema(wrapped_schema, metadata={"a": "c"}).metadata == {b"a": b"c"}
13381341

13391342

13401343
def test_field_import_c_schema_interface():

python/pyarrow/types.pxi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5332,7 +5332,10 @@ def schema(fields, metadata=None):
53325332
if isinstance(fields, Mapping):
53335333
fields = fields.items()
53345334
elif hasattr(fields, "__arrow_c_schema__"):
5335-
return Schema._import_from_c_capsule(fields.__arrow_c_schema__())
5335+
result = Schema._import_from_c_capsule(fields.__arrow_c_schema__())
5336+
if metadata is not None:
5337+
result = result.with_metadata(metadata)
5338+
return result
53365339

53375340
for item in fields:
53385341
if isinstance(item, tuple):

0 commit comments

Comments
 (0)