Skip to content

add PyInt constructor for all supported number types (i32, u32, i64, u64, isize, usize)#4984

Merged
bschoenmaeckers merged 11 commits intoPyO3:mainfrom
Trolldemorted:pyint_constructor
Mar 19, 2025
Merged

add PyInt constructor for all supported number types (i32, u32, i64, u64, isize, usize)#4984
bschoenmaeckers merged 11 commits intoPyO3:mainfrom
Trolldemorted:pyint_constructor

Conversation

@Trolldemorted
Copy link
Copy Markdown
Contributor

Solves a part of #2221.

I deliberately did not implement support for PyLong_FromString because I don't feel familiar enough with the codebase to do this in my first PR.

I am aware that my ToPyInt trait will not work with PyLong_FromString, because PyLong_FromString takes more parameters than just a single value. I acknowledge that changing the trait to also accommodate PyLong_FromString will be a breaking change, but one could also add a dedicated constructor for that instead of squeezing it into new.

Copy link
Copy Markdown
Member

@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 for working on this! In my opinion we should not introduce a new trait for this. This basically reimplements a subset of IntoPyObject. If we want to provide a generic constructor here, I think we should reuse the existing implementations and be generic over T: IntoPyObject<Target = PyInt, Error = Infallible>. I think that would cover all the (un)signed integer conversion. It would not cover f64 but I don't think that's a huge deal, it can easily be cast in Rust first and it is probably better to have this conversion explicit anyway.

@Trolldemorted
Copy link
Copy Markdown
Contributor Author

Trolldemorted commented Mar 18, 2025

Oh so borderline everything already exists through int_convert_u64_or_i64 and friends, I didn't realize that! I have pushed two more commits that clean up the mess.

I presume the Netlify tests are failing because of things beyond my control?

Copy link
Copy Markdown
Member

@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.

Thank you! Two small style suggestions otherwise LGTM 🚀

Comment thread src/types/num.rs
Comment thread src/types/num.rs Outdated
@Icxolu Icxolu added this pull request to the merge queue Mar 19, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 19, 2025
@Icxolu Icxolu added this pull request to the merge queue Mar 19, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 19, 2025
@bschoenmaeckers bschoenmaeckers changed the title add PyInt constructor for all supported number types (i32, u32, i64, u64, isize, usize, f64) add PyInt constructor for all supported number types (i32, u32, i64, u64, isize, usize) Mar 19, 2025
@bschoenmaeckers bschoenmaeckers added this pull request to the merge queue Mar 19, 2025
@davidhewitt
Copy link
Copy Markdown
Member

I have some ideas how to make the cache situation better so that the cargo-xwin cache is less likely to get evicted; I will try to push those tonight.

Merged via the queue into PyO3:main with commit 3454ab6 Mar 19, 2025
48 checks passed
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.

4 participants