Repository navigation
Exclude dataclass ClassVar attributes from Python prompt schemas - #533
Open
Goran (goranai) wants to merge 1 commit into
Open
Goran (goranai) wants to merge 1 commit into
Goran (goranai) wants to merge 1 commit into
Conversation
python_type_to_typescript_schema emitted dataclass class variables as instance properties, although dataclasses.fields and the Pydantic validator exclude them, and a bare ClassVar stopped translator construction. Skip bare and parameterized ClassVar annotations in the dataclass property loop. Fixes microsoft#532
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change excluding dataclass ClassVar attributes from generated TypeScript prompt schemas.
No open issues 🎉
What changed in this PR
Fixes #532 by excluding dataclass ClassVar attributes from Python-generated TypeScript prompt schemas, aligning emitted fields with dataclass and Pydantic instance fields.
Changes:
- Skips bare and parameterized
ClassVarannotations during dataclass schema conversion. - Adds six regression cases covering typed, defaulted, inherited, and bare class variables, translator behavior, and invalid-input rejection.
| File | Description |
|---|---|
python/tests/test_dataclass_classvars.py |
Adds schema, translator, and validation regression tests. |
python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py |
Adds a guard to omit dataclass class variables from prompt schemas. |
Review effort: Balanced
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.
Summary
Exclude bare and parameterized dataclass ClassVar attributes from JSON prompt schemas. A typed class variable currently appears as an instance property; a bare ClassVar prevents normal translator construction.
Changes
Add a two-line guard in the dataclass property loop and six regression cases comparing the emitted fields with dataclasses.fields and Pydantic's actual JSON schema, then exercising the translator and invalid-input validation.
Typed, defaulted and inherited class variables are omitted from generated schemas. Bare ClassVar dataclasses can construct a translator. Ordinary instance fields and Pydantic validation remain unchanged. InitVar and other unsupported annotations are outside the fix. Seventy test lines cover both schema generation and real public translator behavior; the production repair is two lines.
Testing
From python, with the project dependencies and dev test dependencies installed:
Latest Windows/Python 3.14.7/Pydantic 2.14.3 result: 749 tests and 22 snapshots passed; Pyright 1.1.358 reports zero errors. All six new regression cases fail on main. Independent dataclasses/Pydantic references correct 500 ClassVar cases and preserve 500 ordinary cases, with zero new regressions.
Local source style/type limits: 31 Ruff diagnostics in already-failing code, with complexity/statement metrics increased by the guard; existing Black formatting failure; the same three baseline mypy errors. The new test file passes Ruff and Black. These checks and both baseline/fixed outputs are saved. No hosted CI or other Python/OS matrix result is claimed. Fresh-clone
git apply --checkpasses.Related Issues
Fixes #532.
Found and validated with AI assistance (GPT-6.1 via OpenAI Codex). No AI co-author trailer is used.