[New-bugs-announce] [issue44578] dict with more than two type parameters doesn't raise a TypeError

Michael report at bugs.python.org
Wed Jul 7 07:37:54 EDT 2021


New submission from Michael <login at michaelthe.com>:

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]'}

----------
messages: 397073
nosy: mthe
priority: normal
severity: normal
status: open
title: dict with more than two type parameters doesn't raise a TypeError
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44578>
_______________________________________


More information about the New-bugs-announce mailing list