-
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 |
It looks like PyCharm also picks |
Epic: Improved Typing #12689 This PR adds the flag `Languages.Python.InputTypes` to the schema, which can take the values `classes` or `classes-and-dicts`. In the first iteration of the feature, this defaults to `classes`, which leaves code generation as is and does not change input types.. If the flag is set to `classes-and-dicts`, `TypedDict` based types are generated next to the current `<Type>Args` classes. In the future this could be extended to support `dicts` to generate only `TypedDict` types. The generated types are conditional on the used type checker to work around perf issues in MyPy and PyCharm, see #12689 (comment) ```python if not MYPY: class DeploymentArgsDict(TypedDict): api_version: NotRequired[Input[str]] kind: NotRequired[Input[str]] metadata: NotRequired[Input['ObjectMetaArgsDict']] ... elif False: DeploymentArgsDict: TypeAlias = Mapping[str, Any] ``` Removing the workaround is tracked in #16408 --------- Co-authored-by: Anthony King <anthony@datapane.com> Co-authored-by: Justin Van Patten <jvp@justinvp.com>
### Proposed changes This PR requires pulumi/pulumi 3.121.0 and is stacked on top of #3066. Enable TypedDict input types for the Python SDK. The only manual change is to set `inputTypes` flag in the python language features of the schema. This is done here 20314f5. This flag is currently opt in, but will default to `classes-and-dicts` in the future. I ran `make build` afterwards. ### Related issues (optional) Epic pulumi/pulumi#12689
Enable TypedDict input types for the Python SDK. The only manual change is to set the InputTypes flag in the python language features of the schema. This is done here 25a4cec. This flag is currently opt in, but will default to classes-and-dicts in the future. I ran `make tfgen build_sdks` afterwards. Epic pulumi/pulumi#12689
Enable TypedDict input types for the Python SDK. The only manual change is to set the InputTypes flag in the python language features of the schema. This is done here 5a99a47. This flag is currently opt in, but will default to classes-and-dicts in the future. Epic pulumi/pulumi#12689
Enable TypedDict input types for the Python SDK. The only manual change is to set the InputTypes flag in the python language features of the schema. This is done here f11e86d. This flag is currently opt in, but will default to classes-and-dicts in the future. Epic pulumi/pulumi#12689
Enable TypedDict input types for the Python SDK. The only manual change is to set the InputTypes flag in the python language features of the schema. This is done here b7001de. This flag is currently opt in, but will default to classes-and-dicts in the future. Epic pulumi/pulumi#12689
Enable TypedDict input types for the Python SDK. The only manual change is to set the InputTypes flag in the python language features of the schema. This is done here e0ccec8. This flag is currently opt in, but will default to classes-and-dicts in the future. Epic pulumi/pulumi#12689
Closing this out as fixed! 🎉 Blog announcement here: https://www.pulumi.com/blog/pulumi-loves-python/ There is some remaining work to rollout fully across the long tail of providers, docs, and examples, but we'll track the remaining work via the individual issues. |
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
With type checking for dictionary literals, we now prefer this style over args classes pulumi/pulumi#12689
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
M106
M107
Future Iteration
TypedDictSettingSideBySide
#16375Rollout & Announce
The text was updated successfully, but these errors were encountered: