This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict/list with more than two type parameters doesn't raise a TypeError
Type: behavior Stage: resolved
Components: Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, mthe
Priority: normal Keywords:

Created on 2021-07-07 11:37 by mthe, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg397073 - (view) Author: Michael (mthe) Date: 2021-07-07 11:37
dict with three type parameters is legal (e.g., dict[str, str, str]), where I expected a TypeError. When using typing.Dict, it does raise a TypeError.

Example in python 3.9:

>>> from typing import Dict
>>> Dict[str, str, str]  # Raises a TypeError, as expected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 275, in inner
    return func(*args, **kwds)
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 828, in __getitem__
    _check_generic(self, params, self._nparams)
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 212, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too many parameters for typing.Dict; actual 3, expected 2
>>> dict[str, str, str]  # No TypeError here?
dict[str, str, str]

This also works in 3.7 and 3.8:

Python 3.8.11 (default, Jul  6 2021, 12:13:09) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> dict[str, str, str]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> def foo(bar: dict[str, str, str]): pass
... 
>>> foo.__annotations__
{'bar': 'dict[str, str, str]'}
msg397080 - (view) Author: Michael (mthe) Date: 2021-07-07 13:26
Same for list btw

Python 3.9.6 (default, Jul  7 2021, 11:41:04) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> list[str, str, str]
list[str, str, str]
>>> from typing import List
>>> List[str, str, str]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 275, in inner
    return func(*args, **kwds)
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 828, in __getitem__
    _check_generic(self, params, self._nparams)
  File "/Users/michaelthe/.pyenv/versions/3.9.6/lib/python3.9/typing.py", line 212, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too many parameters for typing.List; actual 3, expected 1
>>>
msg397088 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-07-07 14:24
Sorry, I don't think this will be fixed as it is intentional. types.GenericAlias (the type of list[int] or dict[str, str]) does almost 0 checking for two reasons IIUC:

- Types with wrong number of parameters will usually be caught by your IDE/type checker.
- Checking would add complexity and slow down runtime performance. The current implementation of GenericAlias in C is intentionally simple.

We can add a note in the docs noting that too many type parameters won't raise a TypeError. It would probably be here https://docs.python.org/3/library/stdtypes.html#generic-alias-type

BTW, I adjusted the issue's affected versions because this was introduced only in 3.9.
msg397092 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-07-07 14:33
Yes this is intentional. Use a static checker to find this type of bug.
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88744
2021-07-07 14:33:32gvanrossumsetstatus: open -> closed
resolution: not a bug
messages: + msg397092

stage: resolved
2021-07-07 14:24:45kjsetnosy: + kj, gvanrossum

messages: + msg397088
versions: + Python 3.10, Python 3.11, - Python 3.7, Python 3.8
2021-07-07 13:26:46mthesettitle: dict with more than two type parameters doesn't raise a TypeError -> dict/list with more than two type parameters doesn't raise a TypeError
2021-07-07 13:26:23mthesetmessages: + msg397080
2021-07-07 11:37:54mthecreate