mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Do not consider a type T
to satisfy a method member on a protocol unless the method is available on the meta-type of T
(#19187)
This commit is contained in:
parent
b124e182ca
commit
f722bfa9e6
5 changed files with 132 additions and 19 deletions
|
@ -0,0 +1,5 @@
|
|||
from unresolved_module import SomethingUnknown
|
||||
|
||||
class Foo(SomethingUnknown): ...
|
||||
|
||||
tuple(Foo)
|
|
@ -754,3 +754,42 @@ def f(never: Never):
|
|||
for x in never:
|
||||
reveal_type(x) # revealed: Unknown
|
||||
```
|
||||
|
||||
## A class literal is iterable if it inherits from `Any`
|
||||
|
||||
A class literal can be iterated over if it has `Any` or `Unknown` in its MRO, since the
|
||||
`Any`/`Unknown` element in the MRO could materialize to a class with a custom metaclass that defines
|
||||
`__iter__` for all instances of the metaclass:
|
||||
|
||||
```py
|
||||
from unresolved_module import SomethingUnknown # error: [unresolved-import]
|
||||
from typing import Any, Iterable
|
||||
from ty_extensions import static_assert, is_assignable_to, TypeOf, Unknown
|
||||
|
||||
class Foo(SomethingUnknown): ...
|
||||
|
||||
reveal_type(Foo.__mro__) # revealed: tuple[<class 'Foo'>, Unknown, <class 'object'>]
|
||||
|
||||
# TODO: these should pass
|
||||
static_assert(is_assignable_to(TypeOf[Foo], Iterable[Unknown])) # error: [static-assert-error]
|
||||
static_assert(is_assignable_to(type[Foo], Iterable[Unknown])) # error: [static-assert-error]
|
||||
|
||||
# TODO: should not error
|
||||
# error: [not-iterable]
|
||||
for x in Foo:
|
||||
reveal_type(x) # revealed: Unknown
|
||||
|
||||
class Bar(Any): ...
|
||||
|
||||
reveal_type(Bar.__mro__) # revealed: tuple[<class 'Bar'>, Any, <class 'object'>]
|
||||
|
||||
# TODO: these should pass
|
||||
static_assert(is_assignable_to(TypeOf[Bar], Iterable[Any])) # error: [static-assert-error]
|
||||
static_assert(is_assignable_to(type[Bar], Iterable[Any])) # error: [static-assert-error]
|
||||
|
||||
# TODO: should not error
|
||||
# error: [not-iterable]
|
||||
for x in Bar:
|
||||
# TODO: should reveal `Any`
|
||||
reveal_type(x) # revealed: Unknown
|
||||
```
|
||||
|
|
|
@ -1460,6 +1460,68 @@ static_assert(is_subtype_of(NominalSubtype, P))
|
|||
static_assert(not is_subtype_of(NotSubtype, P)) # error: [static-assert-error]
|
||||
```
|
||||
|
||||
A callable instance attribute is not sufficient for a type to satisfy a protocol with a method
|
||||
member: a method member specified by a protocol `P` must exist on the *meta-type* of `T` for `T` to
|
||||
be a subtype of `P`:
|
||||
|
||||
```py
|
||||
from typing import Callable, Protocol
|
||||
from ty_extensions import static_assert, is_assignable_to
|
||||
|
||||
class SupportsFooMethod(Protocol):
|
||||
def foo(self): ...
|
||||
|
||||
class SupportsFooAttr(Protocol):
|
||||
foo: Callable[..., object]
|
||||
|
||||
class Foo:
|
||||
def __init__(self):
|
||||
self.foo: Callable[..., object] = lambda *args, **kwargs: None
|
||||
|
||||
static_assert(not is_assignable_to(Foo, SupportsFooMethod))
|
||||
static_assert(is_assignable_to(Foo, SupportsFooAttr))
|
||||
```
|
||||
|
||||
The reason for this is that some methods, such as dunder methods, are always looked up on the class
|
||||
directly. If a class with an `__iter__` instance attribute satisfied the `Iterable` protocol, for
|
||||
example, the `Iterable` protocol would not accurately describe the requirements Python has for a
|
||||
class to be iterable at runtime. Allowing callable instance attributes to satisfy method members of
|
||||
protocols would also make `issubclass()` narrowing of runtime-checkable protocols unsound, as the
|
||||
`issubclass()` mechanism at runtime for protocols only checks whether a method is accessible on the
|
||||
class object, not the instance. (Protocols with non-method members cannot be passed to
|
||||
`issubclass()` at all at runtime.)
|
||||
|
||||
```py
|
||||
from typing import Iterable, Any
|
||||
from ty_extensions import static_assert, is_assignable_to
|
||||
|
||||
class Foo:
|
||||
def __init__(self):
|
||||
self.__iter__: Callable[..., object] = lambda *args, **kwargs: None
|
||||
|
||||
static_assert(not is_assignable_to(Foo, Iterable[Any]))
|
||||
```
|
||||
|
||||
Because method members must always be available on the class, it is safe to access a method on
|
||||
`type[P]`, where `P` is a protocol class, just like it is generally safe to access a method on
|
||||
`type[C]` where `C` is a nominal class:
|
||||
|
||||
```py
|
||||
from typing import Protocol
|
||||
|
||||
class Foo(Protocol):
|
||||
def method(self) -> str: ...
|
||||
|
||||
def f(x: Foo):
|
||||
reveal_type(type(x).method) # revealed: def method(self) -> str
|
||||
|
||||
class Bar:
|
||||
def __init__(self):
|
||||
self.method = lambda: "foo"
|
||||
|
||||
f(Bar()) # error: [invalid-argument-type]
|
||||
```
|
||||
|
||||
## Equivalence of protocols with method members
|
||||
|
||||
Two protocols `P1` and `P2`, both with a method member `x`, are considered equivalent if the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue