Skip to content

put similar dunder-call tests next to each other #18343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
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
70 changes: 36 additions & 34 deletions crates/ty_python_semantic/resources/mdtest/call/dunder.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ ClassWithNormalDunder[0]

## Operating on instances

### Attaching dunder methods to instances in methods

When invoking a dunder method on an instance of a class, it is looked up on the class:

```py
Expand Down Expand Up @@ -116,6 +118,40 @@ def _(flag: bool):
reveal_type(this_fails[0]) # revealed: Unknown | str
```

### Dunder methods as class-level annotations with no value

Class-level annotations with no value assigned are considered instance-only, and aren't available as
dunder methods:

```py
from typing import Callable

class C:
__call__: Callable[..., None]

# error: [call-non-callable]
C()()

# error: [invalid-assignment]
_: Callable[..., None] = C()
```

And of course the same is true if we have only an implicit assignment inside a method:

```py
from typing import Callable

class C:
def __init__(self):
self.__call__ = lambda *a, **kw: None

# error: [call-non-callable]
C()()

# error: [invalid-assignment]
_: Callable[..., None] = C()
```

## When the dunder is not a method

A dunder can also be a non-method callable:
Expand Down Expand Up @@ -239,37 +275,3 @@ def _(flag: bool):
# error: [possibly-unbound-implicit-call]
reveal_type(c[0]) # revealed: str
```

## Dunder methods cannot be looked up on instances

Class-level annotations with no value assigned are considered instance-only, and aren't available as
dunder methods:

```py
from typing import Callable

class C:
__call__: Callable[..., None]

# error: [call-non-callable]
C()()

# error: [invalid-assignment]
_: Callable[..., None] = C()
```

And of course the same is true if we have only an implicit assignment inside a method:

```py
from typing import Callable

class C:
def __init__(self):
self.__call__ = lambda *a, **kw: None

# error: [call-non-callable]
C()()

# error: [invalid-assignment]
_: Callable[..., None] = C()
```