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

fix(py): get rid of pydantic config deprecation warnings #1084

Merged
merged 1 commit into from
May 21, 2024
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
36 changes: 19 additions & 17 deletions hugr-py/src/hugr/serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC
from typing import Any, Literal

from pydantic import Field, RootModel
from pydantic import Field, RootModel, ConfigDict

from . import tys
from .tys import (
Expand Down Expand Up @@ -108,15 +108,14 @@ class SumValue(ConfiguredBaseModel):
tag: int
typ: SumType
vs: list["Value"]

class Config:
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {
model_config = ConfigDict(
json_schema_extra={
"description": (
"A Sum variant For any Sum type where this value meets the type "
"of the variant indicated by the tag."
),
}
)


class Value(RootModel):
Expand All @@ -126,8 +125,7 @@ class Value(RootModel):
discriminator="v"
)

class Config:
json_schema_extra = {"required": ["v"]}
model_config = ConfigDict(json_schema_extra={"required": ["v"]})


class Const(BaseOp):
Expand Down Expand Up @@ -168,11 +166,13 @@ def insert_child_dfg_signature(self, inputs: TypeRow, outputs: TypeRow) -> None:
self.sum_rows.append(variant)
self.other_outputs = outputs[1:]

class Config:
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {

model_config = ConfigDict(
json_schema_extra={
"description": "A CFG basic block node. The signature is that of the internal Dataflow graph.",
}
)


class ExitBlock(BaseOp):
Expand All @@ -182,11 +182,12 @@ class ExitBlock(BaseOp):
op: Literal["ExitBlock"] = "ExitBlock"
cfg_outputs: TypeRow

class Config:
json_schema_extra = {
model_config = ConfigDict(
json_schema_extra={
# Needed to avoid random '\n's in the pydantic description
"description": "The single exit node of the CFG, has no children, stores the types of the CFG node output.",
}
)


# ---------------------------------------------
Expand Down Expand Up @@ -234,16 +235,17 @@ class Call(DataflowOp):
type_args: list[tys.TypeArg]
instantiation: FunctionType

class Config:
model_config = ConfigDict(
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {
json_schema_extra={
"description": (
"Operation to call a function directly. The first port is "
"connected to the def/declare of the function being called directly, "
"with a `Static<FunctionType>` edge. The signature of the remaining "
"ports matches the function being called."
)
}
)


class CallIndirect(DataflowOp):
Expand Down Expand Up @@ -386,14 +388,15 @@ def insert_port_types(self, in_types: TypeRow, out_types: TypeRow) -> None:
def display_name(self) -> str:
return self.op_name

class Config:
model_config = ConfigDict(
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {
json_schema_extra={
"description": (
"A user-defined operation that can be downcasted by the extensions that "
"define it."
)
}
)


class Noop(DataflowOp):
Expand Down Expand Up @@ -491,8 +494,7 @@ class OpType(RootModel):
| AliasDefn
) = Field(discriminator="op")

class Config:
json_schema_extra = {"required": ["parent", "op"]}
model_config = ConfigDict(json_schema_extra={"required": ["parent", "op"]})


# --------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions hugr-py/src/hugr/serialization/serial_hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def _pydantic_rebuild(cls, config: ConfigDict = ConfigDict(), **kwargs):
my_classes[cls.__name__] = cls
model_rebuild(my_classes, config=config, **kwargs)

class Config:
title = "Hugr"
json_schema_extra = {
model_config = ConfigDict(
title="Hugr",
json_schema_extra={
"required": ["version", "nodes", "edges"],
}
},
)
22 changes: 10 additions & 12 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ class TypeParam(RootModel):
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="tp")

class Config:
json_schema_extra = {"required": ["tp"]}
model_config = ConfigDict(json_schema_extra={"required": ["tp"]})


# ------------------------------------------
Expand Down Expand Up @@ -153,8 +152,7 @@ class TypeArg(RootModel):
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="tya")

class Config:
json_schema_extra = {"required": ["tya"]}
model_config = ConfigDict(json_schema_extra={"required": ["tya"]})


# --------------------------------------------
Expand Down Expand Up @@ -197,8 +195,7 @@ class SumType(RootModel):
def t(self) -> str:
return self.root.t

class Config:
json_schema_extra = {"required": ["s"]}
model_config = ConfigDict(json_schema_extra={"required": ["s"]})


# ----------------------------------------------
Expand Down Expand Up @@ -235,14 +232,15 @@ class FunctionType(ConfiguredBaseModel):
def empty(cls) -> "FunctionType":
return FunctionType(input=[], output=[], extension_reqs=[])

class Config:
model_config = ConfigDict(
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {
json_schema_extra={
"description": (
"A graph encoded as a value. It contains a concrete signature and "
"a set of required resources."
)
}
)


class PolyFuncType(ConfiguredBaseModel):
Expand All @@ -261,14 +259,15 @@ class PolyFuncType(ConfiguredBaseModel):
def empty(cls) -> "PolyFuncType":
return PolyFuncType(params=[], body=FunctionType.empty())

class Config:
model_config = ConfigDict(
# Needed to avoid random '\n's in the pydantic description
json_schema_extra = {
json_schema_extra={
"description": (
"A polymorphic type scheme, i.e. of a FuncDecl, FuncDefn or OpDef. "
"(Nodes/operations in the Hugr are not polymorphic.)"
)
}
)


class TypeBound(Enum):
Expand Down Expand Up @@ -326,8 +325,7 @@ class Type(RootModel):
Field(discriminator="t"),
]

class Config:
json_schema_extra = {"required": ["t"]}
model_config = ConfigDict(json_schema_extra={"required": ["t"]})


# -------------------------------------------
Expand Down
15 changes: 12 additions & 3 deletions hugr-py/tests/serialization/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
def test_it_works():
"""TODO: Replace this with a real test."""
assert 2 + 2 != "🐟"
from hugr.serialization import SerialHugr


def test_empty():
h = SerialHugr(nodes=[], edges=[])
assert h.model_dump() == {
"version": "v1",
"nodes": [],
"edges": [],
"metadata": None,
"encoder": None,
}