Skip to content

Add extra diagnostic context for failed callable assignability due to positional-only or argument names #2662

@philj56

Description

@philj56

Summary

ty rejects matching protocols if a dunder method doesn't restrict itself to position-only arguments. This is fine, but the error message is rather hard to work out:

from typing import Any, Protocol, runtime_checkable

@runtime_checkable
class WithSlash(Protocol):
    def __getitem__(self, key: Any, /) -> Any: ...

@runtime_checkable
class NoSlash(Protocol):
    def __getitem__(self, key: Any) -> Any: ...

a: WithSlash = {1: 2}  # No error
b: NoSlash = {1: 2}  # error[invalid-assignment]: Object of type `dict[Unknown | int, Unknown | int]` is not assignable to `NoSlash`
assert(isinstance(a, WithSlash))  # True
assert(isinstance(b, NoSlash))  # True

It'd be nice if ty (or perhaps ruff) checked for special dunder method declarations that do not match the necessary format, or had some other way of making this error clearer (or even accepting it as-is).

Version

ty 0.0.14 (16597f5 2026-01-26)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions