Skip to content

Conversation

@davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Sep 3, 2025

Following on from #4390 I was exploring extract_argument.rs to understand if anything could be simplified, and I've found a way to move the definition of PyFunctionArgument for the #[pyclass] type references out of the macros and into generic code.

This should reduce the generated macro output a fair bit, hopefully beneficial for compile times 🤞

The main trick is to adjust the const generic specialization from IsOption to IsFromPyObject and then all the custom implementations in extract_argument.rs set that parameter to false, while all "normal" types will have that be discovered to be true.

While at it, I added a diagnostic::on_unimplemented to make the error messages a touch nicer.

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Sep 3, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 3, 2025

CodSpeed Performance Report

Merging #5408 will not alter performance

Comparing davidhewitt:simplify-extract-argument (6db6bb7) with main (ee29399)

Summary

✅ 98 untouched benchmarks

Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

This look great! I was actually looking yesterday as well to remove the IsOption special handling but ran into the case that we still need it for something like Option<&Bound>.

I think this is a much better and I really like that this only special cases the things that need it. Something like Option<&str> that can go through FromPyObject now will go through it instead of a mixture of PyFunctionArgument and FromPyObject.

Moving things out of the macros is always good 👍

Maybe we add a ui test for the new diagnostic?

_,
{ #pyo3_path::impl_::pyclass::IsOption::<#arg_ty>::VALUE }
>(
#pyo3_path::impl_::extract_argument::extract_argument(
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the compiler is now smart enough to figure this out via inference? Great 💯

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think due to the way the true / false const generic is working it happens to fall out nicely that each type only ever should have one implementation, so type inference can do it 🎉

}
)?
}
quote_arg_span! {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great to remove special casing of Options and reduce usage of option_wrapped_type 🥳

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, this felt like a great cleanup 😁

@Icxolu
Copy link
Contributor

Icxolu commented Sep 4, 2025

Also, this might mean that we can seal PyFunctionArgument now

@davidhewitt
Copy link
Member Author

Good point, added UI test and sealed 👍

Turns out that to seal I had to use the same const generic on IMPLEMENTS_FROMPYOBJECT to ensure the specialized implementations did not overlap with the blankets. I think that's still fine, will proceed to merge.

Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

Turns out that to seal I had to use the same const generic on IMPLEMENTS_FROMPYOBJECT to ensure the specialized implementations did not overlap with the blankets. I think that's still fine, will proceed to merge.

Makes sense I agree.

@davidhewitt davidhewitt added this pull request to the merge queue Sep 9, 2025
Merged via the queue into PyO3:main with commit f7e181e Sep 9, 2025
43 checks passed
@davidhewitt davidhewitt deleted the simplify-extract-argument branch September 9, 2025 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants