Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update workflow to run pydantic compatibility #217

Merged
merged 5 commits into from
Sep 6, 2023
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/_pydantic_compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ jobs:
echo "Found pydantic version ${CURRENT_VERSION}, as expected"
- name: Run pydantic compatibility tests
shell: bash
run: make test
run: poetry run poe test

19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ on:
push:
branches: [main]
pull_request:
workflow_dispatch: # Allows to trigger the workflow manually in GitHub UI

# If another push to the same PR or branch happens while this workflow is still running,
# cancel the earlier run in favor of the next run.
#
# There's no point in testing an outdated version of the code. GitHub only allows
# a limited number of job runners to be active at the same time, so it's better to cancel
# pointless jobs early so that more useful jobs can run sooner.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true


env:
POETRY_VERSION: "1.3.1"
Expand Down Expand Up @@ -31,3 +43,10 @@ jobs:
- name: Run unit tests
run: |
poetry run poe test

pydantic-compatibility:
uses:
./.github/workflows/_pydantic_compatibility.yml
with:
working-directory: .
secrets: inherit
13 changes: 7 additions & 6 deletions kor/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ def _translate_pydantic_to_kor(
if PYDANTIC_MAJOR_VERSION == 1:
field_info = field.field_info
extra = field_info.extra
field_examples = extra.get("examples", tuple())
field_description = field_info.description or ""
type_ = field.type_
field_examples = extra.get( # type: ignore[attr-defined]
"examples", tuple()
)
field_description = getattr(field_info, "description") or ""
type_ = field.outer_type_
else:
type_ = field.annotation
field_examples = field.examples or tuple()
field_description = field.description or ""
field_examples = field.examples or tuple() # type: ignore[attr-defined]
field_description = getattr(field, "description") or ""

field_many = _is_many(type_)
get_origin(type_)

attribute: Union[ExtractionSchemaNode, Selection, "Object"]

Expand Down
13 changes: 12 additions & 1 deletion kor/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ class ExtractionSchemaNode(AbstractSchemaNode, abc.ABC):

def __init__(self, **kwargs: Any) -> None:
"""Initialize."""
kwargs[TYPE_DISCRIMINATOR_FIELD] = type(self).__name__
super().__init__(**kwargs)
if PYDANTIC_MAJOR_VERSION == 1:
self.__dict__[TYPE_DISCRIMINATOR_FIELD] = type(self).__name__

@classmethod
def parse_obj(cls, data: dict) -> ExtractionSchemaNode:
"""Parse an object."""
if PYDANTIC_MAJOR_VERSION != 1:
raise NotImplementedError("Only supported for pydantic 1.x")
type_ = data.pop(TYPE_DISCRIMINATOR_FIELD, None)
if type_ is None:
raise ValueError(f"Need to specify type ({TYPE_DISCRIMINATOR_FIELD})")
Expand All @@ -146,6 +149,10 @@ def validate(cls: Type[ExtractionSchemaNode], v: Any) -> ExtractionSchemaNode:
class Number(ExtractionSchemaNode):
"""Built-in number input."""

examples: Sequence[
Tuple[str, Union[int, float, Sequence[Union[float, int]]]]
] = tuple()

def accept(self, visitor: AbstractVisitor[T], **kwargs: Any) -> T:
"""Accept a visitor."""
return visitor.visit_number(self, **kwargs)
Expand All @@ -154,6 +161,8 @@ def accept(self, visitor: AbstractVisitor[T], **kwargs: Any) -> T:
class Text(ExtractionSchemaNode):
"""Built-in text input."""

examples: Sequence[Tuple[str, Union[Sequence[str], str]]] = tuple()

def accept(self, visitor: AbstractVisitor[T], **kwargs: Any) -> T:
"""Accept a visitor."""
return visitor.visit_text(self, **kwargs)
Expand All @@ -162,6 +171,8 @@ def accept(self, visitor: AbstractVisitor[T], **kwargs: Any) -> T:
class Bool(ExtractionSchemaNode):
"""Built-in bool input."""

examples: Sequence[Tuple[str, Union[Sequence[bool], bool]]] = tuple()

def accept(self, visitor: AbstractVisitor[T], **kwargs: Any) -> T:
"""Accept a visitor."""
return visitor.visit_bool(self, **kwargs)
Expand Down
12 changes: 8 additions & 4 deletions kor/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ def clean_data(
Returns:
cleaned data instantiated as the corresponding pydantic model
"""
model_ = self.model_class # a proxy to make code fit in char limit

if self.many:
exceptions: List[Exception] = []
records: List[BaseModel] = []

for item in data:
try:
if PYDANTIC_MAJOR_VERSION == 1:
record = self.model_class.parse_obj(item)
record = model_.parse_obj(item) # type: ignore[attr-defined]
else:
record = self.model_class.model_validate(item)
record = model_.model_validate( # type: ignore[attr-defined]
item
)

records.append(record)
except ValidationError as e:
Expand All @@ -66,9 +70,9 @@ def clean_data(
else:
try:
if PYDANTIC_MAJOR_VERSION == 1:
record = self.model_class.parse_obj(data)
record = model_.parse_obj(data) # type: ignore[attr-defined]
else:
record = self.model_class.model_validate(data)
record = model_.model_validate(data) # type: ignore[attr-defined]
return record, []
except ValidationError as e:
return None, [e]
2 changes: 1 addition & 1 deletion tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Toy(pydantic.BaseModel):
attributes=[
Text(id="a", description="hello"),
Number(id="b", examples=[("b is 1", 1)]),
Number(id="c"),
Number(id="c", many=False),
Bool(id="d"),
# We don't have optional yet internally, so we don't check the
# optional setting.
Expand Down
8 changes: 4 additions & 4 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_example_generation() -> None:
"""
option = Option(id="option", description="Option", examples=["selection"])
number = Number(
id="number", description="Number", examples=[("number", "2")], many=True
id="number", description="Number", examples=[("number", 2)], many=True
)
text = Text(id="text", description="Text", examples=[("text", "3")], many=True)

Expand All @@ -24,15 +24,15 @@ def test_example_generation() -> None:
obj = Object(
id="object",
description="object",
examples=[("another number", {"number": "1"})],
examples=[("another number", {"number": 1})],
attributes=[number, text, selection],
many=True,
)

examples = generate_examples(obj)
assert examples == [
("another number", {"object": [{"number": "1"}]}),
("number", {"object": [{"number": ["2"]}]}),
("another number", {"object": [{"number": 1}]}),
("number", {"object": [{"number": [2]}]}),
("text", {"object": [{"text": ["3"]}]}),
("selection", {"object": [{"selection": ["option"]}]}),
("foo", {}),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_serialize_deserialize_equals() -> None:
id="root",
description="root-object",
attributes=[
Text(id="text", description="text description", examples=[]),
Number(id="number", description="Number description", examples=[]),
Text(id="text", description="text description", examples=[]),
Bool(id="bool", description="bool description", examples=[]),
],
examples=[],
Expand All @@ -50,21 +50,21 @@ def test_serialize_deserialize_equals() -> None:
"examples": [],
"id": "number",
"many": False,
"type_": "Number",
"$type": "Number",
},
{
"description": "text description",
"examples": [],
"id": "text",
"many": False,
"type_": "Text",
"$type": "Text",
},
{
"description": "bool description",
"examples": [],
"id": "bool",
"many": False,
"type_": "Bool",
"$type": "Bool",
},
],
"description": "root-object",
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import Extra # type: ignore[assignment]
else:
from pydantic.v1 import Extra # type: ignore[assignment]
from pydantic.v1 import Extra # type: ignore[assignment,no-redef]


class ToyChatModel(BaseChatModel):
Expand Down