From 685a83298a9e944e4ad99f9d0d570f67eefe5da1 Mon Sep 17 00:00:00 2001 From: Dieter Weber Date: Fri, 13 Sep 2024 20:02:41 +0200 Subject: [PATCH] Xfailing tests for complex Remove conversion to float --- src/libertem_schema/__init__.py | 2 +- tests/test_schemas.py | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/libertem_schema/__init__.py b/src/libertem_schema/__init__.py index 1707463..4fe976d 100644 --- a/src/libertem_schema/__init__.py +++ b/src/libertem_schema/__init__.py @@ -28,7 +28,7 @@ class DimensionError(ValueError): def to_tuple(q: pint.Quantity): base = q.to_base_units() - return (float(base.magnitude), str(base.units)) + return (base.magnitude, str(base.units)) def to_array_tuple(q: pint.Quantity, info: ValidationInfo, array_serializer: Callable): diff --git a/tests/test_schemas.py b/tests/test_schemas.py index fdc2ac8..22b4139 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -300,6 +300,74 @@ def test_json_schema_smoke(): assert tuple(loaded['overfocus']) == (0.0015, 'meter') +def test_json_schema_array(): + class T(BaseModel): + t: LengthArray[Shape['2 x, 2 y'], float] + + params = T(t=Quantity( + np.array([ + (1, 2), + (3, 4) + ]), + 'cm' + )) + json_schema = params.model_json_schema() + pprint.pprint(json_schema) + as_json = params.model_dump_json() + pprint.pprint(as_json) + loaded = json.loads(as_json) + jsonschema.validate( + instance=loaded, + schema=json_schema + ) + +@pytest.mark.xfail +def test_json_schema_complex_array(): + ''' + No native support for complex numbers in JSON + ''' + class T(BaseModel): + t: LengthArray[Shape['2 x, 2 y'], complex] + + params = T(t=Quantity( + np.array([ + (1, 2), + (3, 4) + ]), + 'cm' + )) + json_schema = params.model_json_schema() + pprint.pprint(json_schema) + as_json = params.model_dump_json() + pprint.pprint(as_json) + loaded = json.loads(as_json) + jsonschema.validate( + instance=loaded, + schema=json_schema + ) + + +@pytest.mark.xfail +def test_json_schema_complex(): + ''' + No native support for complex numbers in JSON + ''' + class T(BaseModel): + t: Length[complex] + + params = T(t=Quantity(1, 'cm')) + json_schema = params.model_json_schema() + pprint.pprint(json_schema) + as_json = params.model_dump_json() + pprint.pprint(as_json) + loaded = json.loads(as_json) + jsonschema.validate( + instance=loaded, + schema=json_schema + ) + + + def test_json_schema_repr(): params = Simple4DSTEMParams( overfocus=0.0015 * ureg.meter,