Repository navigation
[patch] Defer Python annotations so a class can name itself or a later class - #158
Merged
Merged
Conversation
…r class Python before 3.14 evaluates an annotation when the def or class body runs, so a factory returning its own class, a parameter of the declaring type, a field holding one, or a parameter naming a class declared further down made the generated module raise NameError on import. The Python generator now writes `from __future__ import annotations` as the file's first statement. Fixes #102 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GQx1vbckMdZECPqnBTXWek
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #102
What was wrong
Python before 3.14 evaluates annotations eagerly. A generated class whose own name, or the name of a class declared later in the file, appeared in a return type, parameter or field annotation therefore made the module raise
NameErroron import. #64 fixed the same problem for base lists only.Change
PythonGeneratornow overridesWriteFileDirectivesand writesfrom __future__ import annotationsas the first statement of a generatedSourceFile. It goes after the header comment and before any import. A file with no members gets nothing.PythonBaseFromGenericType), because a base is an expression and the future import does not defer it.WriteFileDirectivesremark inLanguageGeneratorBase, which said C++ was the only override even though Go already overrides it too.Tests
New
PythonGeneratedSourceImportsTests.AnnotationsNamingUndeclaredClasses_Importgenerates a module containing:It then imports the module with a real interpreter and calls the factory.
Before the fix it failed under Python 3.11 with
NameError: name 'Point' is not defined. With the fix it passes.Full suite: 1055/1055 passed locally.
🤖 Generated with Claude Code
https://claude.ai/code/session_01GQx1vbckMdZECPqnBTXWek
Generated by Claude Code