From c4983fe9b23cefea25fab28f421b48f78e10b0c6 Mon Sep 17 00:00:00 2001 From: gaoflow Date: Mon, 1 Jun 2026 18:23:41 +0200 Subject: [PATCH] Make magnetization direction required in public reduction_to_pole PR #661 (fixing #440) made the magnetization inclination/declination required in the reduction_to_pole_kernel and removed the code that filled in defaults when they were None. However, the public reduction_to_pole function was missed: it still declared magnetization_inclination=None and magnetization_declination=None and passed those straight to the kernel. As a result, the documented default usage reduction_to_pole(grid, inclination, declination) (omitting the magnetization direction) crashed deep inside the kernel with a cryptic 'np.radians(None)' TypeError instead of the intended behaviour of requiring the user to be explicit about the magnetization direction. Complete #661 on the public function: make the magnetization arguments required and drop the stale 'If None ...' docstring text (mirroring the kernel). Omitting them now raises a clear 'missing required argument' error at call time. Add a regression test. Relevant issues/PRs: #440, #661 --- src/harmonica/_transformations.py | 16 +++++----------- test/test_transformations.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/harmonica/_transformations.py b/src/harmonica/_transformations.py index 0e16cb6c5..6387a0da0 100644 --- a/src/harmonica/_transformations.py +++ b/src/harmonica/_transformations.py @@ -389,8 +389,8 @@ def reduction_to_pole( grid, inclination, declination, - magnetization_inclination=None, - magnetization_declination=None, + magnetization_inclination, + magnetization_declination, *, pad=True, pad_kwargs=None, @@ -412,16 +412,10 @@ def reduction_to_pole( The inclination of the inducing Geomagnetic field. declination : float in degrees The declination of the inducing Geomagnetic field. - magnetization_inclination : float in degrees or None - The inclination of the total magnetization of the anomaly source. If - None, the ``magnetization_inclination`` will be set equal to the - ``inclination``, neglecting remanent magnetization and self - demagnetization. Default None. + magnetization_inclination : float in degrees + The inclination of the total magnetization of the anomaly source. magnetization_declination : float in degrees - The declination of the total magnetization of the anomaly source. If - None, the ``magnetization_declination`` will be set equal to the - ``declination``, neglecting remanent magnetization and self - demagnetization. Default None. + The declination of the total magnetization of the anomaly source. pad : bool, optional If True, will add padding to the grid before taking the Fourier Transform and applying the filter and remove it after the inverse Fourier Transform. diff --git a/test/test_transformations.py b/test/test_transformations.py index 19209f6f3..0d7954b4d 100644 --- a/test/test_transformations.py +++ b/test/test_transformations.py @@ -553,6 +553,21 @@ def test_reduction_to_pole_dim_names(sample_potential): reduction_to_pole(renamed_dims_grid, 60, 45, 60, 45) +def test_reduction_to_pole_magnetization_required(sample_potential): + """ + The magnetization direction must be a required argument. + + Making it required forces users to be explicit about the induced-only + magnetization assumption (see #440 and #661). It used to default to None on + the public ``reduction_to_pole`` function, which crashed deep inside the + kernel after #661 removed the None handling. + """ + with pytest.raises(TypeError, match="magnetization_inclination"): + reduction_to_pole(sample_potential, 60, 45) + with pytest.raises(TypeError, match="magnetization_declination"): + reduction_to_pole(sample_potential, 60, 45, 60) + + class TestTotalGradientAmplitude: """ Test total_gradient_amplitude function.