Possible to enforce type hints?

Are there any plans for python to contain type constraints within the language itself?

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

What is the advantage of having a “type hint” ? Couldn’t I just as easily throw that into the docstring or something?

Off the top of my head, consider the following:

  • Type hints can be verified with tooling like mypy.
  • Type hints can be used by IDEs and other tooling to give hints and tips. E.g., when you’re calling a function and you’ve just written foo(, the IDE can pick up on the type hints and display a box nearby that shows foo(x: int, y: List[int]). The advantage to you as a developer is that you have exactly the information you need exposed to you and don’t have to munge an entire docstring.
  • 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).

Leave a Comment