Skip to content

Quantity: add dunder methods #8

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

Merged
merged 8 commits into from
Apr 1, 2025

Conversation

andrewgsavage
Copy link
Contributor

transferred from quantity-dev/quantity-api#6

@lucascolley lucascolley changed the title Quantity dunders Quantity: add dunder methods Mar 17, 2025
@lucascolley lucascolley linked an issue Mar 17, 2025 that may be closed by this pull request
Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

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

I've added some tentative suggestions but I'm not sure that they are correct — @jorenham do my suggestions look closer to what we would want here?

@andrewgsavage
Copy link
Contributor Author

I feel like we need to get a type checker going to know what works and doesnt

@lucascolley
Copy link
Member

lucascolley commented Mar 17, 2025

I feel like we need to get a type checker going to know what works and doesnt

Yes, but we then also need a minimal an implementation to check against :)

@andrewgsavage
Copy link
Contributor Author

I feel like we need to get a type checker going to know what works and doesnt

Yes, but we then also need a minimal implementation to check against :)

does it have to be minimal? would a fork of pint (or another module) work?

@lucascolley
Copy link
Member

would a fork of pint (or another module) work?

yeah, I think so!

@jorenham
Copy link
Contributor

I've added some tentative suggestions but I'm not sure that they are correct — @jorenham do my suggestions look closer to what we would want here?

My mental typechecker did not report any errors 👌🏻

@jorenham
Copy link
Contributor

I feel like we need to get a type checker going to know what works and doesnt

Exactly; writing annotations without a type-checker is like programming without a compiler or interpreter

Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

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

the linked issue also suggests:

__floordiv__, __mod__, __divmod__, shifts, &, ^, |, ...

@lucascolley lucascolley force-pushed the quantity_dunder branch 2 times, most recently from 7623c3f to 5eed30f Compare March 28, 2025 21:45
@lucascolley
Copy link
Member

type checkers are happy! Any opinions on #5 (comment) @nstarman @neutrinoceros ?

andrewgsavage and others added 7 commits April 1, 2025 11:50
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
@lucascolley
Copy link
Member

alright, let's get this in, we can always revisit other methods as and when need arises. thanks all!

@lucascolley lucascolley merged commit 05af87c into quantity-dev:main Apr 1, 2025
1 check passed
@andrewgsavage
Copy link
Contributor Author

oh I meant to ask if we want to keep rpow

@lucascolley
Copy link
Member

I think so. If we can do x ** q.value, we should also be able to do x ** q.

@andrewgsavage
Copy link
Contributor Author

that only makes sense for a unitless quantity, you shouldn't be able to do 2 ** Quantity(2, "m"). I think its reasonable for a library to implement this or not implement it

@lucascolley
Copy link
Member

ah yeah, good point, we should probably remove it then

@jorenham
Copy link
Contributor

jorenham commented Apr 1, 2025

that only makes sense for a unitless quantity, you shouldn't be able to do 2 ** Quantity(2, "m"). I think its reasonable for a library to implement this or not implement it

not sure if it's a thing, but maybe it makes sense for logarithmic units like decibels?

@nstarman
Copy link
Contributor

nstarman commented Apr 1, 2025

Agreed. This feels like an API vs implementation issue. If you have access to the runtime, which we do not, then you can distinguish between dimensionful and dimensionless quantities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quantity: dunder methods
4 participants