I'm trying to wrap my head around Pulumi python's ...
# python
m
I'm trying to wrap my head around Pulumi python's treatement of
None
values. A couple of weird things I've noticed: • Any `None`s that I pass to a resource (for example, a label on a kubernetes resource) seems to get completely ignored • Any exports of value
None
are also ignored: it's as if there's no export at all. I've found a couple of issues discussing possibly related things: • https://github.com/pulumi/pulumi/issues/7376 talks about exporting
undefined
from Typescript apps. • https://github.com/pulumi/pulumi-dotnet/issues/18 shows a dotnet example of a dictionary with a key with a null value getting "coerced" into an empty dictionary Just to be clear: I'm not actually trying to export
None
values or feed
nulls
into any of the resources I'm declaring with Pulumi, I just am making mistakes, and they're really confusing to debug with the current (silent) behavior. I'd much rather have my pulumi app crash at runtime, or at least get a warning. Am I understanding this correctly? Is this behavior documented anywhere? Is it configurable or is it a bug? Is this just part of the Pulumi learning curve?
m
If I understand you correctly, then this is how Python (by convention) handles optional arguments: The default value of the argument is defined as
None
. The code cannot distinguish between you passing in
None
explicitly or the default
None
. Example:
Copy code
class RandomId(pulumi.CustomResource):
    @overload
    def __init__(__self__,
                 resource_name: str,
                 opts: Optional[pulumi.ResourceOptions] = None,
                 byte_length: Optional[pulumi.Input[int]] = None,
                 keepers: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None,
                 prefix: Optional[pulumi.Input[str]] = None,
                 __props__=None):
m
@modern-zebra-45309 fair point! I should have given a concrete example. Consider this k8s resource declared with Pulumi:
Copy code
argocd_namespace = k8s.core.v1.Namespace(
    "demo",
    metadata={
        "name": "demo",
        "labels": {
            "i-am-none", None,
        },
    },
)
(in practice, that
None
value is usually something a little more complicated and non-obvious, like an
Output
that will eventually resolve to
None
) There's nothing fundamental about Python that's preventing Pulumi from noticing this mistake. IMO, this feels like it should be an error to me. However, currently Pulumi just happily ignores this: no diff, no warnings, nothing.
m
If you explicitly set
None
, I'd assume that this would be caught by a type checker, because "labels" should be
pulumi.Input[Mapping[str, pulumi.Input[str]]]
. My guess is that an output that resolves to
None
leads to the label being dropped, because labels have to be "string": "string". But I don't know where this happens, https://github.com/pulumi/pulumi-kubernetes/blob/master/provider/pkg/metadata/labels.go doesn't seem to handle nulls explicitly.