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

feat(hugr-py)!: Reexport commonly used classes from the package root #1393

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 17 additions & 5 deletions hugr-py/src/hugr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
representation.
"""

from .hugr import Hugr
from .node_port import Direction, InPort, Node, OutPort, Wire
from .ops import Op
from .tys import Kind, Type

__all__ = [
"Hugr",
"Node",
"OutPort",
"InPort",
"Direction",
"Op",
"Kind",
"Type",
"Wire",
]

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


def get_serialization_version() -> str:
"""Return the current version of the serialization schema."""
return "live"
2 changes: 2 additions & 0 deletions hugr-py/src/hugr/node_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Direction(Enum):

NodeIdx = int
PortOffset = int
Edge = tuple["OutPort", "InPort"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related? (Neither of these is re-exported)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all :P
It was part of the port of useful things from guppy, but I should probably remove it from here.

OrderEdge = tuple[NodeIdx, NodeIdx]


@dataclass(frozen=True, eq=True, order=True)
Expand Down
8 changes: 3 additions & 5 deletions hugr-py/src/hugr/serialization/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import pydantic as pd
from pydantic_extra_types.semantic_version import SemanticVersion

from hugr import get_serialization_version

from .ops import Value
from .serial_hugr import SerialHugr
from .serial_hugr import SerialHugr, serialization_version
from .tys import (
ConfiguredBaseModel,
ExtensionId,
Expand Down Expand Up @@ -76,7 +74,7 @@ class Extension(ConfiguredBaseModel):

@classmethod
def get_version(cls) -> str:
return get_serialization_version()
return serialization_version()


class Package(ConfiguredBaseModel):
Expand All @@ -85,4 +83,4 @@ class Package(ConfiguredBaseModel):

@classmethod
def get_version(cls) -> str:
return get_serialization_version()
return serialization_version()
14 changes: 10 additions & 4 deletions hugr-py/src/hugr/serialization/serial_hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from pydantic import ConfigDict, Field

import hugr
from hugr import get_serialization_version
from hugr.node_port import NodeIdx, PortOffset

from .ops import OpType
Expand All @@ -13,8 +11,14 @@
Port = tuple[NodeIdx, PortOffset | None]
Edge = tuple[Port, Port]


def serialization_version() -> str:
"""Return the current version of the serialization schema."""
return "live"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the best we can do? (Sorry - that sounds a bit rude! But I'd hope that it might be, oh, replaced with an actual version number by some part of the release process or something?? That would be fine, but if so I think it'd be good to have a comment here explaining that)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really happy with the unversioned versions, but it's the "no guarantees" value we are currently used. I guess we'll revisit it once hugr-model is implemented...



VersionField = Field(
default_factory=get_serialization_version,
default_factory=serialization_version,
title="Version",
description="Serialisation Schema Version",
frozen=True,
Expand All @@ -34,7 +38,9 @@ class SerialHugr(ConfiguredBaseModel):

def to_json(self) -> str:
"""Return a JSON representation of the Hugr."""
self.encoder = f"hugr-py v{hugr.__version__}"
from hugr import __version__ as hugr_version

self.encoder = f"hugr-py v{hugr_version}"
return self.model_dump_json()

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions hugr-py/tests/serialization/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from hugr import get_serialization_version
from hugr.serialization.serial_hugr import SerialHugr
from hugr.serialization.serial_hugr import SerialHugr, serialization_version


def test_empty():
h = SerialHugr(nodes=[], edges=[])
assert h.model_dump() == {
"version": get_serialization_version(),
"version": serialization_version(),
"nodes": [],
"edges": [],
"metadata": None,
Expand Down
7 changes: 3 additions & 4 deletions hugr-py/tests/serialization/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from semver import Version

from hugr import get_serialization_version
from hugr.serialization.extension import (
ExplicitBound,
Extension,
Expand All @@ -9,7 +8,7 @@
TypeDef,
TypeDefBound,
)
from hugr.serialization.serial_hugr import SerialHugr
from hugr.serialization.serial_hugr import SerialHugr, serialization_version
from hugr.serialization.tys import (
FunctionType,
PolyFuncType,
Expand Down Expand Up @@ -75,7 +74,7 @@


def test_extension():
assert get_serialization_version() == Extension.get_version()
assert serialization_version() == Extension.get_version()
param = TypeParam(root=TypeTypeParam(b=TypeBound.Copyable))

bound = TypeDefBound(root=ExplicitBound(bound=TypeBound.Copyable))
Expand Down Expand Up @@ -113,7 +112,7 @@ def test_extension():


def test_package():
assert get_serialization_version() == Package.get_version()
assert serialization_version() == Package.get_version()

ext = Extension(
version=Version(0, 1, 0),
Expand Down
Loading