Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I believe that some recent Python version has added (optional) type annotation support. I haven't ever used it though.


> I believe that some recent Python version has added (optional) type annotation support.

Annotation support was added in 3.0, which isn't really a recent version of Python, having been released about 11.5 years ago.

Also, mypy, while it can use Python 3.x annotations, also supports static checking of Python 2.x code using type comments.


> Annotation support was added in 3.0

However, support for type annotations, specifically, wasn’t added until Python 3.5 (via PEP 484).


What PEP 484 was standardize the use of annotations as type annotations and provide ancillary out-of-the-box support for type hinting via annotations, particularly the typing module.

Mypy was actually using python 3.x annotation for type annotations before PEP 484 standardized them brought the stdlib typing module, but with PEP 484 there was a common, language-defined standard baseline for mypy and other efforts.


It's super-easy to use! More flexible than Java, but (so far as I can tell) just as powerful in terms of catching errors.


You can even benefit from it without bothering with the type annotations (though they make it more effective).

https://google.github.io/pytype/


Maybe your IDE experience is different than mine, but a typed function will gladly accepted an untyped var without complaining, and during runtime could care less.


> but a typed function will gladly accepted an untyped var without complaining

this can be made prohibited by MyPy's --strict flag [1], and enforced with a mypy git pre-commit hook locally, and with a MyPy CI job.

[1] https://mypy.readthedocs.io/en/stable/command_line.html#cmdo...


That's a mypy setting


There is type inference in all of the Python type checkers, it's not necessarily an "untyped" var because it lacks an annotation.

Running a type checker in Python is a way to avoid runtime type errors, just like in compiled languages, but without a performance benefit.


Type annotations aren’t equivalent to this check, though, because they’re not enforced at runtime.


Why should type annotation be enforced at run time? In statically typed languages there is no type checking at run time, your type system already proved the type of variable.


Because Python’s type annotations don’t prove anything. That’s why, for example, they can’t be used to increase the performance of the interpreter.


> That’s why, for example, they can’t be used to increase the performance of the interpreter.

They can (as in - there's API for that, the rest is up to a community effort) improve performance of a final program, if type-annotated code is passed through Cython with ``annotation_typing=True`` flag:

http://docs.cython.org/en/latest/src/tutorial/pure.html#stat...

https://github.com/cython/cython/issues/1672#issuecomment-29...


It’s not enforced at all, and only serves to provide development feedback.


The annotations are a language syntax feature than the runtime doesn't enforce. There are a number of separate static typecheckers, such as mypy, that do allow AOT static verification (which is, after all, all languages like Haskell have; runtime enforcement of types that have been statically verified in advance isn't super common or necessary.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: