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

Dataclass field that contains a descriptor object isn't handled correctly #3245

Closed
erictraut opened this issue Mar 23, 2022 · 2 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@erictraut
Copy link
Collaborator

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 wrong
@erictraut erictraut added the bug Something isn't working label Mar 23, 2022
@erictraut
Copy link
Collaborator Author

This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Mar 27, 2022
@erictraut
Copy link
Collaborator Author

This is addressed in pyright 1.1.234, which I just published. It will also be included in the next pylance release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant