Skip to content

Commit

Permalink
fix: Ensure json validation, fixing comment validation
Browse files Browse the repository at this point in the history
Common problem with standard types subclasses. Not really sure why, did
not investigate that deep
  • Loading branch information
alecandido committed Feb 7, 2025
1 parent 42114f7 commit 245ab6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/qibolab/_core/instruments/qblox/q1asm/ast_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
AfterValidator,
BeforeValidator,
ConfigDict,
GetCoreSchemaHandler,
computed_field,
field_validator,
model_serializer,
model_validator,
)
from pydantic_core import core_schema

from ....serialize import Model

Expand Down Expand Up @@ -701,6 +703,12 @@ def _format_comment(text: str, width: Optional[int] = None) -> str:


class Comment(str):
@classmethod
def __get_pydantic_core_schema__(
cls, source: type[Any], handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.no_info_after_validator_function(cls, handler(str))

def asm(self, width: Optional[int] = None) -> str:
return _format_comment(self, width) + "\n"

Expand Down
5 changes: 5 additions & 0 deletions tests/qblox/test_q1asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def test_roundtrip(asm):
def test_serialization(asm):
prog = parse(asm)
assert prog == Program.model_validate(prog.model_dump())


def test_json_serialization(asm):
prog = parse(asm)
assert prog == Program.model_validate_json(prog.model_dump_json())

0 comments on commit 245ab6a

Please sign in to comment.