Skip to content

Commit

Permalink
Fixed a bug that leads to incorrect type evaluation when `__getitem__…
Browse files Browse the repository at this point in the history
…` is set to a callable object. This addresses #9470. (#9519)
  • Loading branch information
erictraut authored Nov 29, 2024
1 parent 17cee31 commit 01e33d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,7 @@ export function createTypeEvaluator(
return getBoundMagicMethod(
boundMethodResult.type,
'__call__',
selfType ?? ClassType.cloneAsInstance(classType),
/* selfType */ undefined,
diag,
recursionCount
);
Expand Down
24 changes: 16 additions & 8 deletions packages/pyright-internal/src/tests/samples/index1.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ class ClassA(metaclass=MyMetaclass):


class ClassB:
def __setitem__(self, index: int, value: "ClassB"):
...
def __setitem__(self, index: int, value: "ClassB"): ...


class ClassC:
def __setitem__(self, index: int, value: "ClassC"):
...
def __setitem__(self, index: int, value: "ClassC"): ...


B_or_C = TypeVar("B_or_C", ClassB, ClassC)
Expand All @@ -75,8 +73,7 @@ def func1(container: B_or_C):


class ClassD(Generic[TD]):
def __setitem__(self, index: int, value: TD):
...
def __setitem__(self, index: int, value: TD): ...


def func2(container: ClassD[TD], value: TD):
Expand All @@ -98,8 +95,7 @@ def __getattr__(self, s: str) -> Any:


class ClassF(Generic[T]):
def __getitem__(self, args: int) -> Self:
...
def __getitem__(self, args: int) -> Self: ...

def get(self, index: int) -> Self:
reveal_type(self[index], expected_text="Self@ClassF[T@ClassF]")
Expand All @@ -113,3 +109,15 @@ class ClassG:
def func3(g: ClassG):
reveal_type(g.x, expected_text="Unbound")
reveal_type(g.x[0], expected_text="Unknown")


class ClassH:
def __call__(self, *args, **kwargs) -> Self:
return self


class ClassI:
__getitem__ = ClassH()


reveal_type(ClassI()[0], expected_text="ClassH")

0 comments on commit 01e33d7

Please sign in to comment.