How to fix this waring , ```Argument of type "dict...
# python
o
How to fix this waring ,
Copy code
Argument of type "dict[str, str | int]" cannot be assigned to parameter "variables" of type "Input[Mapping[str, Input[str]]] | None" in function "__init__"
  Type "dict[str, str | int]" cannot be assigned to type "Input[Mapping[str, Input[str]]] | None"
    "dict[str, str | int]" is incompatible with "Mapping[str, Input[str]]"
      TypeVar "_VT_co@Mapping" is covariant
        Type "str | int" cannot be assigned to type "Input[str]"
          Type "int" cannot be assigned to type "Input[str]"
    "dict[str, str | int]" is incompatible with protocol "Awaitable[Mapping[str, Input[str]]]"
      "__await__" is not present
    "dict[str, str | int]" is incompatible with "Output[Mapping[str, Input[str]]]"
  ...PylancereportGeneralTypeIssues
Copy code
This is my code :


aws.lambda_.FunctionEnvironmentArgs(
            variables={
                "DESTN_BUCKET": s3_zip_log_bucket_name,
                "RDS_HOSTNAME": "<http://xxxx.ap-east-1.rds.amazonaws.com|xxxx.ap-east-1.rds.amazonaws.com>",
                "RDS_USERNAME": "admin",
                "RDS_PASSWORD": db_password,
                "RDS_PORT": "3306",
                "TTL": 172800,
            },
        ),

I think is need to do that to make dict convert to Input, But I don't know to do
m
Hi there — have a look at this example. It looks similar to your code, so might help: https://github.com/pulumi/examples/blob/258d3bad0a00020704743e37911c51be63c06bb4/aws-py-serverless-raw/__main__.py#L73-L89
This, for example, seems to work for me:
Copy code
some_dict = {
  "THING": "one",
  "OTHER_THING": 2,
}

fn = aws.lambda_.Function("fn",
    runtime="python3.9",
    handler="handler.handler",
    role=role.arn,
    code=pulumi.FileArchive("./function"),
    environment=aws.lambda_.FunctionEnvironmentArgs(
        variables=some_dict,
    ))
When deployed, I see the environment variables:
o
Hi, @miniature-musician-31262 Thank you for your reply, I can understand this code why not warring. https://github.com/pulumi/examples/blob/258d3bad0a00020704743e37911c51be63c06bb4/aws-py-serverless-raw/__main__.py#L73-L89. But for your code.
Copy code
some_dict = {
  "THING": "one",
  "OTHER_THING": 2,
}

fn = aws.lambda_.Function("fn",
    runtime="python3.9",
    handler="handler.handler",
    role=role.arn,
    code=pulumi.FileArchive("./function"),
    environment=aws.lambda_.FunctionEnvironmentArgs(
        variables=some_dict,
    ))
I can not understand, It looks like my codes, but it works! Just eluded pylance’s inspection?
so, There is no way to convert str to pulumi.Input[str] ?
m
In general you shouldn’t have to manually cast anything from a string to an input of string, as plain strings are also valid inputs. Where are your
s3_zip_log_bucket_name
and
db_password
values coming from, though? Are they plain strings, or outputs of some other resource?
f
The warning is easy to fix: make sure the values are strings
Copy code
...
                "RDS_PORT": "3306",
                "TTL": "172800",
...
these will be env vars anyway, so they will be strings.
However, it's weird that if you do this
Copy code
variables = {
      "DESTN_BUCKET": "blah",                
      "RDS_HOSTNAME": "<http://xxxx.ap-east-1.rds.amazonaws.com|xxxx.ap-east-1.rds.amazonaws.com>",
      "RDS_USERNAME": "admin",
      "RDS_PASSWORD": "blah",
      "RDS_PORT": "3306",
      "TTL": 172800,
}
then it works. It seems, the variables type is inferred as
dict[str, Unknown]
Copy code
variables = {                                                                
    "DES (variable) variables: dict[str, Unknown]
but if you define the type properly, then it becomes an error:
Copy code
variables: dict[str, str] = { 
      "DESTN_BUCKET": "blah",
      "RDS_HOSTNAME": "<http://xxxx.ap-east-1.rds.amazonaws.com|xxxx.ap-east-1.rds.amazonaws.com>",
      "RDS_USERNAME": "admin",
      "RDS_PASSWORD": "blah",
      "RDS_PORT": "3306",
E     "TTL": 172800,     ■ Expression of type "dict[str, str | int]" cannot be assigned to declared type "dict[str, str]"    "Literal[172800]" is incompatible with "str"
  }
Add quotes to TTL and it works 🙂
I think of an
Input[str]
as either
str
or
Output[str]
In fact, in the SDK it's defined as a Union
Copy code
Input = Union[T, Awaitable[T], "Output[T]"]
o
@miniature-musician-31262 Thanks for reminding me! The
s3_zip_log_bucket_name
and
db_password
values are string too, I find the key is
Copy code
"TTL": 172800
When I remove this line, It is fine.
@fierce-ability-58936 Thank you for your reply, you are right! I just realized how stupid my question is.
f
There are no stupid questions 🙂
519 Views