Possible to enforce type hints?

Question:

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/.

Asked By: carl.hiass

||

Answers:

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 (4.x).

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).
Answered By: Hans Musgrave

One option to take advantage of type hints is the type_enforced module. It runs in pure python on the fly with no external compiler needed. 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. Update: In v1.0.0 Additional support for the typing package, nested type structures and even Optionals was added.

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 TypeError(f"({self.__fn__.__qualname__}): {message}")
TypeError: (my_fn): Type mismatch for typed function (my_fn) with `a`. Expected one of the following `[<class 'int'>]` but got `<class 'str'>` instead.
Answered By: conmak

Short answer is that there is no plan for standard support, as stated in the other answers. One alternative which I do not see mentioned here is pydantic.

The BaseModel from pydantic is essentially a python data class that performs type validation in its constructor (and also has handy methods to validate/unmarshal from json buffers). One definitely noteworthy feature for your specific question…. if you pass a string that can be parsed to the correct type (IE '1' -> int | '1.5' -> float), pydantic will only throw an error if the value cannot be coerced into the correct type, and otherwise perform the conversion for you (this can be subverted using strict types or by setting strict=True and using BaseModel.model_validate).

It’s not like type_enforced where it will just use your annotations/hints (by wrapping your function in another function that checks them), you’ll still end up using the BaseModel constructor/validators to enforce your types IE:

from pydantic import BaseModel,ValidationError
from typing import Union
from json import dumps as to_json

class Foo(BaseModel):
    a: int
    b: Union[int,str] = 2
    c: int = 3

def my_fn(foo: Foo) -> None:
    pass

good: Foo = Foo(a=1,b=2,c=3)

also_good: Foo = Foo(a='1',b=2,c='3')

good_dict: dict = {"a":1,"b":2,"c":3}
bad_dict_strict: dict = {"a":'1',"b":2,"c":'3'}

good_dict_json: str = to_json(good_dict)

from_good_dict: Foo = Foo(**good_dict)
from_good_dict_json: Foo = Foo.model_validate_json(good_dict_json)

try:
    bad: Foo = Foo(a='b',b='a',c=1)
except ValidationError as ve:
    print(ve)
try:
    bad_strict: Foo = Foo.model_validate(bad_dict_strict,strict=True)
except ValidationError as ve:
    print(ve)

my_fn(good)
my_fn(also_good)
my_fn(from_good_dict)
my_fn(from_good_dict_json)
Answered By: ThisGuyCantEven
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.