mypy icon indicating copy to clipboard operation
mypy copied to clipboard

GenericAlias not Callable, but it should be

Open mjog opened this issue 3 years ago • 2 comments

Bug Report

The docs for GenericAlias explicitly state these objects are callable, giving the following example:

>>> t = list[str]
>>> t([1, 2, 3])
[1, 2, 3]

However the typeshed definition is not marked as such, and hence causes errors when type checking such cases.

In particular mypy erroneous reports: "GenericAlias" not callable when checking calls such as the above.

I initially reported this to typeshed, but they said it was too complex to fix there, so type checkers like mypy will need to special-case this.

Your Environment

  • Mypy version used: 0.971
  • Python version used: 3.10
  • Operating system and version: Ubuntu 22.04

mjog avatar Sep 09 '22 01:09 mjog

Actually, I can't reproduce: https://mypy-play.net/?mypy=latest&python=3.10&gist=aa1db479d5f97e6cd67fede0b7c543e1

AlexWaygood avatar Sep 09 '22 07:09 AlexWaygood

You can reproduce it with something like this:

from types import GenericAlias
t: GenericAlias = list[int]
t([1, 2, 3])

But I'm not sure there's an actionable problem there.GenericAlias is essentially a runtime implementation artifact; mypy doesn't and needn't know that list[int] is a GenericAlias.

JelleZijlstra avatar Sep 09 '22 08:09 JelleZijlstra

I think mypy is doing the right thing here. As Jelle explains, GenericAlias is an internal implementation detail. It is not meant to participate in the static type system. Recommend closing.

erictraut avatar Aug 13 '23 19:08 erictraut