diff --git a/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py b/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py index 69a628a3..82e38e9b 100644 --- a/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py +++ b/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py @@ -464,6 +464,8 @@ def declare_type(py_type: object): for attr_name, field in cast(Dataclassish, py_type).__dataclass_fields__.items(): type_hint = annotated_members[attr_name] + if type_hint is ClassVar or get_origin(type_hint) is ClassVar: + continue optional = not(field.default is MISSING and field.default_factory is MISSING) prop = declare_property(attr_name, type_hint, is_typeddict_attribute=False, optionality_default=optional, fallback_comment=inline_comments.get(attr_name, "")) properties.append(prop) diff --git a/python/tests/test_dataclass_classvars.py b/python/tests/test_dataclass_classvars.py new file mode 100644 index 00000000..d9745a44 --- /dev/null +++ b/python/tests/test_dataclass_classvars.py @@ -0,0 +1,70 @@ +import asyncio +import re +from dataclasses import dataclass, fields +from typing import Any, ClassVar + +import pytest +from pydantic import TypeAdapter +from typing_extensions import override + +from typechat import ( + Failure, + PromptSection, + Result, + Success, + TypeChatJsonTranslator, + TypeChatLanguageModel, + TypeChatValidator, + python_type_to_typescript_schema, +) + + +@dataclass +class ClassVariables: + value: int + shared: ClassVar[str] + defaulted: ClassVar[int] = 1 + + +@dataclass +class InheritedVariables(ClassVariables): + extra: str = "fake" + + +@dataclass +class BareVariable: + value: int + shared: ClassVar = "fake class data" + + +class CapturingModel(TypeChatLanguageModel): + def __init__(self) -> None: + super().__init__() + self.prompt: str | list[PromptSection] = "" + + @override + async def complete(self, prompt: str | list[PromptSection]) -> Result[str]: + self.prompt = prompt + return Success('{"value": 1}') + + +@pytest.mark.parametrize("target", [ClassVariables, InheritedVariables, BareVariable]) +def test_class_variables_match_instance_fields(target: type[Any]) -> None: + schema = python_type_to_typescript_schema(target) + assert not schema.errors + emitted = set(re.findall(r"^ (\w+)\??:", schema.typescript_schema_str, re.MULTILINE)) + assert emitted == {item.name for item in fields(target)} + assert emitted == set(TypeAdapter(target).json_schema()["properties"]) + + +@pytest.mark.parametrize("target", [ClassVariables, InheritedVariables, BareVariable]) +def test_translator_uses_instance_schema(target: type[Any]) -> None: + validator = TypeChatValidator(target) + model = CapturingModel() + translator = TypeChatJsonTranslator(model, validator, target) + result = asyncio.run(translator.translate("Return value 1.")) + assert isinstance(result, Success) + assert result.value.value == 1 + assert "shared" not in str(model.prompt) + assert "defaulted" not in str(model.prompt) + assert isinstance(validator.validate_object({"value": "fake invalid integer"}), Failure)