Skip to content

Consider __getattr__ in structural subtyping #4077

Description

@yangdanny97

Describe the Bug

Right now we raise an error if an attribute is available only through __getattr__. I also don't like that the error message says P.x "or" C.x when we know exactly which one it is.

I think this is a TODO:

// NOTE(grievejia): `__getattr__` does not participate in structural subtyping

from typing import *

class P(Protocol):
    x: int

class C:
    def __getattr__(self, attr) -> int:
        return 1

p: P = C()

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRAYyio4cOgAUAFLOoNcY3FACUiEXV118iTugYjxk6XQDC29HrqYYYOgH1nbGA1QMGlV-LgwUGAANHRePhp0ALQAfEYMNnZ2lB4ArpS2AIym6MSGsnQAvFbyGiDBIGQpYFCkhCo0UBQAxHKk1bV0aFh4%2BHRq6JBs6V4QuOiEIq0AyjAwdAAW3sRwiAD0a1WOtYS8bGsw6GuYqnBrA0MjDGNHdGC8YQBuqNCo2LD945eUo%2BN0uMRruM4JNcqQGAtxlFHjBKHAbkU6EIQABmQiZABMyJEAG1YdQ4QBdESpdBcHh8GCYKKYCApMTXGGIgDkEDY6F4MGZIg5DCiKQAjqk6VSogBrGCkKKoMRieAyYrMgDuqAy3PQIAAvhUZYyYAAxaAwCjdHAEEjkTVAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions