Skip to content

Incorrect handling of bound methods that are overloaded on the type of self #1169

@sharkdp

Description

@sharkdp

Summary

Consider the following example, where our use of CallableSignature::bind_self to eagerly remove the first parameter leads to a wrong display type of the bound method. The call binding goes through a different code path and leads to the correct result, though:

from __future__ import annotations

from typing import reveal_type, overload


class C[T]:
    x: T

    @overload
    def method(self: C[int]) -> int:
        return 0

    @overload
    def method(self: C[str]) -> str:
        return ""

    def method(self) -> int | str:
        raise NotImplementedError


reveal_type(C[str]().method)    # ty: Overload[() -> int, () -> str]
reveal_type(C[str]().method())  # ty: str

https://play.ty.dev/e75a9987-fcee-4bd9-a53f-d7edf9941465

Also related to this, we do not emit an error here. It might be argued that this is fine, though, as long as we're not calling the bound method:

from typing import reveal_type

class C:
    def wrong_annotation(self: int) -> int:
        return 0

reveal_type(C().wrong_annotation)

https://play.ty.dev/671b14e0-fa24-4ef9-a05d-8e2bfcaffe51

Version

Current main (59c8fda)

Metadata

Metadata

Assignees

Labels

attribute accessInstance attributes, class attributes, etc.bugSomething isn't workingcallsIssues relating to call-signature checking and diagnostics

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions