[Python-ideas] Enabling / disabling optional type hinting

Andre Roberge andre.roberge at gmail.com
Sat Mar 23 10:36:20 EDT 2019


Consider the following example [1]:

 Python 3.7.0 (v3.7.0:1bf9cc5093...
 >>> d = {
 ...   "injury": "flesh wound"
 ... }
 >>> d["answer"]: 42
 >>> if "answer" in d:
 ...     print("Don't panic!")
 ... else:
 ...     print("Sorry, I can't help you.")
 ...
 Sorry, I can't help you.

= =
No SyntaxError raised (which would have been the case before version 3.5?)
and yet what could be a very unexpected result occurred.

Of course, the problem is with the line

d["answer"]: 42

which is not an assignment but is an "optional" type hint.

I think it would be very useful to have a way to turn off completely type
hinting and flag any use of code like the above as a SyntaxError.

My preference would be if type hinting would be something that is enabled
per file with a top-level comment pragma, something like

# type: enable

Failing that, having a to-level comment pragma like

# type: disable

might be acceptable as it would not require any change to any existing
file. However, the other option would be more inline with type hinting
being "optional" and something that should not trip beginners who are not
aware of its existence.

André Roberge

[1] This example was inspired by a blog post I read yesterday and which I
cannot find; I apologize to the author of that blog post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190323/25b6e11c/attachment.html>


More information about the Python-ideas mailing list