Skip to content

Commit

Permalink
Add Python keyword tests
Browse files Browse the repository at this point in the history
...and also add support for transforming class names too.
  • Loading branch information
joeduffy committed Jul 16, 2018
1 parent b04fe1c commit 7627bd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/tfgen/generate_python.go
Expand Up @@ -331,7 +331,7 @@ func (g *pythonGenerator) emitRawDocComment(w *tools.GenWriter, comment, prefix

func (g *pythonGenerator) emitPlainOldType(w *tools.GenWriter, pot *plainOldType) {
// Produce a class definition with optional """ comment.
w.Writefmtln("class %s(object):", pot.name)
w.Writefmtln("class %s(object):", pyClassName(pot.name))
if pot.doc != "" {
g.emitDocComment(w, pot.doc, " ")
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func (g *pythonGenerator) emitResourceType(mod *module, res *resourceType) (stri
defer contract.IgnoreClose(w)

// Produce a class definition with optional """ comment.
w.Writefmtln("class %s(pulumi.CustomResource):", res.name)
w.Writefmtln("class %s(pulumi.CustomResource):", pyClassName(res.name))
if res.doc != "" {
g.emitDocComment(w, res.doc, " ")
}
Expand Down Expand Up @@ -680,6 +680,11 @@ func pyPack(s string) string {
return "pulumi_" + s
}

// pyClassName turns a raw name into one that is suitable as a Python class name.
func pyClassName(name string) string {
return ensurePythonKeywordSafe(name)
}

// pyName turns a variable or function name, normally using camelCase, to an underscore_case name.
func pyName(name string) string {
// This method is a state machine with four states:
Expand Down Expand Up @@ -801,14 +806,7 @@ func pyName(name string) string {

components = append(components, string(currentComponent))
result := strings.Join(components, "_")

// If the generated name clashes with a Python 2 or 3 keyword, add a trailing underscore per PEP 8:
// https://www.python.org/dev/peps/pep-0008/?#function-and-method-arguments
if _, isKeyword := pythonKeywords[result]; isKeyword {
result += "_"
}

return result
return ensurePythonKeywordSafe(result)
}

// pythonKeywords is a map of reserved keywords used by Python 2 and 3. We use this to avoid generating unspeakable
Expand Down Expand Up @@ -856,3 +854,12 @@ var pythonKeywords = map[string]bool{
"with": true,
"yield": true,
}

// ensurePythonKeywordSafe adds a trailing underscore if the generated name clashes with a Python 2 or 3 keyword, per
// PEP 8: https://www.python.org/dev/peps/pep-0008/?#function-and-method-arguments
func ensurePythonKeywordSafe(name string) string {
if _, isKeyword := pythonKeywords[name]; isKeyword {
return name + "_"
}
return name
}
7 changes: 7 additions & 0 deletions pkg/tfgen/generate_python_test.go
Expand Up @@ -42,3 +42,10 @@ func TestPyName(t *testing.T) {
})
}
}

// Tests that we properly transform some Python reserved keywords.
func TestPyKeywords(t *testing.T) {
assert.Equal(t, pyName("if"), "if_")
assert.Equal(t, pyName("lambda"), "lambda_")
assert.Equal(t, pyClassName("True"), "True_")
}

0 comments on commit 7627bd9

Please sign in to comment.