Skip to content

Inconsistent type substitution for ParamSpec #138492

@sobolevn

Description

@sobolevn

Bug report

Let's say we have this setup code:

from collections.abc import Callable as abc_Callable
from typing import ParamSpec, TypeVar, Callable
P = ParamSpec('P')
R = TypeVar('R')

This works correctly:

Callable[P, R][0, int]  # correctly raises:
# TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got 0

abc_Callable[P, R][0, int]  # correctly raises:
# TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got 0

We even have a test for this:

def test_type_subst_error(self):
Callable = self.Callable
P = ParamSpec('P')
T = TypeVar('T')
pat = "Expected a list of types, an ellipsis, ParamSpec, or Concatenate."
with self.assertRaisesRegex(TypeError, pat):
Callable[P, T][0, int]

But, this does not raise for some reason:

>>> Callable[P, int][0]
typing.Callable[[0], int]
>>> abc_Callable[P, int][0]
collections.abc.Callable[[0], int]

>>> Callable[P, int][0, int]
typing.Callable[[0, int], int]
>>> abc_Callable[P, int][0, int]
collections.abc.Callable[[0, int], int]

Somehow the presense of R makes the validation correct. But, when R is removed, we skip the ParamSpec validation.

I would like to work on this if we decide that this is a bug indeed. I remember that we want less runtime checks. But, technically this is a regression in one of them?

CC @JelleZijlstra

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions