Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Recommended use of wraps and check for unitizing float functions with default args #1065

Closed
rkingsbury opened this issue Mar 30, 2020 · 2 comments

Comments

@rkingsbury
Copy link
Contributor

For performance and compatibility reasons, I need to write a function that accepts and returns floats, but I want to provide a "unitized" version of the function using @wraps or @check. I've run into some limitations, and I want to make sure I fully understand the intended use of these two decorators.

Suppose my function is

def f(a, b=10)
    return a * (1 / (2-b) ) **2

For the unitized version, I want to enforce that

  1. a has certain dimensions
  2. the returned value has the same units as a
  3. b is dimensionless

I can accomplish 1) with @check

f_unitized = ureg.check(('[length]'),('[dimensionless]'))(f)

and 2) and 3) with @wraps

f_unitized = ureg.wraps('=A', ('=A','dimensionless'))(f)

But, @wraps only works properly when b is passed explicitly. If the default value is used, it raises a ValueError (see #439).

ValueError: A wrapped function using strict=True requires quantity or a string for all arguments with not None units.

So, my questions are:

  1. Can the wraps and check functions be combined / chained to accomplish both dimensionality and units checking?
  2. What is the intended way to handle unfilled default arguments?
  3. Is there a better/different way to implement this that is more consistent with pint design principles?

Thank you!

@hgrecco
Copy link
Owner

hgrecco commented Mar 30, 2020

  1. wraps and check are not intendend to be combined. The first is intended to convert a standard function into a unit aware function. The wrapper convert the input into magnitudes of the chosen units which in turn are supplied to the wrapped function. The second only checks that the units or dimensions are right. So wraps converts (implicit check) and magnitud extraction.

  2. I would like to support that case.

  3. The right way would be to make wraps accept dimensions and fix the default value.

@hgrecco
Copy link
Owner

hgrecco commented Apr 27, 2023

Feel free to reopen if needed

Repository owner locked and limited conversation to collaborators Apr 27, 2023
@hgrecco hgrecco converted this issue into discussion #1755 Apr 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants