Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
178ee89
Use union to hold typevar constraints
dcreager Mar 31, 2025
feb1ea9
Add Type::TypeVar variant
dcreager Mar 31, 2025
3c1ad79
Fix failing tests
dcreager Mar 31, 2025
59430b6
doc Type::TypeVar
dcreager Apr 1, 2025
77ffa80
More correct handling of final bounds/constraints
dcreager Apr 1, 2025
6f86720
use list[T] so generic funcs are callable even with Never
dcreager Apr 1, 2025
cf81967
lint
dcreager Apr 1, 2025
6615df1
Add (currently failing) narrowing tests
dcreager Apr 1, 2025
b2f5a2a
Typevars _can_ be fully static I guess
dcreager Apr 1, 2025
590680c
Simplify intersections with constrained typevars
dcreager Apr 1, 2025
e6b7d40
Merge branch 'main' into dcreager/typevar-type
dcreager Apr 1, 2025
debd60a
Fix tests
dcreager Apr 1, 2025
aa391fd
lint
dcreager Apr 1, 2025
e57e62e
Update crates/red_knot_python_semantic/src/types/type_ordering.rs
dcreager Apr 1, 2025
3df79cc
Clarify that typevar is subtype of object too
dcreager Apr 2, 2025
5b08e93
Clarify non-fully-static bounded typevars aren't subtypes
dcreager Apr 2, 2025
82e810f
Add more tests for constrained gradual typevars
dcreager Apr 2, 2025
a3d7253
Update crates/red_knot_python_semantic/src/types.rs
dcreager Apr 2, 2025
15682d5
Simplify intersections with constrained typevars w/o glossing into union
dcreager Apr 2, 2025
9e07efe
Simplify positive intersections too
dcreager Apr 2, 2025
fb63c22
Intersection of constraints is subtype of typevar
dcreager Apr 2, 2025
3459056
Better descriptions of intersections of constrained typevars
dcreager Apr 2, 2025
71d425e
Add multiple narrowing example
dcreager Apr 2, 2025
233e938
lint
dcreager Apr 2, 2025
9785202
Sort typevar constraints
dcreager Apr 2, 2025
c86af50
Remove moot todo
dcreager Apr 2, 2025
aa00895
Fold typevar match arms back into main match statement
dcreager Apr 3, 2025
d99d1d7
Remove moot comment
dcreager Apr 3, 2025
58f0995
Remove moot todo
dcreager Apr 3, 2025
42fd54a
Add more TODOs about OneOf connector
dcreager Apr 3, 2025
6bd69f1
add todos for unary/binary ops
dcreager Apr 3, 2025
1d6a917
Merge remote-tracking branch 'origin/main' into dcreager/typevar-type
dcreager Apr 3, 2025
37692f1
Merge remote-tracking branch 'origin/dcreager/typevar-type' into dcre…
dcreager Apr 3, 2025
3869dc6
Fix tests
dcreager Apr 3, 2025
0c1745b
Fix tests better
dcreager Apr 3, 2025
8bdd9e2
Support unary and binary ops
dcreager Apr 3, 2025
39244dd
Merge remote-tracking branch 'origin/main' into dcreager/typevar-type
dcreager Apr 3, 2025
30172a0
add super checks
dcreager Apr 3, 2025
7803ec3
remove parenthetical
dcreager Apr 3, 2025
b4d2a0c
use singleton bound
dcreager Apr 3, 2025
a3493cd
normalize upper bound
dcreager Apr 3, 2025
d2c7647
fix comment
dcreager Apr 3, 2025
f70d565
Apply suggestions from code review
dcreager Apr 3, 2025
f3afc91
typo
dcreager Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ def good_return[T: int](x: T) -> T:

def bad_return[T: int](x: T) -> T:
# TODO: error: int is not assignable to T
# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `T` and `Literal[1]`"
return x + 1
```

Expand Down Expand Up @@ -138,8 +137,6 @@ methods that are compatible with the return type, so the `return` expression is

```py
def same_constrained_types[T: (int, str)](t1: T, t2: T) -> T:
# TODO: no error
# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `T` and `T`"
return t1 + t2
```

Expand Down
116 changes: 116 additions & 0 deletions crates/red_knot_python_semantic/resources/mdtest/generics/pep695.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,120 @@ class C[T]:
reveal_type(x) # revealed: T
```

## Subtyping and assignability

An unbounded, unconstrained typevar is assignable to itself, but is not a subtype of itself (or any
other type), since it might be specialized to `Any`, which does not participate in the subtyping
relationship.

It is neither assignable to or a subtype of any other type (including other typevars), since we can
make no assumption about what type it will be specialized to.

```py
from knot_extensions import is_assignable_to, is_subtype_of, static_assert

def unbounded_unconstrained[T, U](t: T, u: U) -> None:
static_assert(is_assignable_to(T, T))
static_assert(is_assignable_to(U, U))
static_assert(not is_assignable_to(T, U))
static_assert(not is_assignable_to(U, T))

static_assert(not is_subtype_of(T, T))
static_assert(not is_subtype_of(U, U))
static_assert(not is_subtype_of(T, U))
static_assert(not is_subtype_of(U, T))
```

A bounded typevar is assignable to its bound, but the bound is not assignable to the typevar (since
the typevar might be specialized to a smaller type). (TODO: Unless the bound is final, in which case
the final class is also assignable to the typevar.)

```py
from typing_extensions import final

def bounded[T: int](t: T) -> None:
static_assert(is_assignable_to(T, int))
static_assert(not is_assignable_to(int, T))

static_assert(not is_subtype_of(T, int))
static_assert(not is_subtype_of(int, T))

@final
class FinalClass: ...

def bounded_final[T: FinalClass](t: T) -> None:
static_assert(is_assignable_to(T, FinalClass))
# TODO: is_assignable_to
static_assert(not is_assignable_to(FinalClass, T))

static_assert(not is_subtype_of(T, FinalClass))
static_assert(not is_subtype_of(FinalClass, T))
```

Two distinct typevars are not assignable to each other, even if they have the same bounds, since
there is (still) no guarantee that they will be specialized to the same type. (TODO: Unless the
bound is final, in which case we _can_ assume that the two typevars must always be specialized to
that final class.)

```py
def two_bounded[T: int, U: int](t: T, u: U) -> None:
static_assert(not is_assignable_to(T, U))
static_assert(not is_assignable_to(U, T))

static_assert(not is_subtype_of(T, U))
static_assert(not is_subtype_of(U, T))

def two_final_bounded[T: FinalClass, U: FinalClass](t: T, u: U) -> None:
# TODO: is_assignable_to
static_assert(not is_assignable_to(T, U))
# TODO: is_assignable_to
static_assert(not is_assignable_to(U, T))

static_assert(not is_subtype_of(T, U))
static_assert(not is_subtype_of(U, T))
```

A constrained typevar is assignable to the union of its constraints, but not to any of the
constraints individually. None of the constraints are assignable to the typevar.

```py
def constrained[T: (int, str)](t: T) -> None:
static_assert(not is_assignable_to(T, int))
static_assert(not is_assignable_to(T, str))
static_assert(is_assignable_to(T, int | str))
static_assert(not is_assignable_to(int, T))
static_assert(not is_assignable_to(str, T))
static_assert(not is_assignable_to(int | str, T))
Copy link
Contributor

Choose a reason for hiding this comment

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

But int & str should be assignable to T, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is! I had to reorder some of the subtype/assignable clauses to make this pass. The correct order is:

  • union
  • one-of / typevar
  • intersection

which aligns with what the extended DNF representation for types would be if/when we introduce OneOf as a new connective.

Copy link
Member Author

Choose a reason for hiding this comment

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

Per another comment, this description of the "correct order" is not accurate anymore. It's more subtle — LHS constrained typevars have to be handled first, then the order described above


static_assert(not is_subtype_of(T, int))
static_assert(not is_subtype_of(T, str))
static_assert(not is_subtype_of(T, int | str))
static_assert(not is_subtype_of(int, T))
static_assert(not is_subtype_of(str, T))
static_assert(not is_subtype_of(int | str, T))
```

Two distinct typevars are not assignable to each other, even if they have the same constraints, and
even if any of the constraints are final. There must always be at least two distinct constraints,
meaning that there is (still) no guarantee that they will be specialized to the same type.

```py
def two_constrainted[T: (int, str), U: (int, str)](t: T, u: U) -> None:
static_assert(not is_assignable_to(T, U))
static_assert(not is_assignable_to(U, T))

static_assert(not is_subtype_of(T, U))
static_assert(not is_subtype_of(U, T))

@final
class AnotherFinalClass: ...

def two_final_constrainted[T: (FinalClass, AnotherFinalClass), U: (FinalClass, AnotherFinalClass)](t: T, u: U) -> None:
static_assert(not is_assignable_to(T, U))
static_assert(not is_assignable_to(U, T))

static_assert(not is_subtype_of(T, U))
static_assert(not is_subtype_of(U, T))
```

[pep 695]: https://peps.python.org/pep-0695/
Loading
Loading