Skip to content

Commit 0639da2

Browse files
authored
[ty] ~T should never be assignable to T (#20606)
## Summary Currently we do not emit an error on this code: ```py from ty_extensions import Not def f[T](x: T, y: Not[T]) -> T: x = y return x ``` But we should do! `~T` should never be assignable to `T`. This fixes a small regression introduced in 14fe122#diff-8049ab5af787dba29daa389bbe2b691560c15461ef536f122b1beab112a4b48aR1443-R1446, where a branch that previously returned `false` was replaced with a branch that returns `C::always_satisfiable` -- the opposite of what it used to be! The regression occurred because we didn't have any tests for this -- so I added some tests in this PR that fail on `main`. I only spotted the problem because I was going through the code of `has_relation_to_impl` with a fine toothcomb for #20602 😄
1 parent caf48f4 commit 0639da2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

crates/ty_python_semantic/resources/mdtest/generics/legacy/functions.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,17 @@ age, name = team.employees[0]
530530
reveal_type(age) # revealed: Age
531531
reveal_type(name) # revealed: Name
532532
```
533+
534+
## `~T` is never assignable to `T`
535+
536+
```py
537+
from typing import TypeVar
538+
from ty_extensions import Not
539+
540+
T = TypeVar("T")
541+
542+
def f(x: T, y: Not[T]) -> T:
543+
x = y # error: [invalid-assignment]
544+
y = x # error: [invalid-assignment]
545+
return x
546+
```

crates/ty_python_semantic/resources/mdtest/generics/pep695/functions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,14 @@ def _(x: int):
543543

544544
reveal_type(C().implicit_self(x)) # revealed: tuple[C, int]
545545
```
546+
547+
## `~T` is never assignable to `T`
548+
549+
```py
550+
from ty_extensions import Not
551+
552+
def f[T](x: T, y: Not[T]) -> T:
553+
x = y # error: [invalid-assignment]
554+
y = x # error: [invalid-assignment]
555+
return x
556+
```

crates/ty_python_semantic/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ impl<'db> Type<'db> {
15671567
(Type::Intersection(intersection), Type::NonInferableTypeVar(_))
15681568
if intersection.negative(db).contains(&target) =>
15691569
{
1570-
ConstraintSet::from(true)
1570+
ConstraintSet::from(false)
15711571
}
15721572

15731573
// Two identical typevars must always solve to the same type, so they are always

0 commit comments

Comments
 (0)