Skip to content

Conversation

@AlexWaygood
Copy link
Member

No description provided.

@AlexWaygood AlexWaygood added testing Related to testing Ruff itself ty Multi-file analysis & type inference labels Sep 11, 2025
Comment on lines +263 to 264
# this is fine: the call succeeds at runtime since the second argument is optional
reveal_type(len(SecondOptionalArgument())) # revealed: Literal[0]
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.

Comment on lines 79 to +80
class CanIndex(Protocol[T]):
def __getitem__(self, index: int) -> T: ...
def __getitem__(self, index: int, /) -> T: ...
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

Comment on lines 1816 to 1833
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` even though
`Foo.__iter__` resolves to a type with 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]))
```
Copy link
Member Author

Choose a reason for hiding this comment

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

A missing test that demonstrates another reason why it's important that dunder methods especially must be looked up on the meta-type when determining protocol subtyping/assignability

@AlexWaygood AlexWaygood marked this pull request as ready for review September 11, 2025 11:39
Co-authored-by: Carl Meyer <carl@astral.sh>
@AlexWaygood AlexWaygood enabled auto-merge (squash) September 11, 2025 14:38
@AlexWaygood AlexWaygood merged commit 0e3697a into main Sep 11, 2025
34 checks passed
@AlexWaygood AlexWaygood deleted the alex/proto-test-improvements branch September 11, 2025 14:42
dcreager added a commit that referenced this pull request Sep 11, 2025
* origin/main:
  [ty] Minor fixes to `Protocol` tests (#20347)
  [ty] use Type::Divergent to avoid panic in infinitely-nested-tuple implicit attribute (#20333)
  [ty] Require that implementors of `Constraints` also implement `Debug` (#20348)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Related to testing Ruff itself ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants