-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make Output[T] covariant in Python SDK #7483
Conversation
PR is now waiting for a maintainer to run the acceptance tests. Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR Further commands available:
|
In short, this allows subtyping to correctly "propagate" through an Output; if Cow is a subtype of Animal, this commit makes it so Output[Cow] is treated as a subtype of Output[Animal]. Without this change, users of the Python SDK occasionally contend with a confusing error message that is resolved by adding a call to `.apply(lambda x: x)` to satisfy mypy. Touches pulumi#3767. Fix pulumi#6843.
PR is now waiting for a maintainer to run the acceptance tests. Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR Further commands available:
|
On Wed, Jul 14, 2021 at 11:00 AM Josh Studt ***@***.***> wrote:
If this is approved I'd like to do similar in the dotnet SDK :) Similarly,
if this is approved, is there any reason to not do the same in Input<T>?
In Python, Input[T] is already covariant as a result of its definition as
Union[T, Awaitable[T], 'Output[T]'].
|
@benesch Makes sense for python. I don't think it is already covariant in the dotnet SDK. Sorry I deleted my comment though, thought it made more sense to make it on the issue instead of your PR |
/run-acceptance-tests |
Please view the results of the PR Build + Acceptance Tests Run Here |
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.
LGTM.
Should we also add a contravariant type variable for Inputs
in a future change? cc @lukehoban @t0yv0 @komalali @justinvp
I was slightly scared to recommend going ahead with this as I don't feel I have a good handle on all the implications, but I guess we will find out now that it is in :)
|
Are we running mypy as part of our build to catch any possible impacts of this? |
Agree!
The python/sdk library definitely gets a mypy run via the |
Perfect. Covariance and contravariance is something I've always struggled to keep straight. |
Note that we do look at the type hints at runtime and it was necessary to pass a definition for pulumi/sdk/python/lib/pulumi/_types.py Lines 725 to 728 in 0bcca38
I think this was only needed for |
In short, this allows subtyping to correctly "propagate" through an Output;
if Cow is a subtype of Animal, this commit makes it so Output[Cow] is
treated as a subtype of Output[Animal].
Without this change, users of the Python SDK occasionally contend with a
confusing error message that is resolved by adding a call to
.apply(lambda x: x)
to satisfy mypy.Touches #3767.
Fix #6843.
cc @lukehoban @t0yv0