Skip to content

Python: an enum member whose value names a keyword-escaped sibling (Default = None) still writes the keyword, so the module fails on import #378

Closed

Description

@matt-edmondson

What's wrong

The #106 Python fix (45ef146) escapes a keyword-named enum member's declaration (None → None_). But PythonGenerator.GenerateEnumDeclaration (Coder/Languages/PythonGenerator.cs:490-493) writes each member's value text as given, and so does the (origin) + n that EnumMemberValues derives from it. A value that refers to an escaped sibling therefore still names the Python keyword.

Repro

var en = new EnumDeclaration("Access");
en.Members.Add(new EnumMember("None") { Value = "0" });
en.Members.Add(new EnumMember("Read") { Value = "1" });
en.Members.Add(new EnumMember("Default") { Value = "None" });
en.Members.Add(new EnumMember("Next"));
new PythonGenerator().Generate(new SourceFile("access") { Members = { en } });

Output:

class Access(IntEnum):
    None_ = 0
    Read = 1
    Default = None
    Next = (None) + 1

Running it with python3 fails on import: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. Without the Next member, the import succeeds but Default is silently bound to Python's None rather than the enum member.

Expected: Default = None_ and Next = (None_) + 1.

Why it matters

None = 0 followed by Default = None is a common C# flags pattern, and the #106 commit message treats a member named None as an ordinary name. The generated module cannot be imported at all.

Not covered by open work: PR #365 (for #364) adds the same sibling-value rewrite for C++ and Rust only, and #106 tracks escaping for the other languages' declarations.

Suggested fix

Run each value through the existing helper before writing it, as the C, Go, Rust and JavaScript generators already do:

string spelled = RenameSiblingMembers(enumDecl, value, s => ReservedWords.Contains(s) ? EscapeIdentifier(s) : null);

This covers explicit values and the derived (origin) + n, because both come from the string EnumMemberValues yields.

Acceptance criteria

  • The repro above generates Default = None_ and Next = (None_) + 1.
  • A test in ReservedWordIdentifierTests runs the generated module under python3 and asserts Access.Default == Access.None_ and Access.Next == 1.

Activity

  1. added
    bug
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions