Skip to content

Exclude dataclass ClassVar attributes from Python prompt schemas - #533

Open
Goran (goranai) wants to merge 1 commit into
microsoft:mainfrom
goranai:fix-dataclass-classvar-schema
Open

Goran (goranai) wants to merge 1 commit into
microsoft:mainfrom
goranai:fix-dataclass-classvar-schema

Conversation

@goranai

Copy link
Copy Markdown

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:

python -m pytest -q tests
npx pyright

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 --check passes.

Related Issues

Fixes #532.

Found and validated with AI assistance (GPT-6.1 via OpenAI Codex). No AI co-author trailer is used.

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
Copilot AI balanced review requested due to automatic review settings November 15, 2026 20:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 ClassVar annotations 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataclass ClassVar attributes appear as JSON instance fields in Python prompt schemas

2 participants