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
Making hug a no-op when running in python2? #435
Comments
Sure, why not? Just wrap the decorators. def my_get(func, *args, **kwargs):
if 'hug' in globals():
return hug.get(func, *args, **kwargs)
return func |
Thanks - that helps. Now I'm stuck on how to add function annotations, as required for proper error handling around types, since code like this naturally leads to syntax errors under python2:
The suggestion at Function annotations — Python-Future documentation to provide them by adding an
And then visit I guess that's because the hug decorators are processing the function before the annotation is added, and I don't know if there's a way to finesse that without things getting too ugly. And I don't know if I'm going to keep running into things like this, as I explore other hug features that I want to take advantage of. Also note that if I add the
|
Try this: import hug
def nminhug2(alpha=0.1, margin=0.05):
if margin > 1.0:
return -1
return 96
nminhug2.__annotations__ = {'alpha': hug.types.float_number, 'margin': hug.types.float_number}
nminhug2 = hug.get(examples='alpha=0.1&margin=0.05')(nminhug2)
nminhug2 = hug.local()(nminhug2) |
Very helpful! I also figured out how to make a substitute
and then use your tips on annotating and manually decorating the functions. The
So now I can define a library that provides a lovely hug UI when used in python3 code, but also works with python2 code that doesn't use the hug UI. If this approach is useful for others, we might want to flesh this out and include it in hug. |
I'm guessing that there is actually also a way to add the annotations via a decorator, which would also allow the code to use decorations and look much cleaner and more similar to normal hug code. |
....and just as I hoped, this seems to work fine for adding annotations in a way that works for python3 and doesn't cause errors in python2:
So combined with the hug_noop.py solution above, this makes things very clean. |
I'm glad I could help ^_^ Maybe |
Going to go ahead and close this as I think @cag did a great job answering it. Glad you found a working solution, Thanks! ~Timothy |
Hug is great. I'd love to take advantage of it in my code when I'm using python3, e.g. to provide a CLI interface.
But I'd also like my code to run under python2.
Can I special-case the import of hug itself, then somehow mock out the decorators, so they just don't get in the way if the code is run via python2?
The text was updated successfully, but these errors were encountered: