Skip to content

Commit

Permalink
format + lints
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed May 1, 2024
1 parent 339b522 commit 50621b5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
13 changes: 8 additions & 5 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, cast

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

from . import tys
from .tys import (
Expand Down Expand Up @@ -549,7 +549,10 @@ class OpDef(BaseOp, populate_by_name=True):

# Now that all classes are defined, we need to update the ForwardRefs in all type
# annotations. We use some inspect magic to find all classes defined in this file.
classes = inspect.getmembers(
sys.modules[__name__],
lambda member: inspect.isclass(member) and member.__module__ == __name__,
) + tys_classes
classes = (
inspect.getmembers(
sys.modules[__name__],
lambda member: inspect.isclass(member) and member.__module__ == __name__,
)
+ tys_classes
)
1 change: 0 additions & 1 deletion hugr-py/src/hugr/serialization/serial_hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def get_version(cls) -> str:
def _pydantic_rebuild(cls, config: ConfigDict = ConfigDict(), **kwargs):
model_rebuild([(cls.__name__, cls)] + classes, config=config, **kwargs)


class Config:
title = "Hugr"
json_schema_extra = {
Expand Down
6 changes: 3 additions & 3 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import sys
from enum import Enum
from typing import Annotated, Any, Literal, Optional, Union, Tuple, Any
from typing import Annotated, Any, Literal, Optional, Union

from pydantic import (
BaseModel,
Expand Down Expand Up @@ -354,13 +354,13 @@ class Signature(ConfiguredBaseModel):


def model_rebuild(
classes: list[tuple[str,Any]],
classes: list[tuple[str, Any]],
config: ConfigDict = ConfigDict(),
**kwargs,
):
new_config = default_model_config.copy()
new_config.update(config)
for c in {k: v for (k,v) in classes}.values():
for c in {k: v for (k, v) in classes}.values():
if issubclass(c, ConfiguredBaseModel):
c.set_model_config(new_config)
c.model_rebuild(**kwargs)
Expand Down
13 changes: 8 additions & 5 deletions scripts/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
from hugr.serialization import SerialHugr
from hugr.serialization.testing_hugr import TestingHugr

from hugr.serialization import tys


def write_schema(
out_dir: Path, name_prefix: str, schema: Type[SerialHugr] | Type[TestingHugr], config: Optional[ConfigDict] = None, **kwargs
out_dir: Path,
name_prefix: str,
schema: Type[SerialHugr] | Type[TestingHugr],
config: Optional[ConfigDict] = None,
**kwargs,
):

version = schema.get_version()
filename = f"{name_prefix}_{version}.json"
path = out_dir / filename
Expand All @@ -45,7 +46,9 @@ def write_schema(

strict_config = ConfigDict(strict=True, extra="forbid")
lax_config = ConfigDict(strict=False, extra="allow")
write_schema(out_dir, "testing_hugr_schema_strict", TestingHugr, config=strict_config)
write_schema(
out_dir, "testing_hugr_schema_strict", TestingHugr, config=strict_config
)
write_schema(out_dir, "testing_hugr_schema", TestingHugr, config=lax_config)
write_schema(out_dir, "hugr_schema_strict", SerialHugr, config=strict_config)
write_schema(out_dir, "hugr_schema", SerialHugr, config=lax_config)

0 comments on commit 50621b5

Please sign in to comment.