Skip to content

Commit

Permalink
Xfailing tests for complex
Browse files Browse the repository at this point in the history
Remove conversion to float
  • Loading branch information
uellue committed Sep 13, 2024
1 parent 68974ab commit 685a832
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libertem_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
68 changes: 68 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 685a832

Please sign in to comment.