Skip to content

Pyrefly doesn't support setting an instance attribute to a descriptor #1803

@rchen152

Description

@rchen152

Describe the Bug

If you set an instance attribute to a descriptor (rather than setting it at the class level), pyrefly should just treat it as as a regular instance attribute with no descriptor behavior. This works in pyright and (mostly) in mypy (mypy doesn't error but doesn't infer precise types). Pyrefly gets confused:

from typing import assert_type

class MyDescriptor:
    def __get__(self, instance, owner=None):
        return 42

class A:
    def __init__(self):
        self.a = MyDescriptor()  # error: Attribute `a` of class `A` is a read-only descriptor

assert_type(A().a, MyDescriptor) # error: assert_type(Literal[42], MyDescriptor) failed

I saw this in real-world code using the torch.device class (which has a __get__ method but no __set__): https://github.com/search?q=repo%3Apytorch%2Fignite+%22self._device+%3D%22&type=code.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN2o4cGHwD6TYjAA66GQGMoguHQCypACLw5lCMQa9EMusbqYYYOqNFsYDKwAphUMABpO6OAwxyYb3AHd0EQBeADlcIIBKQ3QTOLpKWwBXSliAFgAmGXlFIToAQRi4swsrVgg7UUcYZ2ijeOMnMEJUOmDVDS0dPV57SOz0JRE7CRh7fL6WtzVNOG1dfUpIkBcQJIZoOBJyRBAAYjoAVQ2oCtI6MCT0OQ2IuAGSi94aVDt0JJpsEXt8RHcGSJ0AC0AD46J5KEUTIkGClYmApCBQh8vpC6MB8ABfREyFYgMiJMBQUiEfQ0KAUA4ABVIhOJ4IwOAIdDkEUgbBSrwgEUIMgOAGUYDA6AALBgMYhwRAAemlBPMxMIvDY0pg6GlmFwcjg0tZ6HZnNu6qelAEADdUNBUNhYCy2RAOZQuRE6Lg9NyPLz0GQGCKIkCzSI4B62nREQBmQgARiyIFxmNWqBuEEDADFoDAKGgsHgiGQQJigA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions