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

we should have better documentation and error message for with-decorator #1624

Closed
pyx opened this issue May 29, 2018 · 8 comments · Fixed by #2270
Closed

we should have better documentation and error message for with-decorator #1624

pyx opened this issue May 29, 2018 · 8 comments · Fixed by #2270

Comments

@pyx
Copy link
Contributor

pyx commented May 29, 2018

Scenario 1

hy 0.14.0 using CPython(default) 3.6.5 on Linux
=> (with-decorator identity (defn a [] 42))
=> (with-decorator identity (fn [] 42))
  File "<input>", line 1, column 1

  (with-decorator identity (fn [] 42))
  ^----------------------------------^
HyTypeError: Decorated a non-function

It works on anonymous functions before (for example, 0.12.1), I believe decorating (fn ...) is useful sometimes. In python, there is no 'auto return', no multi-line lambda and def is not an expression, so we must use a named function, and the name is usually of no importance beside being used to be returned. To me, it looks good enough to have an fn as the last form.

For Example:

(defn my-decorator [f]
  (with-decorator (functools.wraps f)
    (fn [&rest args &kwargs kwargs]
      ...)))

I think we should support fn (as before) here, if not, we should mention that in the documentation, and using a better error message, fn is obviously a function.

Scenario 2

=> (with-decorator identity (defclass C))
=> (defclass C)
=> (with-decorator identity C)
  File "<input>", line 1, column 1

  (with-decorator identity C)
  ^-------------------------^
HyTypeError: Decorated a non-function

Here, feeding a type that's already been defined (not on the spot) raises an exception, which is reasonable BTW, but we should have more descriptive error message, with-decorator supports classes, too, and the wording implies only functions can be used here.

@Kodiologist
Copy link
Member

Kodiologist commented May 29, 2018

I believe decorating (fn ...) is useful sometimes.

It shouldn't be, or else something is wrong. Instead of (with-decorator f (fn ...)), just say (f (fn ...)).

@pyx
Copy link
Contributor Author

pyx commented May 29, 2018

Fair enough, so the problem remaining is better error message.

@pyx
Copy link
Contributor Author

pyx commented May 29, 2018

Playing with it a bit, I think there is a discrepancy here.

How can we translate this Python code into Hy code?

import functools

def the_answer(f):
    @functools.wraps(f)
    def decorated():
        return 42
    return decorated

@the_answer
def tell_me():
    return 0

tell_me()  # -> 42

Literal translation (sans the return, which is not lispy) does not work here:

(import functools)

(defn the-answer [f]
  (with-decorator (functools.wraps f)
    (defn decorated []
      42)))

(with-decorator the-answer
  (defn tell-me [] 0))

(tell-me)  ;; raises "TypeError: 'NoneType' object is not callable"

I know sometime ago, defn had been re-designed as returning None, combining this fact with the current behaviour of with-decorator, it breaks the "Hy <-> Python interop" to certain extent.

Maybe the true literal translation with (return decorated) is what's preferred?

@Kodiologist
Copy link
Member

Yeah, you need to put decorated, or equivalently (return decorated), at the end of the definition of the-answer.with-decorator returns None, not the decorated function. If you want to be more concise, you can write

(defn the-answer [f]
  ((functools.wraps f) (fn [] 42)))

@pyx
Copy link
Contributor Author

pyx commented May 30, 2018

docmebro, maybe? Cause the proposed way is not lispy (for my own definition of lispy, that is 😄).
I know with-decorator support more than one decorators, but returning None makes nesting with-decorator impossible (not that I think that is a good idea).

@Kodiologist
Copy link
Member

returning None makes nesting with-decorator impossible (not that I think that is a good idea).

No, nesting with-decorator still works:

(defn dec1 [f]
  (fn []
    (print "in dec1")
    (f)))

(defn dec2 [f]
  (fn []
    (print "in dec2")
    (f)))

(with-decorator dec1
  (with-decorator dec2
    (defn foo []
      42)))

(print (foo))

This prints:

in dec1
in dec2
42

@pyx
Copy link
Contributor Author

pyx commented Jun 1, 2018

My fault. I mistakenly thought that because with-decorator evaluated to None, nesting does not work. Didn't know that it is a special form on AST level until I just checked hy.compiler.py, I thought it was either on macro's level or run-time level.

Thanks for your clarification.

@Kodiologist
Copy link
Member

Right, it has to be a special form because it directly modifies attributes of AST nodes.

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 a pull request may close this issue.

2 participants