Skip to content

Go and Python: a generic type declared in the same document and mentioned with type arguments elsewhere doesn't build ("Box is not a generic type" / "'Source' is not subscriptable") #386

Open

Description

@matt-edmondson

What's wrong

CLAUDE.md says that when Go writes a generic type down, a mention of the type carries no arguments, because a type declared without parameters takes none. GoGenerator.SpellTypeName drops the arguments only when the mentioned name equals writtenDownType, which is the type currently being written:

private string SpellTypeName(TypeReference type)
{
string unknown = TypeMappings[UnknownTypeName];
if (string.Equals(type.Name, "list", StringComparison.OrdinalIgnoreCase))
{
return $"[]{(type.TypeArguments.Count == 1 ? SpellType(type.TypeArguments[0]) : unknown)}";
}
if (string.Equals(type.Name, "dict", StringComparison.OrdinalIgnoreCase))
{
return type.TypeArguments.Count == 2
? $"map[{SpellType(type.TypeArguments[0])}]{SpellType(type.TypeArguments[1])}"
: $"map[{StringTypeName}]{unknown}";
}
if (writtenDownParameters.Contains(type.Name))
{
return unknown;
}
string name = TypeMappings.TryGetValue(type.Name, out string? mapped) ? mapped : type.Name;
return type.TypeArguments.Count == 0 || string.Equals(name, writtenDownType, StringComparison.Ordinal)
? name
: $"{name}[{string.Join(", ", type.TypeArguments.Select(SpellType))}]";
}

Every mention of that type from outside its own declaration keeps […]:

  • a field of another struct;
  • a parameter or return type of a free function;
  • the interface assertion var _ X[int] = (*T)(nil).

Python has the same gap in one place. class Source: is written with its type parameters as a comment, but PythonBaseFromGenericType still writes the base as Source[int]. A base list is evaluated when the class statement runs, so the module fails on import. Annotations are unaffected because from __future__ import annotations defers them.

Repro 1

Input: struct Box with TypeParameter("T") and field value: T; struct Shelf with field item: Box<int>; free function width(b: Box<int>) -> int.

type Box struct { value any }
type Shelf struct { item Box[int] }       // go build: invalid operation: Box[int] (Box is not a generic type)
func width(b Box[int]) int { return 1 }    // same error

Repro 2

Input: interface Source<T> with abstract next() -> T; class Counter with Interfaces = [Source<int>].

  • Go: var _ Source[int] = (*Counter)(nil) gives "Source is not a generic type".
  • Python: class Source: … class Counter(Source[int]): raises TypeError: type 'Source' is not subscriptable on import.

C#, C++ and Rust compile both repros. Both were confirmed by generating the output and running go build and python3 on it.

Suggested fix / acceptance criteria

  • Go: collect the names of every type in the generation root that is written down (has TypeParameters). Drop the type arguments from any mention of those names anywhere in the file, the interface assertion included, so the documented rule holds file-wide.
  • Python: when a base names a class in the same document that was written down, write the base without arguments.
  • Add both repros to GoGeneratedSourceCompilesTests and PythonGeneratedSourceImportsTests. The existing Python test injects its generic interfaces from outside the file, so it never covers a generic declared in the same document.

Related: #63, closed, fixed only the mention inside the type's own declaration.

Activity

  1. added
    bug
    on Oct 30, 2026
  2. matt-edmondson commented on Oct 30, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions