When using the deprecated types typing.Mapping, typing.Dict, etc. the correct count of provided type arguments is validated. This behavior however is not implemented in the abc.collections module nor the built-in types.
I propose to adjust the abc.collections module and the built-in type implementation to provide this kind of runtime validation as well.
Current behavior when using types from typing module:
>>> typing.List[str, str]
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
typing.List[str, str]
~~~~~~~~~~~^^^^^^^^^^
File "/home/uli/cpython/Lib/typing.py", line 423, in inner
return func(*args, **kwds)
File "/home/uli/cpython/Lib/typing.py", line 1563, in __getitem__
raise TypeError(f"Too {'many' if actual_len > self._nparams else 'few'} arguments for {self};"
f" actual {actual_len}, expected {expected}")
TypeError: Too many arguments for typing.List; actual 2, expected 1
>>> typing.Dict[str]
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
typing.Dict[str]
~~~~~~~~~~~^^^^^
File "/home/uli/cpython/Lib/typing.py", line 423, in inner
return func(*args, **kwds)
File "/home/uli/cpython/Lib/typing.py", line 1563, in __getitem__
raise TypeError(f"Too {'many' if actual_len > self._nparams else 'few'} arguments for {self};"
f" actual {actual_len}, expected {expected}")
TypeError: Too few arguments for typing.Dict; actual 1, expected 2
Current behavior of list and abc.collections.Mapping:
>>> list[str, int]
list[str, int]
>>> collections.abc.Mapping[str, int, int]
collections.abc.Mapping[str, int, int]
POC: GitHub - wiomoc/cpython at poc-validate-collection-type-arg-count