Skip to content

PEP-646 generic with self annotated function gives incorrect result #21878

Description

@MeGaGiGaGon

Bug Report

When a class is generic over a PEP-646/TypeVarTuple/varadic generic and uses that outer generic in a self annotated function, the result of calling that function is incorrect.

To Reproduce

https://mypy-play.net/?gist=e1eb19d739dd5cdc55e99e1ead4fc55c

from typing import reveal_type

class A[*TS]:
    def __init__(self, *state: *TS):
        self.state: tuple[*TS] = state

    def add(self: A[*TS, int, int]) -> A[*TS, int]:
        return A(*self.state[:-2], self.state[-2] + self.state[-1])

# Expected type: "A[1, int]" or "A[1, 5]" or similar
# Actual: Revealed type is "__main__.A[Literal[1]?, Literal[2]?, Literal[3]?, int]"
reveal_type(A(1, 2, 3).add())

Expected Behavior

Mypy should either give the correct type for A(1, 2, 3).add(), or give an error somewhere.

Actual Behavior

Mypy silently gives the wrong type for A(1, 2, 3).add()

Your Environment

  • Mypy version used: Playground 2.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-self-typesTypes for self

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions