Collection types should validate given type arg count like corresponding types from ``typing`` module

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

Hm. I recall working on that code and specifically deciding not to check the parameter counts, both for speed and for simplicity of implementation (no need to specify the generic signature for each type). Checking this will already be done by the static type checkers.

If you don’t use a static type checker but something that works at runtime (maybe Pydantic) it should be that something’s responsibility.

I could also imagine a linter checking this.

9 Likes

I think it’s a good thing that the type arguments in collections.abc aren’t validated at runtime. The validation in the typing module gets in our way every time we’d like to make a type parameter optional or add another type parameter. Since otherwise typing_extensions will need to ship a backport in order for users to be able to take advantage of the new type parameter signature.

Recent examples include typing.Generator and typing.(Async)ContextManager which have had their type parameters changed as soon as PEP-696 was implemented in mypy and pyright.

The typing ecosystem generally still evolves a little faster than the language itself, which is also the reason why typing_extensions exists for backports of newer typing features, so the fewer runtime validation the typing module performs the better.

I agree that it’s the responsibility of the consumer of runtime type information to perform the validation of type expressions. There are also runtime type checkers like beartype that may inject validation code into generic classes[1].


  1. although I haven’t checked whether or not they actually do ↩︎

8 Likes