Fix self-referential shapes (CategoryShape)
Set field-only _spec before building the full spec with nested shapes. Self-references resolve because cls._spec already exists when the generator encounters shape._spec where shape is cls. Also: pass cls into get_type_hints localns so forward ref strings like list["CategoryShape"] resolve to the class being defined. 49 shapes tests, 0 skipped. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -105,13 +105,10 @@ class BookWithEditorShape(Shape[Book]):
|
||||
editor: FlatAuthorShape | None = None
|
||||
|
||||
|
||||
# CategoryShape is commented out — self-referential forward refs crash
|
||||
# get_type_hints() at __init_subclass__ time. Known gap.
|
||||
#
|
||||
# class CategoryShape(Shape[Category]):
|
||||
# id: int | None = None
|
||||
# name: str
|
||||
# children: list["CategoryShape"] = []
|
||||
class CategoryShape(Shape[Category]):
|
||||
id: int | None = None
|
||||
name: str
|
||||
children: list["CategoryShape"] = []
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -169,11 +166,9 @@ class TestShapeClassCreation(TestCase):
|
||||
)
|
||||
|
||||
def test_self_referential_shape(self):
|
||||
"""CategoryShape.children references itself.
|
||||
Currently crashes at class definition time — known gap."""
|
||||
self.skipTest(
|
||||
"Self-referential forward ref crashes get_type_hints() at __init_subclass__ time"
|
||||
)
|
||||
"""CategoryShape.children references itself."""
|
||||
self.assertIn("children", CategoryShape._nested)
|
||||
self.assertIs(CategoryShape._nested["children"], CategoryShape)
|
||||
|
||||
def test_multiple_shapes_same_model_independent(self):
|
||||
self.assertLess(len(FlatBookShape._field_names), len(BookDetailShape._field_names))
|
||||
|
||||
Reference in New Issue
Block a user