Skip to content

Unexpected IndexError in typing.List[''] #116325

@jureslak

Description

@jureslak

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

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixes3.13bugs and security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions