-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Allow gradual lower/upper bounds in a constraint set #21957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ An unbounded typevar can specialize to any type. We will specialize the typevar | |
| bound of all of the types that satisfy the constraint set. | ||
|
|
||
| ```py | ||
| from typing import Never | ||
| from typing import Any, Never | ||
| from ty_extensions import ConstraintSet, generic_context | ||
|
|
||
| # fmt: off | ||
|
|
@@ -26,6 +26,8 @@ def unbounded[T](): | |
| reveal_type(generic_context(unbounded).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@unbounded = object] | ||
| reveal_type(generic_context(unbounded).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: ty_extensions.Specialization[T@unbounded = Any] | ||
| reveal_type(generic_context(unbounded).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
| # revealed: None | ||
| reveal_type(generic_context(unbounded).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
|
|
@@ -68,6 +70,10 @@ class Unrelated: ... | |
| def bounded[T: Base](): | ||
| # revealed: ty_extensions.Specialization[T@bounded = Base] | ||
| reveal_type(generic_context(bounded).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@bounded = Base] | ||
| reveal_type(generic_context(bounded).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: ty_extensions.Specialization[T@bounded = Base & Any] | ||
| reveal_type(generic_context(bounded).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
| # revealed: None | ||
| reveal_type(generic_context(bounded).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
|
|
@@ -94,26 +100,42 @@ def bounded_by_gradual[T: Any](): | |
| # TODO: revealed: ty_extensions.Specialization[T@bounded_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = object] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = object] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = Any] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
| # revealed: None | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = Base] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Base))) | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = object] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.range(Base, T, object))) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual = Unrelated] | ||
| reveal_type(generic_context(bounded_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Unrelated))) | ||
|
|
||
| def bounded_by_gradual_list[T: list[Any]](): | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = Top[list[Any]]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[object]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[object]))) | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[Any]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Any]))) | ||
| # revealed: None | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[Base]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Base]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[Base]] | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = Top[list[Any]]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(list[Base], T, object))) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[Unrelated]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Unrelated]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = list[Unrelated]] | ||
| # revealed: ty_extensions.Specialization[T@bounded_by_gradual_list = Top[list[Any]]] | ||
| reveal_type(generic_context(bounded_by_gradual_list).specialize_constrained(ConstraintSet.range(list[Unrelated], T, object))) | ||
| ``` | ||
|
|
||
| ## Constrained typevar | ||
|
|
@@ -142,12 +164,21 @@ def constrained[T: (Base, Unrelated)](): | |
| # revealed: None | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
|
Comment on lines
166
to
+169
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right |
||
| # revealed: None | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained = Base] | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Never, T, Base))) | ||
| # revealed: ty_extensions.Specialization[T@constrained = Base] | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Base, T, object))) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained = Unrelated] | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Never, T, Unrelated))) | ||
| # revealed: ty_extensions.Specialization[T@constrained = Unrelated] | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Unrelated, T, object))) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained = Base] | ||
| reveal_type(generic_context(constrained).specialize_constrained(ConstraintSet.range(Never, T, Super))) | ||
|
|
@@ -178,15 +209,25 @@ def constrained_by_gradual[T: (Base, Any)](): | |
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Base] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Base & Any] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Base] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Base))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Base] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Base, T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Unrelated] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Never, T, Unrelated))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = object] | ||
| reveal_type(generic_context(constrained_by_gradual).specialize_constrained(ConstraintSet.range(Unrelated, T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual = Base] | ||
|
|
@@ -206,6 +247,11 @@ def constrained_by_two_gradual[T: (Any, Any)](): | |
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual = object] | ||
| reveal_type(generic_context(constrained_by_two_gradual).specialize_constrained(ConstraintSet.always())) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_two_gradual = Any] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual = object] | ||
| reveal_type(generic_context(constrained_by_two_gradual).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual = Any] | ||
| reveal_type(generic_context(constrained_by_two_gradual).specialize_constrained(ConstraintSet.range(Never, T, Any))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained_by_two_gradual).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
|
|
@@ -233,14 +279,24 @@ def constrained_by_two_gradual[T: (Any, Any)](): | |
| def constrained_by_gradual_list[T: (list[Base], list[Any])](): | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[object]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[object]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Base] & list[Any]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Any]))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Base]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(list[Base], T, object))) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Unrelated]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(Never, T, list[Unrelated]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Unrelated]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_gradual_list).specialize_constrained(ConstraintSet.range(list[Unrelated], T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list = list[Super]] | ||
|
|
@@ -257,14 +313,25 @@ def constrained_by_gradual_list[T: (list[Base], list[Any])](): | |
| def constrained_by_gradual_list_reverse[T: (list[Any], list[Base])](): | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.always())) | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[object]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(Never, T, list[object]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Base] & list[Any]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(Never, T, list[Any]))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(Never, T, list[Base]))) | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Base]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(list[Base], T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Unrelated]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(Never, T, list[Unrelated]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Unrelated]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_gradual_list_reverse).specialize_constrained(ConstraintSet.range(list[Unrelated], T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_gradual_list_reverse = list[Super]] | ||
|
|
@@ -280,15 +347,26 @@ def constrained_by_two_gradual_lists[T: (list[Any], list[Any])](): | |
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.always())) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(Never, T, object))) | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = list[Any]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(Never, T, list[Any]))) | ||
| # revealed: None | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.never())) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = list[Base]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(Never, T, list[Base]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Base]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(list[Base], T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = list[Unrelated]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(Never, T, list[Unrelated]))) | ||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Unrelated]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = Top[list[Any]]] | ||
| reveal_type(generic_context(constrained_by_two_gradual_lists).specialize_constrained(ConstraintSet.range(list[Unrelated], T, object))) | ||
|
|
||
| # TODO: revealed: ty_extensions.Specialization[T@constrained_by_gradual = list[Any]] | ||
| # revealed: ty_extensions.Specialization[T@constrained_by_two_gradual_lists = list[Super]] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this seems like the right solution here