Possible to enforce type hints in python?

Submitted 4 years, 7 months ago
Ticket #128
Views 390
Language/Framework Python
Priority Medium
Status Closed

Is there any advantage to using the 'type hint' notation in python?

import sys
def parse(arg_line: int) -> str:
    print (arg_line) # passing a string, returning None

if __name__ == '__main__':
    parse(' '.join(sys.argv[1:]))

T

o me it seems like it complicates they syntax without providing any actual benefit (outside of perhaps within a development environment). Based on this:

  • Are there any plans for python to contain type constraints within the language itself?
  • What is the advantage of having a "type hint" ? Couldn't I just as easily throw that into the docstring or something?

I also don't see this much in the python codebase itself as far as I can tell -- most types are enforced manually, for example: argparse.py and any other files I've glanced at in https://github.com/python/cpython/blob/3.7/Lib/.

Submitted on Sep 10, 20
add a comment

1 Answer

Verified

Almost certainly not, and definitely not before the next major version.

Type hints can be used by modules like 

 functools.singledispatch 
 

or external libraries like 

multipledispatch 

to add additional type-related features (in this case, dispatching function calls based on name and type, not just name).

Submitted 4 years, 6 months ago


Latest Blogs