Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add num-rational support for Python's fractions.Fraction type #4148

Merged
merged 9 commits into from
May 9, 2024

Conversation

dmatos2012
Copy link
Contributor

Hi,

This should close #3105 by adding support for Python's fractions.Fraction type using num-rational.

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.

Thanks! Overall this does look good to me.

I think we need to make one small change to not leak reference counts (but someone with a bit more CPython API experience than me should probably double check).

src/conversions/num_rational.rs Outdated Show resolved Hide resolved
src/conversions/num_rational.rs Outdated Show resolved Hide resolved
src/conversions/num_rational.rs Outdated Show resolved Hide resolved
@dmatos2012
Copy link
Contributor Author

Thanks for review @Icxolu. I have addressed your remarks :).

Copy link

codspeed-hq bot commented May 2, 2024

CodSpeed Performance Report

Merging #4148 will not alter performance

Comparing dmatos2012:num-rational-conversion (7941a93) with main (635cb80)

Summary

✅ 69 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.

I spotted one small problem, otherwise this look good to me now🚀 Could you also rebase on main please, to unblock CI.

src/conversions/num_rational.rs Show resolved Hide resolved
src/conversions/num_rational.rs Show resolved Hide resolved
src/conversions/num_rational.rs Show resolved Hide resolved
Comment on lines 166 to 177
#[test]
fn test_fraction_as_tuples() {
Python::with_gil(|py| {
let locals = PyDict::new_bound(py);
let py_bound = py.run_bound(
"import fractions\npy_frac = fractions.Fraction(fractions.Fraction((10,)))",
None,
Some(&locals),
);
assert!(py_bound.is_err());
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this was a bit unclear (and part of my fault for misremembering call1 from above), but what I meant was just the test of into_py (which you have in the prop tests below). This now just tests the behavior of fractions.Fraction which is not that useful for us. Perhaps we can instead add special case of the test_int_roundtrip that also runs on wasm32.

Then this is good to go 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem! Hopefully this time is the charm :) Thanks for the review

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.

Looks good to me now! Thanks for implementing 🚀 (and for the patience with my review 😁)

@Icxolu Icxolu added this pull request to the merge queue May 9, 2024
Merged via the queue into PyO3:main with commit 2d19b7e May 9, 2024
43 checks passed
@dmatos2012 dmatos2012 deleted the num-rational-conversion branch May 10, 2024 07:45
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.

Add conversion between fractions.Fraction and num-rational types
2 participants