Skip to content

Python output raises NameError on import for any parameter or return typed long (emitted as the undefined name long) #109

Description

@matt-edmondson

What's wrong

PythonGenerator.PythonTypeName (Coder/Languages/PythonGenerator.cs ~line 695) maps only int, string, bool, float, double and void. Every other type name is written through unchanged. The C, C++, Go and Rust type maps all translate long (to long long, int64 and i64), and C# accepts long natively. Python has no long. Parameter and return annotations are evaluated when the def runs, and no from __future__ import annotations is emitted, so the generated module fails as soon as it is imported.

This is distinct from #102, which covers annotations naming a class that is declared later.

Repro

var f = new FunctionDeclaration("total") { ReturnType = "long" };
f.Parameters.Add(new Parameter("count", "long"));
f.Body.Add(new ReturnStatement(new VariableReference("count")));
var file = new SourceFile("m"); file.Members.Add(f);
Console.WriteLine(new PythonGenerator().Generate(file));
  • Actual: def total(count: long) -> long:. python3 -c "import m" fails with NameError: name 'long' is not defined.
  • Expected: def total(count: int) -> int:.

Suggested fix

Add "long" => "int" to PythonTypeName. It is worth checking the other numeric names the other generators map (e.g. short, byte, decimal) at the same time. Add a case to PythonGeneratedSourceImportsTests that imports a module using long.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions