pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Method invocation should preserve generic annotation on `Self`

Open grievejia opened this issue 10 months ago • 1 comments

MInimal repro:

class A:
    def m[S: A](self: S) -> S:  # The bound of `A` on `S` is optional
        return self

def g[T: A](a: T) -> T:
    return a.m()

Expected: no error Actual: Returned type A is not assignable to declared return type TypeVar[T] [bad-return]

When calculating the type of a.m(), we seem to be pinning the type of a to A immediately -- that's incorrect. The type should be T (where T may be either A or one of A's subclass).

grievejia avatar May 01 '25 17:05 grievejia

https://github.com/facebook/pyrefly/issues/119

This also looks related

yangdanny97 avatar May 01 '25 18:05 yangdanny97