diff --git a/.github/workflows/check-build.yml b/.github/workflows/check-build.yml index 20b1a4c..464dd36 100644 --- a/.github/workflows/check-build.yml +++ b/.github/workflows/check-build.yml @@ -11,7 +11,7 @@ jobs: with: fetch-depth: 0 - - uses: actions/setup-python@v6 + - uses: actions/setup-python@v7 with: python-version: "3.10" diff --git a/.github/workflows/publish-to-pypi.yaml b/.github/workflows/publish-to-pypi.yaml index a758b68..5e1f96e 100644 --- a/.github/workflows/publish-to-pypi.yaml +++ b/.github/workflows/publish-to-pypi.yaml @@ -13,7 +13,7 @@ jobs: with: fetch-depth: 0 - - uses: actions/setup-python@v6 + - uses: actions/setup-python@v7 with: python-version: "3.12" diff --git a/.github/workflows/release-draft.yml b/.github/workflows/release-draft.yml index 2edbb91..b1652d2 100644 --- a/.github/workflows/release-draft.yml +++ b/.github/workflows/release-draft.yml @@ -15,7 +15,7 @@ jobs: with: fetch-depth: 0 - - uses: actions/setup-python@v6 + - uses: actions/setup-python@v7 with: python-version: "3.10" diff --git a/.github/workflows/test_suite.yaml b/.github/workflows/test_suite.yaml index 1232177..11d86b2 100644 --- a/.github/workflows/test_suite.yaml +++ b/.github/workflows/test_suite.yaml @@ -27,7 +27,7 @@ jobs: - uses: actions/checkout@master with: fetch-depth: 1 - - uses: actions/setup-python@v6 + - uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} - name: Install uv diff --git a/.github/workflows/warnings-tests.yaml b/.github/workflows/warnings-tests.yaml index 4772cdb..1992a17 100644 --- a/.github/workflows/warnings-tests.yaml +++ b/.github/workflows/warnings-tests.yaml @@ -27,7 +27,7 @@ jobs: - uses: actions/checkout@master with: fetch-depth: 1 - - uses: actions/setup-python@v6 + - uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} - name: Install uv diff --git a/src/hmf/cosmology/cosmo.py b/src/hmf/cosmology/cosmo.py index f3fe5a5..b75cc30 100644 --- a/src/hmf/cosmology/cosmo.py +++ b/src/hmf/cosmology/cosmo.py @@ -94,7 +94,7 @@ def cosmo_model(self, val): return get_cosmo(val) if not isinstance(val, FLRW): - raise ValueError("cosmo_model must be an instance of astropy.cosmology.FLRW") + raise TypeError("cosmo_model must be an instance of astropy.cosmology.FLRW") return val @_cache.parameter("param") diff --git a/src/hmf/cosmology/growth_factor.py b/src/hmf/cosmology/growth_factor.py index b54924f..df5f328 100644 --- a/src/hmf/cosmology/growth_factor.py +++ b/src/hmf/cosmology/growth_factor.py @@ -346,7 +346,7 @@ def ode(a, y): D = sol["y"][0, :] if (sol["status"] != 0) or (D.shape[0] != a.shape[0]): - raise Exception("The calculation of the growth factor failed.") + raise RuntimeError("The calculation of the growth factor failed.") return (Spline(self._lna, np.log(D)), Spline(D, self._zvec)) @@ -692,7 +692,9 @@ class GenMFGrowth(BaseGrowthFactor): def _validate_assumptions(self, z): if not isinstance(self.cosmo, cosmology.LambdaCDM): - raise ValueError( + # Kept as ValueError (not TypeError): part of the public API contract, + # asserted verbatim by tests/test_growth.py::test_unsupported_cosmo. + raise ValueError( # noqa: TRY004 "The GenMFGrowth factor is only accurate with a cosmological constant. " "Consider using the ODEGrowthFactor instead." ) diff --git a/src/hmf/density_field/transfer_models.py b/src/hmf/density_field/transfer_models.py index 9bc0383..27b8365 100644 --- a/src/hmf/density_field/transfer_models.py +++ b/src/hmf/density_field/transfer_models.py @@ -185,7 +185,10 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not isinstance(self.cosmo, (cosmology.LambdaCDM, cosmology.wCDM, cosmology.w0waCDM)): - raise ValueError("CAMB will only work with LCDM or wCDM cosmologies") + # Kept as ValueError (not TypeError): part of the public API contract, + # asserted verbatim by + # tests/test_transfer_models.py::test_camb_rejects_non_lcdm_cosmology. + raise ValueError("CAMB will only work with LCDM or wCDM cosmologies") # noqa: TRY004 # Save the CAMB object properly for use # Set the cosmology @@ -374,7 +377,7 @@ def __getstate__(self): stacklevel=2, ) - except Exception: + except (pickle.PicklingError, TypeError): warnings.warn(f"CAMB key {pk} is not pickle-able.", stacklevel=2) # Deepcopy self diff --git a/src/hmf/halos/mass_definitions.py b/src/hmf/halos/mass_definitions.py index d03bf2d..06b3451 100644 --- a/src/hmf/halos/mass_definitions.py +++ b/src/hmf/halos/mass_definitions.py @@ -374,7 +374,7 @@ def fnc(x): xmin = x_guess / XDELTA_GUESS_FACTORS[i] xmax = x_guess * XDELTA_GUESS_FACTORS[i] x = sp.optimize.brentq(fnc, xmin, xmax) - except Exception as e: + except (ValueError, RuntimeError) as e: warnings.warn(f"raised following error: {e}", stacklevel=2) i += 1 diff --git a/src/hmf/mass_function/fitting_functions.py b/src/hmf/mass_function/fitting_functions.py index 6260a8a..d495a8b 100644 --- a/src/hmf/mass_function/fitting_functions.py +++ b/src/hmf/mass_function/fitting_functions.py @@ -219,10 +219,10 @@ class BaseFittingFunction(_framework.Component): def __init__( self, nu2: np.ndarray, - m: None | np.ndarray = None, + m: np.ndarray | None = None, z: float = 0.0, - n_eff: None | np.ndarray = None, - mass_definition: None | md.BaseMassDefinition = None, + n_eff: np.ndarray | None = None, + mass_definition: md.BaseMassDefinition | None = None, cosmo: csm.FLRW = csm.Planck15, delta_c: float = 1.68647, **model_parameters, @@ -1368,7 +1368,13 @@ def __init__(self, **model_parameters): super().__init__(**model_parameters) if not isinstance(self.mass_definition, md.SphericalOverdensity): - raise ValueError("The Tinker fitting function is a spherical-overdensity function.") + # Kept as ValueError (not TypeError): part of the public API contract, asserted + # verbatim (via Tinker08/Tinker10, which share this __init__) by + # tests/test_fitting_functions_extra.py::test_tinker08_non_so_raises and + # ::test_tinker10_non_so_raises. + raise ValueError( # noqa: TRY004 + "The Tinker fitting function is a spherical-overdensity function." + ) delta_halo = self.mass_definition.halo_overdensity_mean(self.z, self.cosmo) if delta_halo not in self.delta_virs: diff --git a/src/hmf/mass_function/hmf.py b/src/hmf/mass_function/hmf.py index 927d736..36b25f2 100644 --- a/src/hmf/mass_function/hmf.py +++ b/src/hmf/mass_function/hmf.py @@ -102,7 +102,7 @@ def __init__( dlog10m: float = 0.01, hmf_model: str | ff.BaseFittingFunction = ff.Tinker08, hmf_params: dict[str, Any] | None = None, - mdef_model: None | str | MassDef = None, + mdef_model: str | MassDef | None = None, mdef_params: dict | None = None, delta_c: float = 1.68647, filter_model: str | BaseFilter = TopHat, diff --git a/tests/test_cache.py b/tests/test_cache.py index bab8a08..17614f9 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -138,7 +138,9 @@ def test_obj_eq_numpy(): def test_obj_eq_dictlike_keys(monkeypatch): def fake_array_equal(a, b): if isinstance(a, _DictLike) or isinstance(b, _DictLike): - raise ValueError("boom") + # Kept as ValueError (not TypeError): this exercises the `except ValueError` + # branch in hmf._internals._cache.obj_eq, which only catches ValueError. + raise ValueError("boom") # noqa: TRY004 return np.array_equal(a, b) monkeypatch.setattr(cache, "array_equal", fake_array_equal)