Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/cargo/itertools-0.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q authored May 21, 2024
2 parents e9d5548 + 52fcb9d commit c2e1511
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 39 deletions.
4 changes: 3 additions & 1 deletion hugr-py/src/hugr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
representation.
"""

__version__ = "0.2.0a1"
# This is updated by our release-please workflow, triggered by this
# annotation: x-release-please-version
__version__ = "0.2.1"


def it_works() -> str:
Expand Down
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,
}
16 changes: 16 additions & 0 deletions hugr-py/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# from https://github.com/python-poetry/poetry/issues/144#issuecomment-877835259
import toml # type: ignore[import-untyped]
from pathlib import Path
import hugr


def test_versions_are_in_sync():
"""Checks if the pyproject.toml and package.__init__.py __version__ are in sync."""

path = Path(__file__).resolve().parents[1] / "pyproject.toml"
pyproject = toml.loads(open(str(path)).read())
pyproject_version = pyproject["tool"]["poetry"]["version"]

package_init_version = hugr.__version__

assert package_init_version == pyproject_version
15 changes: 13 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pytest = "^8.1.1"
pytest-cov = "^4.1.0"
mypy = "^1.9.0"
ruff = "^0.3.3"
toml = "^0.10.0"

[tool.poetry.group.hugr.dependencies]
hugr = { path = "hugr-py", develop = true }
Expand Down

0 comments on commit c2e1511

Please sign in to comment.