-
Notifications
You must be signed in to change notification settings - Fork 373
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
add type annotations #640
Comments
so, syntax proposals? |
def foo(a: int, b: str) -> int:
return 0
# Then, given:
foo.__annotations__
# We get:
{'b': <class 'str'>, 'return': <class 'int'>, 'a': <class 'int'>} Currently only given meaning by third party stuff. Let's allow setting them. Who knows some similar syntax for Lisplikes? |
There are similar libraries for many lisp likes. As long as it’s progressive it should be fine. I hate type annotations unless I really need them. Sometimes duck-typing is actually useful. http://docs.racket-lang.org/ts-guide/ A more progressive-enhancement approach: http://www.lispworks.com/documentation/HyperSpec/Body/04_bc.htm But not as popular. Cheers On Aug 22, 2014, at 12:53 PM, Paul Tagliamonte notifications@github.com wrote:
|
For functions, I was thinking of something like this: (defn my-func [[a int] [b int]]
(print 123)
) BTW, Hy has no way of representing function annotations, does it? If it did, then it could be used on top of mypy. |
Not a bad syntax. Yeah not yet, that's what this bug is for! :D
|
Maybe that same syntax could be used for annotations? That way, people can use libraries like plac from Hy, too. |
Erm yeah. I was thinking 3.4 annotations. Bug subject is a typo :)
|
Clojure uses |
Any further thoughts on this? It be cool to be able to support |
It would be cool but a bit pointless. You wouldn't be able to type-check any code yet. We'd probably have to make a lightweight mypy frontend that compiles Hy files to typed_ast (which is identical to the ast module, but with type comment support)... |
With syntax like |
Okay, thought a little bit about this, read more details where Python is going, and played with some code. Apologize upfront if my terminology is off. First is we should be able to get away with always emitting strings for annotations. With PEP 563 coming, this is effectively going to become python's default behaviour anyways. Tools relying on annotations should already support deferred evaluation. And I think we have two options in how to implement it, one which might be easier than the other: Emit annotations in the AST:This means syntax support, so that something like this: (def foo ^int)
(defn bar ^int [x ^int] ...) Would turn into: foo: int
def bar(x: int) -> int:
... We'd have to teach the compiler to emit Manually build
|
@kirbyfan64 another option, but bare minimum, would be to just generate stub files of the function calls. |
Oh, an for what its worth, emitting (defmacro deftuple [name &rest fields]
`(do
(import [typing [NamedTuple]])
(defclass ~name [NamedTuple]
(def __annotations__ (dict ~fields))))) Not sure if you guys would take something like this, polished up, into contrib though. |
Provided by #1810. |
they're really neat. let's do more of that.
The text was updated successfully, but these errors were encountered: