mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
[ty] Fall back to object
for attribute access on synthesized protocols (#20286)
This commit is contained in:
parent
982a0a2a7c
commit
deb3d3d150
4 changed files with 39 additions and 2 deletions
|
@ -392,8 +392,14 @@ from inspect import getattr_static
|
|||
|
||||
def f_okay(c: Callable[[], None]):
|
||||
if hasattr(c, "__qualname__"):
|
||||
c.__qualname__ # okay
|
||||
reveal_type(c.__qualname__) # revealed: object
|
||||
|
||||
# TODO: should be `property`
|
||||
# (or complain that we don't know that `type(c)` has the attribute at all!)
|
||||
reveal_type(type(c).__qualname__) # revealed: @Todo(Intersection meta-type)
|
||||
|
||||
# `hasattr` only guarantees that an attribute is readable.
|
||||
#
|
||||
# error: [invalid-assignment] "Object of type `Literal["my_callable"]` is not assignable to attribute `__qualname__` on type `(() -> None) & <Protocol with members '__qualname__'>`"
|
||||
c.__qualname__ = "my_callable"
|
||||
|
||||
|
|
|
@ -84,3 +84,17 @@ def _(obj: MaybeWithSpam):
|
|||
# error: [possibly-unbound-attribute]
|
||||
reveal_type(obj.spam) # revealed: int
|
||||
```
|
||||
|
||||
All attribute available on `object` are still available on these synthesized protocols, but
|
||||
attributes that are not present on `object` are not available:
|
||||
|
||||
```py
|
||||
def f(x: object):
|
||||
if hasattr(x, "__qualname__"):
|
||||
reveal_type(x.__repr__) # revealed: bound method object.__repr__() -> str
|
||||
reveal_type(x.__str__) # revealed: bound method object.__str__() -> str
|
||||
reveal_type(x.__dict__) # revealed: dict[str, Any]
|
||||
|
||||
# error: [unresolved-attribute] "Type `<Protocol with members '__qualname__'>` has no attribute `foo`"
|
||||
reveal_type(x.foo) # revealed: Unknown
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue