最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hy - Defn async with decorators and types - Stack Overflow

programmeradmin0浏览0评论

I saw from Hy docs

(defn :async [decorator1 decorator2] :tp [T1 T2] #^ annotation name [params] …)

This is working without the :tp (async, decorators, return type are working fine).

  (defn :async [(.get app "directions/{direction_name}")] 
    #^ str get-direction [direction-name]
    (print "---------------"  directon-name)
    "response")

How to add the :tp also? I tried this and many others and didn't work.

(defn :async [(.get app "directions/{direction_name}")] 
    :tp [#^ str : T]
    #^ str get-direction [direction-name : T]
    (print "---------------"  directon-name)
    "response")
Traceback (most recent call last):
  File "/home/white/.local/bin/hy2py", line 8, in <module>
    sys.exit(hy2py_main())
  File "index.hy", line 76
    :tp [#^ str : T]
    ^
hy.errors.HySyntaxError: parse error for pattern macro 'defn': got unexpected token: hy.models.List([
  hy.models.Expression([
    hy.models.Expression([
      hy.models.Symbol('.'),
      hy.models.Symbol('None'),
      hy.models.Symbol('get')]),
    hy.models.Symbol('app'),
    hy.models.String('directions/{direction_name}')])]), expected: Symbol

Minimal example
I want to know how give the types of the arguments, i guess i have wrong syntax because i am getting error.

(defn  :tp [#^ int T] test [i : T]
  i)

(test 10)

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 288, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/home/white/visual-studio/fastapi/index.hy", line 59
    (defn  :tp [#^ int T] test [i : T]
               ^
hy.errors.HySyntaxError: parse error for pattern macro 'defn': got unexpected token: :, expected: end of macro call: hy.models.List([
  hy.models.Expression([
    hy.models.Symbol('annotate'),
    hy.models.Symbol('T'),
    hy.models.Symbol('int')])]), expected: end of macro call
    

Solution (based on accepted answer, if anyone stucks in future)

(defn :async [(.get app "directions/{direction_name}")] 
    #^ str get-direction [#^ str direction-name]
    "response")

With hy2py it generates

@app.get('directions/{direction_name}')
async def get_direction(direction_name: str) -> str:
    return 'response'

I saw from Hy docs

(defn :async [decorator1 decorator2] :tp [T1 T2] #^ annotation name [params] …)

This is working without the :tp (async, decorators, return type are working fine).

  (defn :async [(.get app "directions/{direction_name}")] 
    #^ str get-direction [direction-name]
    (print "---------------"  directon-name)
    "response")

How to add the :tp also? I tried this and many others and didn't work.

(defn :async [(.get app "directions/{direction_name}")] 
    :tp [#^ str : T]
    #^ str get-direction [direction-name : T]
    (print "---------------"  directon-name)
    "response")
Traceback (most recent call last):
  File "/home/white/.local/bin/hy2py", line 8, in <module>
    sys.exit(hy2py_main())
  File "index.hy", line 76
    :tp [#^ str : T]
    ^
hy.errors.HySyntaxError: parse error for pattern macro 'defn': got unexpected token: hy.models.List([
  hy.models.Expression([
    hy.models.Expression([
      hy.models.Symbol('.'),
      hy.models.Symbol('None'),
      hy.models.Symbol('get')]),
    hy.models.Symbol('app'),
    hy.models.String('directions/{direction_name}')])]), expected: Symbol

Minimal example
I want to know how give the types of the arguments, i guess i have wrong syntax because i am getting error.

(defn  :tp [#^ int T] test [i : T]
  i)

(test 10)

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 288, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/home/white/visual-studio/fastapi/index.hy", line 59
    (defn  :tp [#^ int T] test [i : T]
               ^
hy.errors.HySyntaxError: parse error for pattern macro 'defn': got unexpected token: :, expected: end of macro call: hy.models.List([
  hy.models.Expression([
    hy.models.Symbol('annotate'),
    hy.models.Symbol('T'),
    hy.models.Symbol('int')])]), expected: end of macro call
    

Solution (based on accepted answer, if anyone stucks in future)

(defn :async [(.get app "directions/{direction_name}")] 
    #^ str get-direction [#^ str direction-name]
    "response")

With hy2py it generates

@app.get('directions/{direction_name}')
async def get_direction(direction_name: str) -> str:
    return 'response'
Share Improve this question edited Feb 3 at 19:52 Takis asked Feb 1 at 23:10 TakisTakis 8,7052 gold badges16 silver badges27 bronze badges 2
  • Try a minimal self-contained example, and show what you mean when you say it doesn't work. – Kodiologist Commented Feb 1 at 23:45
  • I tried minimal example also before sending it, i added one in the question, i probably have a syntax error, if you can help me, i am new in Hy, giving a simple working example of how to use the :tp would be helpful. – Takis Commented Feb 2 at 18:21
Add a comment  | 

1 Answer 1

Reset to default 1

The lambda list [i : T] is nonsensical. If you want a parameter named i annotated to have the type T, which would be written i : T in Python, that's #^ T i or (annotate i T) in Hy.

发布评论

评论列表(0)

  1. 暂无评论