ruff/crates/ty_python_semantic
Ibraheem Ahmed 674d3902c6
[ty] Only prefer declared types in non-covariant positions (#22068)
## Summary

The following snippet currently errors because we widen the inferred
type, even though `X` is covariant over `T`. If `T` was contravariant or
invariant, this would be fine, as it would lead to an assignability
error anyways.

```python
class X[T]:
    def __init__(self: X[None]): ...

    def pop(self) -> T:
        raise NotImplementedError

# error: Argument to bound method `__init__` is incorrect: Expected `X[None]`, found `X[int | None]`
x: X[int | None] = X()
```

There are some cases where it is still helpful to prefer covariant
declared types, but this error seems hard to fix otherwise, and makes
our heuristics more consistent overall.
2025-12-19 17:27:31 -05:00
..
resources [ty] Only prefer declared types in non-covariant positions (#22068) 2025-12-19 17:27:31 -05:00
src [ty] Only prefer declared types in non-covariant positions (#22068) 2025-12-19 17:27:31 -05:00
tests [ty] Use datatest instead of dirtest (#21937) 2025-12-18 18:05:02 +00:00
Cargo.toml [ty] Use datatest instead of dirtest (#21937) 2025-12-18 18:05:02 +00:00
mdtest.py [ty] Lockfiles for mdtests with external dependencies (#22077) 2025-12-19 14:29:52 +01:00
mdtest.py.lock Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00