Repository navigation
Python: support typing.NewType in schema conversion - #458
David Billiot (WeAreResilience) wants to merge 7 commits into
Conversation
|
@microsoft-github-policy-service agree company="Resilience Agentic Solutions LLC" |
robgruen
left a comment
There was a problem hiding this comment.
Code looks good, thanks for the change.
Can we please add an offline translation test to verify and update the documentation indicating we now support NewType and how it's expected to work.
Add a FixedModel translator snapshot so NewType fields show as their underlying TypeScript types in the prompt schema, and document NewType support in the Python basic-usage guide.
|
Added in 996e22e: |
|
There was a chicken and egg problem with getting your PR to build so I created a def for NewIsh here (I didn't commit to your fork). Can you please resolve the build error and consolidate your test into test_newtype.py. Sorry for making extra work. |
|
Thanks for merging main and adding NewTypeish. I removed our duplicate NewTypeish so the one from main is used (the NewType branch now goes through is_new_type), and consolidated the NewType tests into python/tests/test_newtype.py. Pyright 1.1.358 passes on 3.11-3.14 and the full test suite passes locally. |
|
Thanks for merging main and adding NewTypeish. The offline translation test is in |
Fixes #455
Problem:
python_type_to_typescript_schemarejectstyping.NewTypeannotations ('...UserId' cannot be used as a type annotation.), so a translator can't be built for an otherwise valid JSON-shaped type, even though Pydantic validates it.Fix: In
convert_to_type_node, aNewType(typingortyping_extensions) is converted via its__supertype__. This also handles a NewType built on another NewType, and keeps the underlying type's constraint instead of falling back toany. A NewType over an unsupported type still reports a conversion error. Runtime validation is unchanged.Tests: Added
tests/test_newtype_1.pywith a schema snapshot coveringstr/intNewTypes, a NewType built on another, list, dict,| NoneandNotRequiredfields; validator tests (valid values accepted, wrong types rejected); and an unsupported-underlying-type error test. Addedtest_translator_with_newtype(FixedModel offline translation) so the prompt schema shows NewType fields as their underlying TypeScript types (user_id: string). Documented NewType support insite/src/docs/python/basic-usage.md.pytestpasses on 3.11–3.14; pyright reports no new errors.