diff --git a/CHANGES.rst b/CHANGES.rst index fcc97f598..a57e6ebcd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,8 @@ The ASDF Standard is at v1.6.0 - Fix issue opening files that don't support ``fileno`` [#1557] - Allow Converters to defer conversion to other Converters by returning ``None`` in ``Converter.select_tag`` [#1561] +- Convert numpy scalars to python types during yaml encoding + to handle NEP51 changes for numpy 2.0 [#1605] 2.15.0 (2023-03-28) ------------------- diff --git a/asdf/_tests/test_yaml.py b/asdf/_tests/test_yaml.py index 06f84a3fc..1faa015ef 100644 --- a/asdf/_tests/test_yaml.py +++ b/asdf/_tests/test_yaml.py @@ -287,4 +287,10 @@ def test_numpy_scalar(numpy_value, expected_value): yamlutil.dump_tree(tree, buffer, ctx) buffer.seek(0) - assert yamlutil.load_tree(buffer)["value"] == expected_value + loaded_value = yamlutil.load_tree(buffer)["value"] + if isinstance(numpy_value, np.floating): + abs_diff = abs(expected_value - loaded_value) + eps = np.finfo(numpy_value.dtype).eps + assert abs_diff < eps, abs_diff + else: + assert loaded_value == expected_value diff --git a/asdf/yamlutil.py b/asdf/yamlutil.py index e853b93d6..8b2252dd6 100644 --- a/asdf/yamlutil.py +++ b/asdf/yamlutil.py @@ -117,11 +117,12 @@ def represent_ordereddict(dumper, data): # ---------------------------------------------------------------------- # Handle numpy scalars + for scalar_type in util.iter_subclasses(np.floating): - AsdfDumper.add_representer(scalar_type, AsdfDumper.represent_float) + AsdfDumper.add_representer(scalar_type, lambda dumper, data: dumper.represent_float(float(data))) for scalar_type in util.iter_subclasses(np.integer): - AsdfDumper.add_representer(scalar_type, AsdfDumper.represent_int) + AsdfDumper.add_representer(scalar_type, lambda dumper, data: dumper.represent_int(int(data))) def represent_numpy_str(dumper, data): diff --git a/requirements-dev.txt b/requirements-dev.txt index b3b1ad72f..34a857c96 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,8 +6,9 @@ git+https://github.com/asdf-format/asdf-unit-schemas.git git+https://github.com/asdf-format/asdf-wcs-schemas git+https://github.com/astropy/astropy git+https://github.com/spacetelescope/gwcs -git+https://github.com/yaml/pyyaml.git +#git+https://github.com/yaml/pyyaml.git # jsonschema 4.18 contains incompatible changes: https://github.com/asdf-format/asdf/issues/1485 #git+https://github.com/python-jsonschema/jsonschema numpy>=0.0.dev0 +scipy>=0.0.dev0