Generate typed nested resource dataclasses - #600
Open
razor-x wants to merge 2 commits into
Open
Conversation
Nested resource objects now hydrate as typed mapping-compatible dataclasses and unknown nested API fields are stripped. Free-form record properties remain mappings. BREAKING CHANGE: Nested properties are typed objects rather than dict subclasses. dict(...) and isinstance(value, dict) no longer work for them; typoed attributes now raise AttributeError instead of returning and inserting an empty mapping; undocumented nested fields are stripped.
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.
Motivation
Dict[str, Any]orDeepAttrDict) which conflicted with runtime hydration and prevented attribute completion and nested docs.errors/warnings) were left as lists of bare dicts, forcing subscript access and losing attribute access.Description
codegen/lib/layouts/resources.ts, producing nested class contexts and per-property metadata.codegen/lib/python-type.tsand change templates (codegen/layouts/resource.hbsandcodegen/layouts/partials/resource-dataclass.hbs) to emit nested dataclasses before the parent and to hydrate objects and lists of objects usingfrom_dict.seam/utils/resource_mapping.pythat nested dataclasses inherit so they remain indexable and support.get(),in, iteration, and.keys()while becoming real typed dataclasses; leaverecordproperties asDict[str, Any].seam/py.typed, addmypyto dev dependencies andjust lint, regenerate theseam/resources/modules (many files updated), and add regression tests intest/nested_resource_test.pycovering nested hydration, list-of-object hydration, unknown key stripping, record passthrough, mapping compatibility, and union hydration foraction_attempt.Testing
npx tsx codegen/smith.ts,npm run lint, andnpm run typecheckwhich completed successfully.python -m black --check .,python -m mypy seam/resources --disable-error-code=arg-type --disable-error-code=import-not-found, andpython -m compileall -q seam, which succeeded for the modified resources.python -m pytest test/nested_resource_test.py, which failed to run end-to-end due to a missing runtime dependency (urllib3) in the execution environment (test harness), not due to the generated code itself.Device.propertiesis now typed asDeviceProperties,errors/warningsare typed lists of nested dataclasses,custom_metadataremainsDict[str, Any], and nested classes are documented and hydrated infrom_dict.Codex Task