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

Make units.decorators._validate_arg_value part of the public API. #10678

Open
hamogu opened this issue Aug 26, 2020 · 3 comments
Open

Make units.decorators._validate_arg_value part of the public API. #10678

hamogu opened this issue Aug 26, 2020 · 3 comments

Comments

@hamogu
Copy link
Member

hamogu commented Aug 26, 2020

Description

The purpose of the code in units.decorators is to reduce the boilerplate
code needed to check for the correct unit input. In many cases, the decorator
will do, but in more complex it will not. In the latter case, I could still save a lot
of work by reusing the building blocks of the decorator,
most importantly units.decorators._validate_arg_value which does the actual
checking for the decorator.

Additional context

In my package, I have a function that should accept either a quantity or a callable
that then derives a quantity in some way and, to make matters worse, the
argument I need to check might come in **kwargs.
So, I need to check the input arguments myself. units.decorators._validate_arg_value
can do half the work if only I'm confident to rely on a private function.

@mhvk
Copy link
Contributor

mhvk commented Aug 26, 2020

Seems reasonable (though perhaps at the decorators level? I.e., not import at the top?) Note that @nstarman has a coupe of PR in progress on revamping quantity_input altogether - it might be interesting to see if your use case would be supportable -- see #10655 and #10662 - it would be good to get more input from people who actually use quantity_input (I tend to do little validation, with the excuse of "Easier To Ask Forgiveness Than Permission").

@nstarman
Copy link
Member

nstarman commented Aug 26, 2020

This looks like a very interesting use case. The plan for the quantity_input refactor is to introduce a new class UnitSpec that, in combination with the function annotations, determines how to interact with the input. Since looping through the actual arguments is handled by inspect.BoundArgument this would automatically handle kwargs and probably not need a helper function _validate_arg_value. I hadn't yet considered multi-type kwargs, but an implementation might look like

@quantity_input(arg_in_kwargs=T.Union[u.m, T.Callable])
def func(**kwargs):
    if callable(kwargs["arg_in_kwargs"]):
        print("doing something")
    ...

Where T is the python typing library.

Also a possibility is that UnitSpec could implement a decorator for single-argument interactions.

This full set of features is a number of PRs down the line and thus a whiles away. If I could suggest a provisional change, expose_validate_arg_value as a static method of QuantityInput. That might be easier to continue to support and a better place to put it since _validate_arg_value is not a decorator.

@hamogu
Copy link
Member Author

hamogu commented Sep 30, 2020

I don't think I need an intermediate solution that requires a code change. There are tow obvious intermediate solutions already that are virtually no work:

  • don't validate at all for now. It's going to fail later in the code, and that's OK for now.
  • use the existing private function.

I think I do with the first one for now. However, if this use can can be supported with moderate extra complexity in the future, that would be great!

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

No branches or pull requests

3 participants