-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Epic] Python: Improved Typing #12689
Comments
hi Justin, for folks interested in the progress of this, where would we go to observe the progress and/or help with things? |
We have a working implementation in #15957, however we ran into some performance issues when using MyPy 1. To work around this, we currently generate the TypedDict based args to behave differently depending on the used type checker. MyPy supports a special variable Here's an example from the AWS provider: if not MYPY:
class InstanceCpuOptionsArgsDict(TypedDict):
amd_sev_snp: NotRequired[pulumi.Input[str]]
core_count: NotRequired[pulumi.Input[int]]
threads_per_core: NotRequired[pulumi.Input[int]]
elif False:
InstanceCpuOptionsArgsDict: TypeAlias = Mapping[str, Any] When using Pyright, you get the full type checking experience within dictionary arguments, but not with MyPy. The MyPy behaviour matches the previous implementation where these input types could either take an However the question then becomes what style of arguments should we encourage? Currently we encourage the I'll also note that Pyright does not produce very readable error messages ExampleFor the following code, Pyright correctly notes an error, but MyPy checking passes. from pulumi_kubernetes.apps.v1 import Deployment
deployment = Deployment("nginx",
spec={
"selector":{
"match_labels": { "app": "nginx" }
},
"replicas": "one", # This should be a Input[int]
"template": { "spec": { "containers": [{ "name": "nginx", "image": "nginx" }] }
}
}
) MyPy reports no issues
Pyright reports an error, but the error message is not very friendly, and it is not obvious what the actual error is.
Footnotes |
For Python codegen we generate classes for inputs of object type to enable strong typing for these inputs. However, this leads to somewhat verbose syntax, requiring to instantiate the "args classes". See #11500 for a motivating example.
With PEP 589 – TypedDict and PEP 655 – Marking individual TypedDict items as required or potentially-missing there is now a more concise way to achieve the same result.
For each input object type we can generate a Args class as before, as well as a ArgsDict TypedDict based type. To support backwards compatibility, inputs of object type will accept the union of the two types.
Related issues: #11732
Example with Args
Example with ArgsDict
Design (M103)
Implementation Plan
M104
Rollout & Announce (M105)
The text was updated successfully, but these errors were encountered: