Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jureslak opened this issue Mar 4, 2024 · 1 comment
Closed

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

jureslak opened this issue Mar 4, 2024 · 1 comment
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes topic-typing type-bug An unexpected behavior, bug, or error

Comments

@jureslak
Copy link

jureslak commented Mar 4, 2024

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

@jureslak jureslak added the type-bug An unexpected behavior, bug, or error label Mar 4, 2024
@Eclips4 Eclips4 added topic-typing 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Mar 4, 2024
@sobolevn sobolevn self-assigned this Mar 4, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 4, 2024
AlexWaygood pushed a commit that referenced this issue Mar 5, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2024
…ardRef with empty string arg (pythonGH-116341)

(cherry picked from commit a29998a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2024
…ardRef with empty string arg (pythonGH-116341)

(cherry picked from commit a29998a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Mar 5, 2024
…wardRef with empty string arg (GH-116341) (#116347)

gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef with empty string arg (GH-116341)
(cherry picked from commit a29998a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Mar 5, 2024
…wardRef with empty string arg (GH-116341) (#116348)

gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef with empty string arg (GH-116341)
(cherry picked from commit a29998a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@AlexWaygood
Copy link
Member

Thanks for the report @jureslak, and thanks for fixing @sobolevn!

adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants