Closed
Description
Bug report
Bug description:
Calling typing.List['']
produces an unexpected IndexError
.
>>> import typing
>>> typing.List['']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.11/typing.py", line 362, in inner
return cached(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 1575, in __getitem__
params = tuple(_type_check(p, msg) for p in params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 1575, in <genexpr>
params = tuple(_type_check(p, msg) for p in params)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 186, in _type_check
arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 164, in _type_convert
return ForwardRef(arg, module=module, is_class=allow_special_forms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 859, in __init__
if arg[0] == '*':
~~~^^^
IndexError: string index out of range
The construct is invalid, but I'd expect a more friendly error, something similar to
>>> typing.Dict[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.11/typing.py", line 365, in inner
return func(*args, **kwds)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 1576, in __getitem__
_check_generic(self, params, self._nparams)
File "/usr/lib/python3.11/typing.py", line 293, in _check_generic
raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
TypeError: Too few arguments for typing.Dict; actual 1, expected 2
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
- gh-116325: Raise
SyntaxError
on ForwardRef with empty string arg #116341 - [3.12] gh-116325: Raise
SyntaxError
rather thanIndexError
on ForwardRef with empty string arg (GH-116341) #116347 - [3.11] gh-116325: Raise
SyntaxError
rather thanIndexError
on ForwardRef with empty string arg (GH-116341) #116348