Skip to content

Argument 1 to type_check_only has incompatible type type[S[T, *Ts]]; expected type[S[T, *Ts]] #19093

Closed
@jorenham

Description

@jorenham
Contributor

Consider this .pyi code snippet:

from typing import Any, Generic, Self, TypeVar, TypeVarTuple, type_check_only

T = TypeVar("T")
Ts = TypeVarTuple("Ts") 

@type_check_only
class S(tuple[*Ts], Generic[T, *Ts]):
    def f(self, x: T, /) -> T: ...

When ran through mypy (checked with 1.15.0 and the master branch), the following error is reported

Argument 1 to "type_check_only" has incompatible type "type[S[T, *Ts]]"; expected "type[S[T, *Ts]]"  [arg-type]

mypy playground repro

Without the additional T type parameter, the false positive error disappears. It also disappears if I remove the tuple baseclass.
And as far as I could tell, it doesn't seem to be associated with a particular config option or cli flag.

Activity

changed the title [-]Argument 1 to "type_check_only" has incompatible type "type[S[T, *Ts]]"; expected "type[S[T, *Ts]]"[/-] [+]Argument 1 to `type_check_only` has incompatible type `type[S[T, *Ts]]`; expected `type[S[T, *Ts]]`[/+] on May 16, 2025
sterliakov

sterliakov commented on May 17, 2025

@sterliakov
Collaborator

Amazing. mypy does not support type_check_only at all, but this bug is rather surprising - we must be doing something deeply wrong.

Here's a pdb session following quick debugging:

> /home/stas/Documents/Work/mypy/mypy/messages.py(787)incompatible_argument()
-> breakpoint()
(Pdb) arg_type
def [T, Ts] (typing.Iterable[builtins.object] =) -> tuple[Unpack[Ts`2], fallback=a.S[T`1, Unpack[Ts`2]]]
(Pdb) expected_type
def [T, Ts] (typing.Iterable[builtins.object] =) -> tuple[Unpack[Ts`2], fallback=a.S[T`1, Unpack[Ts`2]]]
(Pdb) arg_type==expected_type
True
(Pdb) up
> /home/stas/Documents/Work/mypy/mypy/checkexpr.py(2650)check_arg()
-> code = self.msg.incompatible_argument(
(Pdb) caller_type
def [T, Ts] (typing.Iterable[builtins.object] =) -> tuple[Unpack[Ts`2], fallback=a.S[T`1, Unpack[Ts`2]]]
(Pdb) callee_type
def [T, Ts] (typing.Iterable[builtins.object] =) -> tuple[Unpack[Ts`2], fallback=a.S[T`1, Unpack[Ts`2]]]
(Pdb) caller_type==callee_type
True
(Pdb) is_subtype(caller_type, callee_type, options=self.chk.options)
False
(Pdb) is_subtype(caller_type, callee_type)
False

Hm, so the type is not a subtype of itself. I suspect fixing this is_subtype behaviour may incidentally close a couple more typevartuple issues.

added a commit that references this issue on May 29, 2025
added a commit that references this issue on Jun 18, 2025
4cda52d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-646PEP 646 (TypeVarTuple, Unpack)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jorenham@sterliakov

      Issue actions

        Argument 1 to `type_check_only` has incompatible type `type[S[T, *Ts]]`; expected `type[S[T, *Ts]]` · Issue #19093 · python/mypy