Skip to content

Commit

Permalink
Updating python dynamic provider types for inputs to Dict[str, Any] (#…
Browse files Browse the repository at this point in the history
…16102)

<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

Updating python dynamic provider types for inputs to Dict[str, Any],
from Any. I also added a Union so it was backward compatible, and
doesn't immediately throw type errors for users.

## 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`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->

---------

Co-authored-by: Thomas Gummerer <t.gummerer@gmail.com>
  • Loading branch information
rshade and tgummerer committed May 2, 2024
1 parent f0085de commit 03712a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: sdk/python
description: Update python dynamic provider types for inputs to Dict[str, Any], from Any
53 changes: 38 additions & 15 deletions sdk/python/lib/pulumi/dynamic/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
import asyncio
import base64
import pickle
from typing import TYPE_CHECKING, Any, ClassVar, List, Optional, cast, no_type_check
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Dict,
List,
Optional,
cast,
no_type_check,
)

import dill

Expand All @@ -34,7 +43,7 @@ class CheckResult:
CheckResult represents the results of a call to `ResourceProvider.check`.
"""

inputs: Any
inputs: Dict[str, Any]
"""
The inputs to use, if any.
"""
Expand All @@ -44,7 +53,7 @@ class CheckResult:
Any validation failures that occurred.
"""

def __init__(self, inputs: Any, failures: List["CheckFailure"]) -> None:
def __init__(self, inputs: Dict[str, Any], failures: List["CheckFailure"]) -> None:
self.inputs = inputs
self.failures = failures

Expand Down Expand Up @@ -118,12 +127,12 @@ class CreateResult:
The ID of the created resource.
"""

outs: Optional[Any]
outs: Optional[Dict[str, Any]]
"""
Any properties that were computed during creation.
"""

def __init__(self, id_: str, outs: Optional[Any] = None) -> None:
def __init__(self, id_: str, outs: Optional[Dict[str, Any]] = None) -> None:
self.id = id_
self.outs = outs

Expand All @@ -138,12 +147,16 @@ class ReadResult:
The ID of the resource ready back (or blank if missing).
"""

outs: Optional[Any]
outs: Optional[Dict[str, Any]]
"""
The current property state read from the live environment.
"""

def __init__(self, id_: Optional[str] = None, outs: Optional[Any] = None) -> None:
def __init__(
self,
id_: Optional[str] = None,
outs: Optional[Dict[str, Any]] = None,
) -> None:
self.id = id_
self.outs = outs

Expand All @@ -153,12 +166,12 @@ class UpdateResult:
UpdateResult represents the results of a call to `ResourceProvider.update`.
"""

outs: Optional[Any]
outs: Optional[Dict[str, Any]]
"""
Any properties that were computed during updating.
"""

def __init__(self, outs: Optional[Any] = None) -> None:
def __init__(self, outs: Optional[Dict[str, Any]] = None) -> None:
self.outs = outs


Expand All @@ -168,41 +181,51 @@ class ResourceProvider:
whose CRUD operations are implemented inside your Python program.
"""

def check(self, _olds: Any, news: Any) -> CheckResult:
def check(self, _olds: Dict[str, Any], news: Dict[str, Any]) -> CheckResult:
"""
Check validates that the given property bag is valid for a resource of the given type.
"""
return CheckResult(news, [])

def diff(self, _id: str, _olds: Any, _news: Any) -> DiffResult:
def diff(
self,
_id: str,
_olds: Dict[str, Any],
_news: Dict[str, Any],
) -> DiffResult:
"""
Diff checks what impacts a hypothetical update will have on the resource's properties.
"""
return DiffResult()

def create(self, props: Any) -> CreateResult:
def create(self, props: Dict[str, Any]) -> CreateResult:
"""
Create allocates a new instance of the provided resource and returns its unique ID
afterwards. If this call fails, the resource must not have been created (i.e., it is
"transactional").
"""
raise Exception("Subclass of ResourceProvider must implement 'create'")

def read(self, id_: str, props: Any) -> ReadResult:
def read(self, id_: str, props: Dict[str, Any]) -> ReadResult:
"""
Reads the current live state associated with a resource. Enough state must be included in
the inputs to uniquely identify the resource; this is typically just the resource ID, but it
may also include some properties.
"""
return ReadResult(id_, props)

def update(self, _id: str, _olds: Any, _news: Any) -> UpdateResult:
def update(
self,
_id: str,
_olds: Dict[str, Any],
_news: Dict[str, Any],
) -> UpdateResult:
"""
Update updates an existing resource with new values.
"""
return UpdateResult()

def delete(self, _id: str, _props: Any) -> None:
def delete(self, _id: str, _props: Dict[str, Any]) -> None:
"""
Delete tears down an existing resource with the given ID. If it fails, the resource is
assumed to still exist.
Expand Down

0 comments on commit 03712a1

Please sign in to comment.