Skip to content

[ty] Expand test suite for assignment errors#24537

Merged
sharkdp merged 1 commit intomainfrom
david/invalid-assignment-extend-tests
Apr 10, 2026
Merged

[ty] Expand test suite for assignment errors#24537
sharkdp merged 1 commit intomainfrom
david/invalid-assignment-extend-tests

Conversation

@sharkdp
Copy link
Copy Markdown
Contributor

@sharkdp sharkdp commented Apr 10, 2026

Summary

Pulling out some new (snapshot) test cases from #24309 to make that easier to review.

The current output in the snapshots is completely boring but will change soon.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Apr 10, 2026
@sharkdp sharkdp added the testing Related to testing Ruff itself label Apr 10, 2026
Comment on lines +334 to +356
## Assigning an overload set

This test makes sure that error context from failed overload candidates does not leak
(`IncompatibleFoo.bar` is assignable to `SupportsFooAndBar.bar`):

```py
from typing import Protocol, overload, SupportsIndex

class SupportsFooAndBar(Protocol):
def foo(self, name: str): ...
def bar(self, x: bytes): ...

class IncompatibleFoo:
def foo(self, name_: str): ...
@overload
def bar(self, x: SupportsIndex): ...
@overload
def bar(self, x: bytes): ...
def bar(self, x: SupportsIndex | bytes): ...

def _(source: IncompatibleFoo):
target: SupportsFooAndBar = source # error: [invalid-assignment]
```
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a bit convoluted, but was important in tracking down a problem during the implementation. It originates from a real world case but was minified/altered a lot.

Comment on lines +361 to +364
from collections.abc import Iterable

def _(source: list[str]):
target: Iterable[bytes] = source # error: [invalid-assignment]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This case is interesting because it probably makes sense to (eventually) add a special cased hint instead of printing an error tree like:

info: type `list[str]` is not compatible with protocol `Iterable[bytes]`
info: └── protocol member `__iter__` is incompatible
info:     └── incompatible return types `Iterator[str]` and `Iterator[bytes]`
info:         └── type `Iterator[str]` is not compatible with protocol `Iterator[bytes]`
info:             └── incompatible return types `str` and `bytes`

target: bytes | None = source # error: [invalid-assignment]
```

## Intersections
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Analogous to the section on unions just above.

target: SupportsCheck = source # error: [invalid-assignment]
```

Missing protocol properties:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test case is important because missing protocol members are detected using a fast path, and so they are handled differently from incompatible protocol members (above).


```py
def _(source: int):
target: str | bytes | bool | None = source # error: [invalid-assignment]
Copy link
Copy Markdown
Contributor Author

@sharkdp sharkdp Apr 10, 2026

Choose a reason for hiding this comment

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

This is a boring case of a union assignment failure where we probably don't want to add any additional context. Just telling you that int is not assignable to str | bytes | bool | None is enough — no breakdown needed.

@sharkdp sharkdp marked this pull request as ready for review April 10, 2026 11:37
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh April 10, 2026 11:37
@sharkdp sharkdp merged commit 038ad83 into main Apr 10, 2026
50 checks passed
@sharkdp sharkdp deleted the david/invalid-assignment-extend-tests branch April 10, 2026 11:50
carljm added a commit that referenced this pull request Apr 10, 2026
* main:
  [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541)
  [ty] Add a `SupportedPythonVersion` enum (#24412)
  [ty] Ignore unsupported editor-selected Python versions (#24498)
  [ty] Add snapshots for `__init_subclass__` diagnostics (#24539)
  [ty] Minor fix in tests (#24538)
  [ty] Allow `Final` variable assignments in `__post_init__` (#24529)
  [ty] Expand test suite for assignment errors (#24537)
  [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535)
  [ty] Rework logic for synthesizing `TypedDict` methods (#24534)
  [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424)
  [ty] mdtest.py: update dependencies (#24533)
  Rename patterns and arguments source order iterator method (#24532)
  [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522)
  [ty] support super() in metaclass methods (#24483)
  [ty] Synthesize `__init__` for `TypedDict` (#24476)
carljm added a commit that referenced this pull request Apr 10, 2026
* main:
  Bump typing conformance suite commit to latest upstream (#24553)
  [ty] Reject deleting`Final` attributes (#24508)
  [ty] Respect property deleters in attribute deletion checks (#24500)
  [ty] stop unioning Unknown into types of un-annotated attributes (#24531)
  [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541)
  [ty] Add a `SupportedPythonVersion` enum (#24412)
  [ty] Ignore unsupported editor-selected Python versions (#24498)
  [ty] Add snapshots for `__init_subclass__` diagnostics (#24539)
  [ty] Minor fix in tests (#24538)
  [ty] Allow `Final` variable assignments in `__post_init__` (#24529)
  [ty] Expand test suite for assignment errors (#24537)
  [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535)
  [ty] Rework logic for synthesizing `TypedDict` methods (#24534)
  [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424)
  [ty] mdtest.py: update dependencies (#24533)
  Rename patterns and arguments source order iterator method (#24532)
  [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522)
  [ty] support super() in metaclass methods (#24483)
  [ty] Synthesize `__init__` for `TypedDict` (#24476)
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