Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved detection of asymmetric descriptors to handle the case where… #9345

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6566,9 +6566,31 @@ export function createTypeEvaluator(
if (!getterSymbolResult || !setterSymbolResult) {
isAsymmetric = false;
} else {
const getterType = getEffectiveTypeOfSymbol(getterSymbolResult.symbol);
let getterType = getEffectiveTypeOfSymbol(getterSymbolResult.symbol);
const setterType = getEffectiveTypeOfSymbol(setterSymbolResult.symbol);

// If this is an overload, find the appropriate overload.
if (isOverloaded(getterType)) {
const getOverloads = OverloadedType.getOverloads(getterType).filter((overload) => {
if (overload.shared.parameters.length < 2) {
return false;
}
const param1Type = FunctionType.getParamType(overload, 1);
return !isNoneInstance(param1Type);
});

if (getOverloads.length === 1) {
getterType = getOverloads[0];
} else {
isAsymmetric = true;
}
}

// If this is an overload, find the appropriate overload.
if (isOverloaded(setterType)) {
isAsymmetric = true;
}

// If either the setter or getter is an overload (or some other non-function type),
// conservatively assume that it's not asymmetric.
if (isFunction(getterType) && isFunction(setterType)) {
Expand Down
74 changes: 45 additions & 29 deletions packages/pyright-internal/src/tests/samples/descriptor1.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
# This sample tests the detection and handling of asymmetric descriptors
# and properties. Type narrowing should be disabled in these cases.

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


class A:
@property
def prop1(self) -> int | None:
...
def prop1(self) -> int | None: ...

@prop1.setter
def prop1(self, val: int | None) -> None:
...
def prop1(self, val: int | None) -> None: ...

@property
def prop2(self) -> int | None:
...
def prop2(self) -> int | None: ...

@prop2.setter
def prop2(self, val: int) -> None:
...
def prop2(self, val: int) -> None: ...

@prop2.deleter
def prop2(self) -> None:
...
def prop2(self) -> None: ...

@property
def prop3(self) -> int:
...
def prop3(self) -> int: ...

@prop3.setter
def prop3(self, val: int | None) -> None:
...
def prop3(self, val: int | None) -> None: ...

@prop3.deleter
def prop3(self) -> None:
...
def prop3(self) -> None: ...


def func1(obj: A) -> Literal[3]:
Expand Down Expand Up @@ -72,33 +64,49 @@ def func3(obj: A) -> Literal[3]:


class Descriptor1:
def __get__(self, instance: Any, owner: Any) -> int | None:
...
def __get__(self, instance: Any, owner: Any) -> int | None: ...

def __set__(self, owner: Any, value: int | None) -> None:
...
def __set__(self, owner: Any, value: int | None) -> None: ...


class Descriptor2:
def __get__(self, instance: Any, owner: Any) -> int | None:
...
def __get__(self, instance: Any, owner: Any) -> int | None: ...

def __set__(self, owner: Any, value: int) -> None:
...
def __set__(self, owner: Any, value: int) -> None: ...


class Descriptor3:
def __get__(self, instance: Any, owner: Any) -> int:
...
def __get__(self, instance: Any, owner: Any) -> int: ...

def __set__(self, owner: Any, value: int | None) -> None:
...
def __set__(self, owner: Any, value: int | None) -> None: ...


class Descriptor4:
@overload
def __get__(self, instance: None, owner: Any) -> int: ...
@overload
def __get__(self, instance: Any, owner: Any) -> str: ...
def __get__(self, instance: Any, owner: Any) -> int | str: ...

def __set__(self, owner: Any, value: int | None) -> None: ...


class Descriptor5:
def __get__(self, instance: Any, owner: Any) -> int: ...

@overload
def __set__(self, owner: bytes, value: int | None) -> None: ...
@overload
def __set__(self, owner: "B", value: int | None) -> None: ...
def __set__(self, owner: Any, value: int | None) -> None: ...


class B:
desc1: Descriptor1
desc2: Descriptor2
desc3: Descriptor3
desc4: Descriptor4
desc5: Descriptor5


def func4(obj: B) -> Literal[3]:
Expand Down Expand Up @@ -132,3 +140,11 @@ def func6(obj: B) -> Literal[3]:
# This should generate an error because prop2 isn't
# narrowed in this case.
return obj.desc3


def func7(obj: B):
obj.desc4 = 3
reveal_type(obj.desc4, expected_text="str")

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