'Possible to enforce type hints?
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:]))
To me it seems like it complicates the 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/.
Solution 1:[1]
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 showsfoo(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 likemultipledispatch
to add additional type-related features (in this case, dispatching function calls based on name and type, not just name).
Solution 2:[2]
One option to take advantage of type hints is the type_enforced module. Regarding official python support, it still seems unlikely that types hints will be enforced directly in the near future.
Going into type_enforced
, the package allows you to take advantage of type hints. It supports both input and output typing. Only types that are specified are enforced. Multiple possible inputs are also supported so you can specify something like int or float.
Input types are first validated (lazily on the function call) and if valid, the function is processed where the return value is then validated.
There are some limitations such that nested type structures are not supported. For example you can not specify type as a list of integers, but only a list. You would need to validate the items in the list inside of your function.
pip install type_enforced
>>> import type_enforced
>>> @type_enforced.Enforcer
... def my_fn(a: int , b: [int, str] =2, c: int =3) -> None:
... pass
...
>>> my_fn(a=1, b=2, c=3)
>>> my_fn(a=1, b='2', c=3)
>>> my_fn(a='a', b=2, c=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/conmak/development/personal/type_enforced/type_enforced/enforcer.py", line 47, in __call__
return self.__validate_types__(*args, **kwargs)
File "/home/conmak/development/personal/type_enforced/type_enforced/enforcer.py", line 83, in __validate_types__
self.__check_type__(assigned_vars.get(key), value, key)
File "/home/conmak/development/personal/type_enforced/type_enforced/enforcer.py", line 56, in __check_type__
self.__exception__(
File "/home/conmak/development/personal/type_enforced/type_enforced/enforcer.py", line 37, in __exception__
raise Exception(f"({self.__fn__.__qualname__}): {message}")
Exception: (my_fn): Type mismatch for typed function (my_fn) with `a`. Expected one of the following `[<class 'int'>]` but got `<class 'str'>` instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hans Musgrave |
Solution 2 | conmak |