Skip to content
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

Escape import names for python #12447

Merged
merged 1 commit into from Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/python
description: Fix handling of reserved words in imports.
2 changes: 1 addition & 1 deletion pkg/codegen/python/gen_program.go
Expand Up @@ -256,7 +256,7 @@ func (g *generator) genPreamble(w io.Writer, program *pcl.Program, preambleHelpe
}
control := importSet[pkg]
if control.ImportAs {
imports = append(imports, fmt.Sprintf("import %s as %s", pkg, control.Pkg))
imports = append(imports, fmt.Sprintf("import %s as %s", pkg, EnsureKeywordSafe(control.Pkg)))
} else {
imports = append(imports, fmt.Sprintf("import %s", pkg))
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/codegen/testing/test/program_driver.go
Expand Up @@ -245,6 +245,11 @@ var PulumiPulumiProgramTests = []ProgramTest{
Directory: "throw-not-implemented",
Description: "Function notImplemented is compiled to a runtime error at call-site",
},
{
Directory: "python-reserved",
Description: "Test python reserved words aren't used",
Skip: allProgLanguages.Except("python"),
},
}

var PulumiPulumiYAMLProgramTests = []ProgramTest{
Expand Down
21 changes: 21 additions & 0 deletions pkg/codegen/testing/test/testdata/lambda-0.1.0.json
@@ -0,0 +1,21 @@
{
"$schema": "https://raw.githubusercontent.com/pulumi/pulumi/master/pkg/codegen/schema/pulumi.json",
"name": "lambda",
"version": "0.1.0",
"//": "Test schema for checking python codegen when packages, modules and types have reserved names.",
"resources": {
"lambda:lambda:Lambda": {
"properties": {
"lambda": {
"type": "string"
}
},
"inputProperties": {
"lambda": {
"type": "string"
}
},
"type": "object"
}
}
}
@@ -0,0 +1,7 @@
resource assert "lambda:lambda:Lambda" {
lambda = "dns"
}

output global {
value = assert.lambda
}
@@ -0,0 +1,5 @@
import pulumi
import pulumi_lambda as lambda_

assert_ = lambda_.lambda_.Lambda("assert", lambda_="dns")
pulumi.export("global", assert_.lambda_)
1 change: 1 addition & 0 deletions pkg/codegen/testing/utils/host.go
Expand Up @@ -79,5 +79,6 @@ func NewHost(schemaDirectoryPath string) plugin.Host {

SchemaProvider{"other", "0.1.0"},
SchemaProvider{"synthetic", "1.0.0"},
SchemaProvider{"lambda", "0.1.0"},
)
}