Skip to content

Commit

Permalink
Fixed bug that results in the incorrect detection of an asymmetric de…
Browse files Browse the repository at this point in the history
…scriptor if the descriptor class is generic. This addresses #9450.
  • Loading branch information
erictraut committed Nov 13, 2024
1 parent 838bf7b commit 07f25ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6610,8 +6610,8 @@ export function createTypeEvaluator(
if (!getterSymbolResult || !setterSymbolResult) {
isAsymmetric = false;
} else {
let getterType = getEffectiveTypeOfSymbol(getterSymbolResult.symbol);
const setterType = getEffectiveTypeOfSymbol(setterSymbolResult.symbol);
let getterType = getTypeOfMember(getterSymbolResult);
const setterType = getTypeOfMember(setterSymbolResult);

// If this is an overload, find the appropriate overload.
if (isOverloaded(getterType)) {
Expand Down
17 changes: 16 additions & 1 deletion packages/pyright-internal/src/tests/samples/descriptor1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the detection and handling of asymmetric descriptors
# and properties. Type narrowing should be disabled in these cases.

from typing import Any, Hashable, Iterable, Literal, overload
from typing import Any, Hashable, Iterable, Literal, Self, overload


class A:
Expand Down Expand Up @@ -101,12 +101,24 @@ def __set__(self, owner: "B", value: int | None) -> None: ...
def __set__(self, owner: Any, value: int | None) -> None: ...


class Descriptor6[GT, ST]:
@overload
def __get__(self, instance: None, owner: Any) -> Self: ...

@overload
def __get__(self, instance: Any, owner: Any) -> GT: ...
def __get__(self, instance: Any, owner: Any) -> Any: ...

def __set__(self, instance: Any, value: ST): ...


class B:
desc1: Descriptor1
desc2: Descriptor2
desc3: Descriptor3
desc4: Descriptor4
desc5: Descriptor5
desc6: Descriptor6[int | None, int | None]


def func4(obj: B) -> Literal[3]:
Expand Down Expand Up @@ -148,3 +160,6 @@ def func7(obj: B):

obj.desc5 = 3
reveal_type(obj.desc5, expected_text="int")

obj.desc6 = 1
reveal_type(obj.desc6, expected_text="Literal[1]")

0 comments on commit 07f25ad

Please sign in to comment.