facebook / facebook/pyrefly

Descriptor access collapses intersection types

Open
#3,601 4 comments 0 reactions 1 assignee Assigned to @stroxler View on GitHub
narrowing stale typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

When accessing a descriptor on an instance whose type was narrowed to an intersection type (via `isinstance` or `TypeIs`), the narrowed type appears to be discarded before the type of the `instance` parameter is evaluated. When calling `__get__` directly, the intersection is preserved.

Example:

```python
from typing import Any, Self, overload, reveal_type

class Descriptor:
@overload
def __get__(self, instance: None, owner: type, /) -> Self: ...
@overload
def __get__[T](self, instance: T, owner: type | None = None, /) -> T: ...
def __get__(self, instance: object, owner: type | None = None, /) -> Any: ...

class A:
desc = Descriptor()

class Mixin:
pass

def g(x: A) -> None:
if isinstance(x, Mixin):
reveal_type(x) # revealed type: A & Mixin
reveal_type(x.desc) # revealed type: A
reveal_type(A.desc.__get__(x)) # revealed type: A & Mixin
```

Unclear if this is related, but `reveal_type(type(x))` produces `type` where I would have expected `type[A & Mixin]`.

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BBdKQA0dAMowoYMbgBuMSlFypMYyjAWooAfSbEYAHXTGAxlFRw4dACLxTlCMQa9Exuh7oABeYuWr3T0wYMDodHTYYBnCACjgpGU50OAYMUxhEOgA5XHQYWQB3PMpM-Xy6AHoASjoAWgA%2BCQTMwlbAjx8FJRVMdrpg0PDI6J0AbQAVAF04hLFWFLSMunHC4tLmGDoAH2zczYBeXbyxarrG8Za29E9%2BkLCIqNj46Tnk1PR0zNxsACsYUwYq0U6wM2yOB3BJxqDUEwkuhGMZgsVkEbmuQXsdEOdjgDicLkoMSqiI%2ByOsAFkIPhWGibsRLHASQM6GwYvhMgJoY0cnlaZ4IKEIHB5u90myxJTqegqnybh4NFpdGU2cT0XK6AqYNo9Bs2YRgrjVer5ZotUrdQJ9fZCENHjoVVUQCIQGQNGAoKRCC4aFAKABiOgABVIbo9dDQWDw%2BDoplykDYAFdKKgGBBcgj0AHJJsABYMBjEOCICoVV0hD2EXhsCowdAVTC4UxwCqx9DxpMptN1uhgXh0VByVDQVDYWAxuMQRPJ1O5Oi4ZxduAZsgMHO5WpdYWzw6GEAAZkIAEYAEy74yjRTUShwSbGBPoLg8PgwTC1TAQDQAiAKLF0ADkk7oLwMB-sYQEMLUGgAI4Jh%2BL61AA1jApC1KgpjpCihx-gUqCUOgoHoCAAC%2BzpoamCgAGLQDAFARjgBAkOQRFAA

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.