Skip to content
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

Use a separate type variable for static methods on Output #16172

Merged
merged 2 commits into from
May 23, 2024

Conversation

julienp
Copy link
Contributor

@julienp julienp commented May 10, 2024

Description

Pyright >= 1.1.354 assumes the typevar is Unknown if the generic class
that holds the staticmethods has no default type parameter, however in
our case the staticmethods have no link to the class's typevar.

Fixes #15914
Fixes #16194

Checklist

  • I have run make tidy to update any new dependencies
  • I have run make lint to verify my code passes the lint check
    • I have formatted my code using gofumpt
  • I have added tests that prove my fix is effective or that my feature works
  • I have run make changelog and committed the changelog/pending/<file> documenting my change
  • Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version

@pulumi-bot
Copy link
Contributor

pulumi-bot commented May 10, 2024

Changelog

[uncommitted] (2024-05-15)

Bug Fixes

  • [sdk/python] Use a separate type variable for static methods on Output
    #16172

  • [sdk/python] Relax Output.all types to better match the implementation
    #16172

@julienp julienp force-pushed the julienp/py-output-static-typevar branch from af29ea5 to f6baf41 Compare May 10, 2024 15:59
@julienp julienp force-pushed the julienp/py-output-static-typevar branch 2 times, most recently from a66a8cf to 7cddcf8 Compare May 10, 2024 16:14
@julienp julienp requested a review from justinvp May 10, 2024 16:18
@julienp julienp force-pushed the julienp/py-output-static-typevar branch 4 times, most recently from e5ee7ef to 03a4afd Compare May 13, 2024 09:30
@@ -50,6 +50,7 @@
T3 = TypeVar("T3")
T_co = TypeVar("T_co", covariant=True)
U = TypeVar("U")
U_co = TypeVar("U_co", covariant=True)
Copy link
Member

Choose a reason for hiding this comment

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

does covariance make sense on methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, at least for static methods.

https://typing.readthedocs.io/en/latest/spec/generics.html#variance

Variance is meaningful only when a type variable is bound to a generic class. If a type variable declared as covariant or contravariant is bound to a generic function or type alias, type checkers may warn users about this.

I'll update the PR to use plain U.

Pyright >= 1.1.354 assumes the typevar is Unknown if the generic class
that holds the staticmethods has no default type parameter, however in
our case the staticmethods have no link to the class's typevar.
@julienp julienp force-pushed the julienp/py-output-static-typevar branch from 03a4afd to 59ba669 Compare May 13, 2024 14:46
@julienp julienp marked this pull request as ready for review May 13, 2024 16:00
@julienp julienp requested a review from a team as a code owner May 13, 2024 16:00
@@ -437,22 +437,24 @@ def secret(val: Input[T]) -> "Output[T]":
# https://mypy.readthedocs.io/en/stable/more_types.html#type-checking-the-variants:~:text=considered%20unsafely%20overlapping
@overload
@staticmethod
def all(*args: "Output[T_co]") -> "Output[List[T_co]]": ... # type: ignore
def all(*args: "Output[Any]") -> "Output[List[Any]]": ... # type: ignore
Copy link
Contributor Author

@julienp julienp May 15, 2024

Choose a reason for hiding this comment

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

Previous types stated that all arguments have the same type, however you can pass mixed arguments. Because this returns a list type, and not a tuple, we can't provide overloads for specific types like we do in nodejs

export function all<T1, T2, T3, T4, T5, T6, T7, T8>(

pyright playground example using a tuple return

#16194

Copy link
Member

Choose a reason for hiding this comment

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

Yeh this is kinda sad but probably the most practical we can do for now

The previous types stated that all the arguments to `Output.all` should
be of the same type, however you can pass through a list of mixed types.

If `Output.all` returned a tuple isntead, we could provide overloads
that returned a `Output[tuple[T1, T2, ...]]` with the precise types.
With the return type being a list, we are limited to `List[Any]`.
@julienp julienp force-pushed the julienp/py-output-static-typevar branch from d1c653b to d0ce289 Compare May 15, 2024 13:44
@@ -0,0 +1,4 @@
changes:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Has this been added accidentally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, there are two changes here.

The initial change changed all T_co uses on static methods to U.

The 2nd change changes removes the type variable for from all and uses Any.

@@ -437,22 +437,24 @@ def secret(val: Input[T]) -> "Output[T]":
# https://mypy.readthedocs.io/en/stable/more_types.html#type-checking-the-variants:~:text=considered%20unsafely%20overlapping
@overload
@staticmethod
def all(*args: "Output[T_co]") -> "Output[List[T_co]]": ... # type: ignore
def all(*args: "Output[Any]") -> "Output[List[Any]]": ... # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

Yeh this is kinda sad but probably the most practical we can do for now

@julienp julienp added this pull request to the merge queue May 22, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 22, 2024
@julienp julienp added this pull request to the merge queue May 23, 2024
Merged via the queue into master with commit 9f6c520 May 23, 2024
49 checks passed
@julienp julienp deleted the julienp/py-output-static-typevar branch May 23, 2024 09:35
github-merge-queue bot pushed a commit that referenced this pull request May 24, 2024
Tentative changelog:

### Features

- [engine] Guess ID references of dependant resources when generating
code for import operations
  [#16208](#16208)


### Bug Fixes

- [engine] Check property dependencies and deleted-with relationships
for target dependents
  [#16220](#16220)

- [engine] Propagate dependencies of untargeted resources correctly
during targeted updates
  [#16247](#16247)

- [backend/diy] Rewrite DeletedWith references when renaming stacks
  [#16216](#16216)

- [sdk/python] Use a separate type variable for static methods on Output
  [#16172](#16172)

- [sdk/python] Relax Output.all types to better match the implementation
  [#16172](#16172)

- [sdkgen/python] Generate __init__.py files for modules that only
contain enumerations
  [#16229](#16229)
@justinvp justinvp mentioned this pull request May 24, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 24, 2024
To be merged after:
- #16261
- pulumi/pulumi-docker-containers#195

Tentative changelog...

### Features

- [engine] Guess ID references of dependant resources when generating
code for import operations
  [#16208](#16208)


### Bug Fixes

- [engine] Check property dependencies and deleted-with relationships
for target dependents
  [#16220](#16220)

- [engine] Propagate dependencies of untargeted resources correctly
during targeted updates
  [#16247](#16247)

- [backend/diy] Rewrite DeletedWith references when renaming stacks
  [#16216](#16216)

- [cli/state] Fix state renames involving DeletedWith

- [sdk/python] Use a separate type variable for static methods on Output
  [#16172](#16172)

- [sdk/python] Relax Output.all types to better match the implementation
  [#16172](#16172)

- [sdkgen/python] Generate __init__.py files for modules that only
contain enumerations
  [#16229](#16229)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants