Skip to content

Change return type of from_list, from_sequence to PlainQuantity[Array[MagnitudeT]]#2258

Open
MatthieuDartiailh wants to merge 2 commits intohgrecco:masterfrom
MatthieuDartiailh:MatthieuDartiailh-Quantity.from_list-typing
Open

Change return type of from_list, from_sequence to PlainQuantity[Array[MagnitudeT]]#2258
MatthieuDartiailh wants to merge 2 commits intohgrecco:masterfrom
MatthieuDartiailh:MatthieuDartiailh-Quantity.from_list-typing

Conversation

@MatthieuDartiailh
Copy link
Contributor

Updated return type of from_list and from_sequence methods to support numpy arrays.

…[MagnitudeT]]

Updated return type of from_list and from_sequence methods to support numpy arrays.
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 15, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing MatthieuDartiailh:MatthieuDartiailh-Quantity.from_list-typing (8e338e3) with master (9325ea2)

Summary

✅ 448 untouched benchmarks

@andrewgsavage
Copy link
Collaborator

Thanks, this looks fine to merge.
Before I do so, is there a way to test these kind of typing issues to avoid them in the first place?

@MatthieuDartiailh
Copy link
Contributor Author

I did not check what was running in term of type check in the CI. I think mypy should be able to catch this but the rest of teh codebase may not be ready. Another options would be to run mypy on some select files with test assignment.

As a side note, there is still a minor annoyance in the behavior of this function in that it generates a PlainQuantity which is not indexable but since the magnitude is a NDArray, it is indexable and always is. Would it be possible to return a Quantity or another appropriate facet ?

>>> import pint
>>> q1 = pint.Quantity("1V")
>>> q2 = pint.Quantity("1V")
>>> pint.Quantity.from_list([q1, q2])[0]
<Quantity(1.0, 'volt')>

@andrewgsavage
Copy link
Collaborator

Would it be possible to return a Quantity or another appropriate facet ?

that would be ideal, though I'm not sure how to go about it

@jules-ch
Copy link
Collaborator

Array is not a generic though, can you change to have PlainQuantity[Array]

@MatthieuDartiailh
Copy link
Contributor Author

It is not ?!? Well this is sad since Numpy array is and the typing could be more explicit this way. Any idea how it would be to make Array generic ?

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.

PlainQuantity from_list and from_sequence typing is wrong

3 participants