[ty] Expand test suite for assignment errors#24537
Conversation
| ## 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] | ||
| ``` |
There was a problem hiding this comment.
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.
| from collections.abc import Iterable | ||
|
|
||
| def _(source: list[str]): | ||
| target: Iterable[bytes] = source # error: [invalid-assignment] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Analogous to the section on unions just above.
| target: SupportsCheck = source # error: [invalid-assignment] | ||
| ``` | ||
|
|
||
| Missing protocol properties: |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
* 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)
* 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)
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.