Drew
Drew

Reputation: 537

Python: typing: Typehints on function not working

I'm having trouble getting type hints to work in the following example. Can anyone see what is wrong.

The 2nd and 3rd example works fine. The first one fails when i run it

#python 3.6
from typing import List, Tuple

# line 5 below where error occurs
def func(x: List[int, str]) -> int:
    return 1

a = func([1, "a"])

# OK
def func2(x: Tuple[int, str]) -> int:
    return 1

b = func2((1, "a"))

# OK
def func3(x: List[Union[str, int]]) -> int:
    return 1

c = func3((1, "a"))

Here is the stack trace. I can't work out what is wrong.

Traceback (most recent call last):
  File "C:scratch/scratch2.py", line 5, in <module>
    def func(x: List[int, str]) -> int:
  File "C:\Miniconda3\lib\typing.py", line 682, in inner
    return func(*args, **kwds)
  File "C:\Miniconda3\lib\typing.py", line 1152, in __getitem__
    _check_generic(self, params)
  File "C:\Miniconda3\typing.py", line 662, in _check_generic
    ("many" if alen > elen else "few", repr(cls), alen, elen))
TypeError: Too many parameters for typing.List; actual 2, expected 1

Upvotes: 0

Views: 332

Answers (1)

GlaceCelery
GlaceCelery

Reputation: 1033

The typing library will only accept one argument to the List type

Upvotes: 1

Related Questions