ruff/crates/red_knot_python_semantic/resources/mdtest
Joey Bar e8e24310fb
[red-knot] Handle gradual intersection types in assignability (#16611)
## Summary

This mostly fixes #14899

My motivation was similar to the last comment by @sharkdp there. I ran
red_knot on a codebase and the most common error was patterns like this
failing:

```
def foo(x: str): ...

x: Any = ...
if isinstance(x, str):
    foo(x) # Object of type `Any & str` cannot be assigned to parameter 1 (`x`) of function `foo`; expected type `str`
```

The desired behavior is pretty much to ignore Any/Unknown when resolving
intersection assignability - `Any & str` should be assignable to `str`,
and `str` should be assignable to `str & Any`
 
The fix is actually very similar to the existing code in
`is_subtype_of`, we need to correctly handle intersections on either
side, while being careful to handle dynamic types as desired.

This does not fix the second test case from that issue:

```
static_assert(is_assignable_to(Intersection[Unrelated, Any], Not[tuple[Unrelated, Any]]))
```

but that's misleading because the root cause there has nothing to do
with gradual types. I added a simpler test case that also fails:

```
static_assert(is_assignable_to(Unrelated, Not[tuple[Unrelated]]))
```
This is because we don't determine that Unrelated does not subclass from
tuple so we can't rule out this relation. If that logic is improved then
this fix should also handle the case of the intersection

## Test Plan

Added a bunch of is_assignable_to tests, most of which failed before
this fix.
2025-03-11 07:58:56 -07:00
..
annotations [red-knot] Understand `typing.Callable` (#16493) 2025-03-08 03:58:52 +00:00
assignment [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
binary [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
boolean [red-knot] Statically known branches (#15019) 2024-12-21 11:33:10 +01:00
boundness_declaredness [red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890) 2025-02-04 08:27:17 +01:00
call [red-knot] Add support for calling `type[…]` (#16597) 2025-03-10 13:24:13 +01:00
comparison [red-knot] Correct modeling of dunder calls (#16368) 2025-02-25 20:38:15 +01:00
comprehensions [red-knot] Resolve references in eager nested scopes eagerly (#16079) 2025-02-19 10:22:30 -05:00
conditional [red-knot] Diagnostics for incorrect `bool` usages (#16238) 2025-02-21 19:26:05 +01:00
declaration [red-knot] Avoid undeclared path when raising conflicting declarations (#14958) 2024-12-17 09:49:39 +05:30
diagnostics [red-knot] Better diagnostics for method calls (#16362) 2025-02-25 09:58:08 +01:00
directives [red-knot] Add missing imports in mdtests (#15869) 2025-02-03 09:27:29 +00:00
doc [red-knot] Document 'public type of undeclared symbols' behavior (#16096) 2025-02-12 08:52:11 +01:00
exception [red-knot] MDTest: Use custom class names instead of builtins (#16269) 2025-02-20 12:25:55 +00:00
expression [red-knot] Infer `lambda` expression (#16547) 2025-03-11 11:25:20 +05:30
function [red-knot] function parameter types (#14802) 2024-12-06 12:55:56 -08:00
generics [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
import Add `OsSystem` support to mdtests (#16518) 2025-03-06 10:41:40 +01:00
literal [red-knot] Migrate `bool`/`str`/`repr` unit tests to Markdown tests (#15534) 2025-01-16 11:21:56 -08:00
loops [red-knot] Never is callable and iterable. Arbitrary attributes can be accessed. (#16533) 2025-03-06 15:59:19 +00:00
narrow Teach red-knot that `type(x)` is the same as `x.__class__` (#16301) 2025-02-21 21:05:48 +00:00
regression [red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890) 2025-02-04 08:27:17 +01:00
scopes [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
shadowing [red-knot] Merge Markdown code blocks inside a single section (#15950) 2025-02-05 22:26:15 +01:00
snapshots [red-knot] Correct modeling of dunder calls (#16368) 2025-02-25 20:38:15 +01:00
stubs [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
subscript [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
suppressions [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
type_of [red-knot] Method calls and the descriptor protocol (#16121) 2025-02-20 23:22:26 +01:00
type_properties [red-knot] Handle gradual intersection types in assignability (#16611) 2025-03-11 07:58:56 -07:00
type_qualifiers [red-knot] Handle possibly-unbound instance members (#16363) 2025-02-25 20:00:38 +01:00
unary [red-knot] Diagnostics for incorrect `bool` usages (#16238) 2025-02-21 19:26:05 +01:00
with [red-knot] Support unpacking `with` target (#16469) 2025-03-08 02:36:35 +00:00
.mdformat.toml [red-knot] have mdformat wrap mdtest files to 100 columns (#14020) 2024-10-31 21:00:51 +00:00
attributes.md [red-knot] Consistent spelling of "metaclass" and "meta-type" (#16576) 2025-03-09 12:30:32 +00:00
descriptor_protocol.md [red-knot] Consistent spelling of "metaclass" and "meta-type" (#16576) 2025-03-09 12:30:32 +00:00
final.md [red-knot] Add support for `@final` classes (#15070) 2024-12-19 21:02:14 +00:00
intersection_types.md [red-knot] Add missing imports in mdtests (#15869) 2025-02-03 09:27:29 +00:00
invalid_syntax.md [red-knot] Understand `typing.Callable` (#16493) 2025-03-08 03:58:52 +00:00
known_constants.md [red-knot] Add rule `invalid-type-checking-constant` (#16501) 2025-03-04 19:49:34 +00:00
mdtest_config.md [red-knot] Add missing imports in mdtests (#15869) 2025-02-03 09:27:29 +00:00
mdtest_custom_typeshed.md [red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890) 2025-02-04 08:27:17 +01:00
metaclass.md [red-knot] Consistent spelling of "metaclass" and "meta-type" (#16576) 2025-03-09 12:30:32 +00:00
mro.md [red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890) 2025-02-04 08:27:17 +01:00
pep695_type_aliases.md [red-knot] Move `type_alias_types` test to Markdown (#15607) 2025-01-20 09:55:54 +01:00
protocols.md [red-knot] Method calls and the descriptor protocol (#16121) 2025-02-20 23:22:26 +01:00
slots.md [red-knot] Add support for `@classmethod`s (#16305) 2025-02-24 09:55:34 +01:00
statically_known_branches.md [red-knot] Litate tests: minor follow-up (#15987) 2025-02-06 07:15:26 +00:00
sys_platform.md [red-knot] Method calls and the descriptor protocol (#16121) 2025-02-20 23:22:26 +01:00
sys_version_info.md [red-knot] Merge Markdown code blocks inside a single section (#15950) 2025-02-05 22:26:15 +01:00
terminal_statements.md [red-knot] Combine terminal statement support with statically known branches (#15817) 2025-02-05 17:47:49 -05:00
type_api.md [red-knot] Diagnostics for incorrect `bool` usages (#16238) 2025-02-21 19:26:05 +01:00
union_types.md [red-knot] add special case for float/complex (#16166) 2025-02-14 12:24:10 -08:00
unpacking.md [red-knot] Support unpacking `with` target (#16469) 2025-03-08 02:36:35 +00:00