Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 70 additions & 0 deletions python/tests/test_dataclass_classvars.py
Original file line number Diff line number Diff line change
@@ -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)