Closed
Description
openedon Mar 23, 2022
If a @dataclass field is initialized with a descriptor as a default value, pyright should use the value type of the __set__ method for the descriptor when determining the type of the corresponding parameter within the synthesized __init__ method.
Types are also evaluated incorrectly in this case.
Note that mypy also exhibits these same bugs.
from dataclasses import dataclass
from typing import Any, cast
class MyDescriptor:
def __get__(self, __obj: object | None, __owner: Any) -> "int | MyDescriptor":
if __obj is None:
return self
return cast(Any, __obj)._x
def __set__(self, __obj: object | None, __value: int) -> None:
if __obj is not None:
cast(Any, __obj)._x = __value
@dataclass
class Foo:
y: MyDescriptor = MyDescriptor()
f = Foo(3) # Currently a false positive error in pyright (and mypy)
print(f.y) # 3
f.y = 4
print(f.y) # 4
print(Foo.y) # MyDescriptor object
reveal_type(f.y) # int | MyDescriptor, which is wrong
reveal_type(Foo.y) # int | MyDescriptor, which is wrongSign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Metadata
Assignees
Labels
Issue is fixed and will appear in next published versionIssue is fixed and will appear in next published versionSomething isn't workingSomething isn't working