From d19a58497d57b22f048406dd9e277668bf45a0fe Mon Sep 17 00:00:00 2001 From: zacharyburnett Date: Thu, 18 Jul 2024 10:43:52 -0400 Subject: [PATCH 1/5] replace usages of ``copy_arrays`` with ``memmap`` for ``asdf>=3.1.0`` add change log entry modify arguments --- CHANGES.rst | 1 + .../converters/unit/tests/test_quantity.py | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f459e140..e038477c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,7 @@ - drop support for python 3.9. [#232] +- replace usages of ``copy_arrays`` with ``memmap`` [#230] 0.6.1 (2024-04-05) ------------------ diff --git a/asdf_astropy/converters/unit/tests/test_quantity.py b/asdf_astropy/converters/unit/tests/test_quantity.py index 2595131c..fd374163 100644 --- a/asdf_astropy/converters/unit/tests/test_quantity.py +++ b/asdf_astropy/converters/unit/tests/test_quantity.py @@ -1,3 +1,5 @@ +import importlib.metadata + import asdf import numpy as np import pytest @@ -7,6 +9,13 @@ from numpy.testing import assert_array_equal +def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict: + if tuple(int(part) for part in importlib.metadata.version("asdf").split(".")) >= (3, 1, 0): + return {"memmap": memmap} + else : + return {"copy_arrays": not memmap} + + def create_quantities(): return [ # Scalar: @@ -77,7 +86,7 @@ def test_read_array_value(): def test_memmap(tmp_path): """ - Test that memmap (copy_arrays=False) works with quantities. + Test that memmap (memmap=True) works with quantities. Unfortunately, this is not a simple `isinstance(obj, np.memmap)` Instead it requires a more complicated check. @@ -95,7 +104,7 @@ def test_memmap(tmp_path): af.write_to(file_path) # Update a value in the ASDF file - with asdf.open(file_path, mode="rw", copy_arrays=False) as af: + with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=True)) as af: assert (af.tree["quantity"] == quantity).all() assert af.tree["quantity"][-1, -1] != new_value @@ -105,7 +114,7 @@ def test_memmap(tmp_path): assert (af.tree["quantity"] != quantity).any() assert (af.tree["quantity"] == new_quantity).all() - with asdf.open(file_path, mode="rw", copy_arrays=False) as af: + with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=True)) as af: assert af.tree["quantity"][-1, -1] == new_value assert (af.tree["quantity"] != quantity).any() assert (af.tree["quantity"] == new_quantity).all() @@ -113,7 +122,7 @@ def test_memmap(tmp_path): def test_no_memmap(tmp_path): """ - Test that turning off memmap (copy_arrays=True) works as expected for quantities + Test that turning off memmap (memmap=False) works as expected for quantities """ file_path = tmp_path / "test.asdf" quantity = Quantity(np.arange(100, dtype=np.float64).reshape(5, 20), units.km) @@ -128,7 +137,7 @@ def test_no_memmap(tmp_path): af.write_to(file_path) # Update a value in the ASDF file - with asdf.open(file_path, mode="rw", copy_arrays=True) as af: + with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af: assert (af.tree["quantity"] == quantity).all() assert af.tree["quantity"][-1, -1] != new_value @@ -138,7 +147,7 @@ def test_no_memmap(tmp_path): assert (af.tree["quantity"] != quantity).any() assert (af.tree["quantity"] == new_quantity).all() - with asdf.open(file_path, mode="rw", copy_arrays=True) as af: + with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af: assert af.tree["quantity"][-1, -1] != new_value assert (af.tree["quantity"] != new_quantity).any() assert (af.tree["quantity"] == quantity).all() From d6fe6a12efab0560049e74589c18a961aec0fcbf Mon Sep 17 00:00:00 2001 From: zacharyburnett Date: Fri, 19 Jul 2024 15:48:19 -0400 Subject: [PATCH 2/5] use minversion --- asdf_astropy/converters/unit/tests/test_quantity.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/asdf_astropy/converters/unit/tests/test_quantity.py b/asdf_astropy/converters/unit/tests/test_quantity.py index fd374163..a81a8c9b 100644 --- a/asdf_astropy/converters/unit/tests/test_quantity.py +++ b/asdf_astropy/converters/unit/tests/test_quantity.py @@ -1,16 +1,15 @@ -import importlib.metadata - import asdf import numpy as np import pytest from asdf.testing import helpers from astropy import units from astropy.units import Quantity +from astropy.utils.introspection import minversion from numpy.testing import assert_array_equal def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict: - if tuple(int(part) for part in importlib.metadata.version("asdf").split(".")) >= (3, 1, 0): + if minversion("asdf", "3.1.0"): return {"memmap": memmap} else : return {"copy_arrays": not memmap} From 7c95233e46f86674f854916fedf5a3161a27544a Mon Sep 17 00:00:00 2001 From: Zach Burnett Date: Mon, 22 Jul 2024 08:50:23 -0400 Subject: [PATCH 3/5] apply review comments Co-authored-by: Brett Graham --- asdf_astropy/converters/unit/tests/test_quantity.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asdf_astropy/converters/unit/tests/test_quantity.py b/asdf_astropy/converters/unit/tests/test_quantity.py index a81a8c9b..23ea3093 100644 --- a/asdf_astropy/converters/unit/tests/test_quantity.py +++ b/asdf_astropy/converters/unit/tests/test_quantity.py @@ -11,7 +11,7 @@ def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict: if minversion("asdf", "3.1.0"): return {"memmap": memmap} - else : + else: return {"copy_arrays": not memmap} @@ -136,7 +136,7 @@ def test_no_memmap(tmp_path): af.write_to(file_path) # Update a value in the ASDF file - with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af: + with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=False)) as af: assert (af.tree["quantity"] == quantity).all() assert af.tree["quantity"][-1, -1] != new_value @@ -146,7 +146,7 @@ def test_no_memmap(tmp_path): assert (af.tree["quantity"] != quantity).any() assert (af.tree["quantity"] == new_quantity).all() - with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af: + with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=False)) as af: assert af.tree["quantity"][-1, -1] != new_value assert (af.tree["quantity"] != new_quantity).any() assert (af.tree["quantity"] == quantity).all() From 25271bce6f78c31f9d370752ba9849feec9b8dec Mon Sep 17 00:00:00 2001 From: zacharyburnett Date: Mon, 22 Jul 2024 09:46:42 -0400 Subject: [PATCH 4/5] ignore FBT --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 9a83b079..6f5461ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -94,6 +94,7 @@ extend-ignore = [ "FIX", # flake8-fixme # Individually ignored checks "SLF001", # private-member-access + "FBT001", # boolean positional arguments in function definition ] extend-exclude = ["docs/*"] From e686404394019fe4742d425fa5698db3feaca700 Mon Sep 17 00:00:00 2001 From: Brett Graham Date: Tue, 23 Jul 2024 12:02:01 -0400 Subject: [PATCH 5/5] Update asdf_astropy/converters/unit/tests/test_quantity.py --- asdf_astropy/converters/unit/tests/test_quantity.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/asdf_astropy/converters/unit/tests/test_quantity.py b/asdf_astropy/converters/unit/tests/test_quantity.py index 23ea3093..71c07ae2 100644 --- a/asdf_astropy/converters/unit/tests/test_quantity.py +++ b/asdf_astropy/converters/unit/tests/test_quantity.py @@ -11,8 +11,7 @@ def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict: if minversion("asdf", "3.1.0"): return {"memmap": memmap} - else: - return {"copy_arrays": not memmap} + return {"copy_arrays": not memmap} def create_quantities():