mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Spurious "Invalid self argument ... to attribute function" error when currying TypeVarTuple

Open vbraun opened this issue 1 year ago • 0 comments

Bug Report

I'm getting an "Invalid self argument ... to attribute function" error when inheriting and simultaneously augmenting the TypeVarTuple. The error happens only when calling attribute functions, and not when accessing normal attributes.

To Reproduce

from typing import Iterator, TypeGuard, TypeVar, Iterable, Type, Any, Callable, TypeVarTuple, Generic


T = TypeVarTuple('T')


class Base(Generic[*T]):

    attr: tuple[*T]
    
    @property
    def prop(self) -> tuple[*T]:
        return self.attr

    def meth(self) -> tuple[*T]:
        return self.attr


S = TypeVarTuple('S')


class Derived(Base[str, *S]):
    
    def frob(self) -> None:
        reveal_type(self.attr)     # OK: "tuple[builtins.str, Unpack[S`1]]"
        reveal_type(self.prop)     # Error
        reveal_type(self.meth())   # Error


d: Derived[int]
reveal_type(d.attr)         # OK: tuple[str, int]
reveal_type(d.prop)         # OK: tuple[str, int]
reveal_type(d.meth())       # OK: tuple[str, int]

Gist URL: https://gist.github.com/mypy-play/531a3a730ac2b172120e2fa1a2c307b8 Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=531a3a730ac2b172120e2fa1a2c307b8

Expected Behavior

In the derived class, accessing base attributes (self.attr), base properties (self.prop), and base methods (self.meth()) should work.

Actual Behavior

Accessing the base properties (self.prop), and base methods (self.meth()) causes a spurious

error: Invalid self argument "Derived[*S]" to attribute function "prop" with type "Callable[[Base[*T]], tuple[*T]]"  [misc]

At the same time, reveal_type reports the expected type despite the error.

Full mypy output:

main.py:23: note: Revealed type is "tuple[builtins.bytes, Unpack[S`1]]"
main.py:24: error: Invalid self argument "Derived[*S]" to attribute function "prop" with type "Callable[[Base[*T]], tuple[*T]]"  [misc]
main.py:24: note: Revealed type is "tuple[builtins.bytes, Unpack[S`1]]"
main.py:25: error: Invalid self argument "Derived[*S]" to attribute function "meth" with type "Callable[[Base[*T]], tuple[*T]]"  [misc]
main.py:25: note: Revealed type is "tuple[builtins.bytes, Unpack[S`1]]"
main.py:29: note: Revealed type is "tuple[builtins.bytes, builtins.str, builtins.float]"
main.py:30: note: Revealed type is "tuple[builtins.bytes, builtins.str, builtins.float]"
main.py:31: note: Revealed type is "tuple[builtins.bytes, builtins.str, builtins.float]"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0, also earlier versions
  • Mypy command-line flags: none (also happens with --strict)
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Tested on 3.11 and 3.12

vbraun avatar Apr 27 '24 16:04 vbraun