-
Notifications
You must be signed in to change notification settings - Fork 118
variance for parameter specification and type variable tuples #1771
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # This sample tests variance inference for TypeVarTuple type parameters. | ||
|
|
||
| from typing import Callable, ParamSpec | ||
|
|
||
| P_co = ParamSpec("P_co", covariant=True) | ||
| P_contra = ParamSpec("P_contra", contravariant=True) | ||
| P_infer = ParamSpec("P_infer", infer_variance=True) | ||
|
Comment on lines
+5
to
+7
Owner
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. how come these are unused
Owner
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. is it just to make sure theres no error on the arguments? if so should add a comment |
||
|
|
||
|
|
||
| class ShouldBeContravariant1[**OutP]: | ||
| def f(self) -> Callable[OutP, None]: ... | ||
|
Comment on lines
+10
to
+11
Owner
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. should also have a test for the most common usage: class ShouldBeContravariant2[**OutP]:
def f(self, *args: P.args, **kwargs: P.kwargs): ... |
||
|
|
||
|
|
||
| vcontra1_1: ShouldBeContravariant1[object] = ShouldBeContravariant1[int]() # pyright: ignore[reportAssignmentType] | ||
| vcontra1_2: ShouldBeContravariant1[int] = ShouldBeContravariant1[object]() # OK | ||
|
|
||
|
|
||
| class ShouldBeCovariant1[**OutP]: | ||
| def f(self, fn: Callable[OutP, None]) -> None: ... | ||
|
|
||
|
|
||
| vco1_1: ShouldBeCovariant1[int] = ShouldBeCovariant1[object]() # pyright: ignore[reportAssignmentType] | ||
| vco1_2: ShouldBeCovariant1[object] = ShouldBeCovariant1[int]() # OK | ||
|
|
||
|
|
||
| class ShouldBeInvariant1[**OutP]: | ||
| def f(self, fn: Callable[OutP, None]) -> Callable[OutP, None]: ... | ||
|
|
||
|
|
||
| vinv1_1: ShouldBeInvariant1[object] = ShouldBeInvariant1[int]() # pyright: ignore[reportAssignmentType] | ||
| vinv1_2: ShouldBeInvariant1[int] = ShouldBeInvariant1[object]() # pyright: ignore[reportAssignmentType] | ||
|
Owner
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. honestly i have never encountered a single situation where a typevartuple has been useful. could you please provide a realistic example of when anyone would ever use one, and how it could benefit from supporting variance? otherwise i dont see why we should even bother to add variance support to them or touch them in any way. it sounds like it was just scope creeped in your discourse thread about |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # This sample tests variance inference for ParamSpec type parameters. | ||
|
|
||
| from typing import TypeVarTuple | ||
|
|
||
| Ts_co = TypeVarTuple("Ts_co", covariant=True) | ||
| Ts_contra = TypeVarTuple("Ts_contra", contravariant=True) | ||
| Ts_infer = TypeVarTuple("Ts_infer", infer_variance=True) | ||
|
Comment on lines
+5
to
+7
Owner
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. comment saying why these unused typevar variables are here |
||
|
|
||
|
|
||
| class ShouldBeContravariant1[*OutTs]: | ||
| def f(self, t: tuple[*OutTs]): ... | ||
|
|
||
|
|
||
| vcontra1_1: ShouldBeContravariant1[object] = ShouldBeContravariant1[int]() # pyright: ignore[reportAssignmentType] | ||
| vcontra1_2: ShouldBeContravariant1[int] = ShouldBeContravariant1[object]() # OK | ||
|
|
||
|
|
||
| class ShouldBeCovariant1[*OutTs]: | ||
| def f(self) -> tuple[*OutTs]: ... | ||
|
|
||
|
|
||
| vco1_1: ShouldBeCovariant1[int] = ShouldBeCovariant1[object]() # pyright: ignore[reportAssignmentType] | ||
| vco1_2: ShouldBeCovariant1[object] = ShouldBeCovariant1[int]() # OK | ||
|
|
||
|
|
||
| class ShouldBeInvariant1[*OutTs]: | ||
| def f(self, t: tuple[*OutTs]) -> tuple[*OutTs]: ... | ||
|
|
||
|
|
||
| vinv1_1: ShouldBeInvariant1[object] = ShouldBeInvariant1[int]() # pyright: ignore[reportAssignmentType] | ||
| vinv1_2: ShouldBeInvariant1[int] = ShouldBeInvariant1[object]() # pyright: ignore[reportAssignmentType] | ||
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.
duplicated code