Skip to content

Incorrect "Type application has too few types" #13437

@Dr-Irv

Description

@Dr-Irv

Bug Report

A Generic class that is a subclass of a class with a __new__() declared where there is a TypeVar as a parameter of __new__() reports that "Type application has too few types"

To Reproduce

from __future__ import annotations
from typing import Generic, Sequence, TypeVar, Union

T1 = TypeVar("T1", Sequence, list)
T2 = TypeVar("T2")


class Base:
    def __new__(cls, data: T1 | str) -> Base:
        ...

    def meth(self, data: T1 | str) -> int:
        ...


class GenBase(Base, Generic[T2]):
    ...


def fun() -> GenBase[int]:
    return GenBase[int]("ac")

Actual Behavior
mypy 0.971 reports:

gentypevar.py:21: error: Returning Any from function declared to return "GenBase[int]"
gentypevar.py:21: error: Type application has too few types (2 expected)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8
  • Operating system and version: Windows 10

Note that if T1 is changed to a Union, then no error is reported:

from __future__ import annotations
from typing import Generic, Sequence, TypeVar, Union

U1 = Union[Sequence, list, str]
T2 = TypeVar("T2")

class Base2:
    def __new__(cls, data: U1 | str) -> Base2:
        ...

    def meth(self, data: U1 | str) -> int:
        ...


class GenBase2(Base2, Generic[T2]):
    ...


def fun2() -> GenBase2[int]:
    return GenBase2[int]("ac")

This came up with the pandas-stubs project. pandas-dev/pandas-stubs#197

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions