Skip to content

pyarrow: Small code simplifications - #9594

Merged
alamb merged 1 commit into
apache:mainfrom
Tpt:tpt/pyarrow-nits
Mar 31, 2026
Merged

pyarrow: Small code simplifications#9594
alamb merged 1 commit into
apache:mainfrom
Tpt:tpt/pyarrow-nits

Conversation

@Tpt

@Tpt Tpt commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

Makes the code simpler and more readable by relying on new PyO3 and Rust features. No behavior should have changed outside of an error message if __arrow_c_array__ does not return a tuple

What changes are included in this PR?

  • use .call_method0(M)? instead of .getattr(M)?.call0()
  • Use .extract() that allows more advanced features like directly extracting tuple elements
  • remove temporary variables just before returning
  • use &raw const and &raw mut pointers instead of casting and addr_of!

- use `.call_method0(M)?` instead of `.getattr(M)?.call0()`
- Use `.extract()` that allows more advanced features like directly extracting tuple elements
- remove temporary variables just before returning
- use &raw const and &raw mut pointers instead of casting and addr_of!
@Tpt
Tpt marked this pull request as ready for review March 20, 2026 17:28

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice cleanup to me -- thank you @Tpt
FYI @kylebarron in case you would also like a chance to review

Comment thread arrow-pyarrow/src/lib.rs
let capsule = value.getattr("__arrow_c_schema__")?.call0()?;
let capsule = capsule.cast::<PyCapsule>()?;
validate_pycapsule(capsule, "arrow_schema")?;
let capsule = value.call_method0("__arrow_c_schema__")?.extract()?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. I didn't know about &raw mut and &raw const

Comment thread arrow-pyarrow/src/lib.rs
Comment on lines -278 to -282
if !tuple.is_instance_of::<PyTuple>() {
return Err(PyTypeError::new_err(
"Expected __arrow_c_array__ to return a tuple.",
));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw previously there was a nice user-facing error here, while now the error generated from extract will be much more obtuse. Ideally this exception will never be raised except if the producer doesn't follow the spec correctly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make a follow on PR to keep the message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alamb
alamb merged commit 61b5763 into apache:main Mar 31, 2026
13 checks passed
@alamb

alamb commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Thanks again @Tpt and @kylebarron

alamb added a commit that referenced this pull request Apr 1, 2026
# Which issue does this PR close?

- Follow on to #9594

# Rationale for this change


@kylebarron says
#9594 (comment):

> fwiw previously there was a nice user-facing error here, while now the
error generated from extract will be much more obtuse. Ideally this
exception will never be raised except if the producer doesn't follow the
spec correctly.

# What changes are included in this PR?
Restore the nice error

# Are these changes tested?

yes, added a test

# Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.

If there are any breaking changes to public APIs, please call them out.
-->
@Tpt
Tpt deleted the tpt/pyarrow-nits branch April 3, 2026 15:12
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Jun 2, 2026
# Rationale for this change

Makes the code simpler and more readable by relying on new PyO3 and Rust
features. No behavior should have changed outside of an error message if
`__arrow_c_array__` does not return a tuple

# What changes are included in this PR?

- use `.call_method0(M)?` instead of `.getattr(M)?.call0()`
- Use `.extract()` that allows more advanced features like directly
extracting tuple elements
- remove temporary variables just before returning
- use &raw const and &raw mut pointers instead of casting and addr_of!
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Jun 2, 2026
# Which issue does this PR close?

- Follow on to apache#9594

# Rationale for this change


@kylebarron says
apache#9594 (comment):

> fwiw previously there was a nice user-facing error here, while now the
error generated from extract will be much more obtuse. Ideally this
exception will never be raised except if the producer doesn't follow the
spec correctly.

# What changes are included in this PR?
Restore the nice error

# Are these changes tested?

yes, added a test

# Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.

If there are any breaking changes to public APIs, please call them out.
-->
alamb pushed a commit that referenced this pull request Jun 22, 2026
Remove validate_pycapsule: pointer_checked already raises an exception
if the capsule type is wrong, and we wrap it into a nicer error

Also relies on a trait to associate types with their capsules names

Because we wrap the exception (equivalent of `raise X from Y`), the
original exception is still there

Follow up of  #9639 #9594
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants