Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class SecondRequiredArgument:
def __len__(self, v: int) -> Literal[1]:
return 1

# TODO: Emit a diagnostic
# this is fine: the call succeeds at runtime since the second argument is optional
reveal_type(len(SecondOptionalArgument())) # revealed: Literal[0]
Comment on lines +263 to 264
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could consider emitting a diagnostic on the definition of __len__ itself here (but that should be a disabled-by-default lint rule, IMO!), but I see no reason why we should emit a diagnostic at the len() call itself here. SecondOptionalArgument is a subtype of Sized.


# TODO: Emit a diagnostic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ from typing import Protocol, TypeVar
T = TypeVar("T")

class CanIndex(Protocol[T]):
def __getitem__(self, index: int) -> T: ...
def __getitem__(self, index: int, /) -> T: ...
Comment on lines 79 to +80
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent of this test is that list[str] should be a subtype of CanIndex; below, an instance of list[str] is passed into takes_in_protocol. But as written, list[str] is not a subtype of CanIndex, since all parameters of list.__getitem__ are positional-only


class ExplicitlyImplements(CanIndex[T]): ...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ from typing import Protocol, TypeVar
S = TypeVar("S")

class CanIndex(Protocol[S]):
def __getitem__(self, index: int) -> S: ...
def __getitem__(self, index: int, /) -> S: ...

class ExplicitlyImplements[T](CanIndex[T]): ...

Expand Down
25 changes: 22 additions & 3 deletions crates/ty_python_semantic/resources/mdtest/protocols.md
Original file line number Diff line number Diff line change
Expand Up @@ -1813,9 +1813,28 @@ class Foo:
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:
Because method members are always looked up on the meta-type of an object when testing assignability
and subtyping, we understand that `IterableClass` here is a subtype of `Iterable[int]` even though
`IterableClass.__iter__` has the wrong signature:

```py
from typing import Iterator, Iterable
from ty_extensions import static_assert, is_subtype_of, TypeOf

class Meta(type):
def __iter__(self) -> Iterator[int]:
yield from range(42)

class IterableClass(metaclass=Meta):
def __iter__(self) -> Iterator[str]:
yield from "abc"

static_assert(is_subtype_of(TypeOf[IterableClass], Iterable[int]))
```

Enforcing that members must always be available on the class also means that 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
Expand Down
Loading