From b3b0f4b2a76fa29de8febdd74b6aa7025cad9d82 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Thu, 12 Nov 2026 20:35:12 +0000 Subject: [PATCH 01/11] [FEAT] Check Development parameter values in fit and read_json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DevelopmentBase and MethodBase gain a _validate_params hook that checks nothing by default. Development overrides it: average must be 'volume', 'simple', 'regression' or 'geometric', n_periods must be -1 or a positive integer, and sigma_interpolation must be 'log-linear' or 'mack'. A bad value raises a ValueError naming the parameter. fit calls the hook with the triangle, so lists are checked element by element and dicts and callables after they resolve against it. read_json calls it with no triangle when it loads an estimator or a pipeline step. Before this, average='bogus' loaded and then raised a KeyError in fit, n_periods=-5 or 0 fitted silently as -1, and sigma_interpolation='bogus' left sigma_ as NaN. Closes #1569. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/base.py | 30 +++++++++++ chainladder/development/development.py | 33 ++++++++++++ .../development/tests/test_development.py | 50 +++++++++++++++++++ chainladder/methods/base.py | 8 +++ chainladder/utils/utility_functions.py | 19 +++++-- 5 files changed, 135 insertions(+), 5 deletions(-) diff --git a/chainladder/development/base.py b/chainladder/development/base.py index 5ee8614a1..fd2a62357 100644 --- a/chainladder/development/base.py +++ b/chainladder/development/base.py @@ -22,6 +22,36 @@ class DevelopmentBase(BaseEstimator, TransformerMixin, EstimatorIO, Common): + def _validate_params(self, X: Triangle | None = None) -> None: + """ + Check the estimator's parameter values and raise a ``ValueError`` that + names the first parameter with a value the estimator cannot use. + + ``fit`` calls it with the triangle, so values given as a dict or a + callable are checked after they are resolved against it. + ``read_json`` calls it with no triangle and checks the other values. + Subclasses override it; the base class checks nothing. + """ + + def _assumption_values(self, value, X: Triangle | None, axis: int = 3) -> list: + """ + Return the individual values of an assumption that can be checked. + + A scalar gives itself and a list, tuple, set or array gives its + elements. A dict or callable gives the values it resolves to against + ``X`` along ``axis``, or nothing when ``X`` is ``None``. + """ + if isinstance(value, dict) or callable(value): + if X is None: + return [] + values = self._validate_assumption(X, value, axis=axis).flatten() + elif isinstance(value, (list, tuple, set, np.ndarray)): + values = np.asarray(list(value) if isinstance(value, set) else value) + values = values.flatten() + else: + return [value] + return [v.item() if isinstance(v, np.generic) else v for v in values] + def fit(self, X, y=None, sample_weight=None): average_ = self._validate_assumption(y, self.average, axis=3) self.average_ = average_.flatten() diff --git a/chainladder/development/development.py b/chainladder/development/development.py index f78a10ec2..5f8cd2eb8 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -356,6 +356,38 @@ def __init__( # Undeclared until fitted attributes - scikit-learn convention. self.average_: np.ndarray + def _validate_params(self, X: TriangleLike | None = None) -> None: + """ + Check ``average``, ``n_periods`` and ``sigma_interpolation``. + + ``average`` takes 'volume', 'simple', 'regression' or 'geometric', + ``n_periods`` takes -1 or a positive integer, and + ``sigma_interpolation`` takes 'log-linear' or 'mack'. A list, dict or + callable is checked value by value; see + ``DevelopmentBase._validate_params``. + """ + averages = ("volume", "simple", "regression", "geometric") + for value in self._assumption_values(self.average, X): + if not (isinstance(value, str) and value in averages): + raise ValueError( + "average must be 'volume', 'simple', 'regression' or " + f"'geometric', got {value!r}." + ) + for value in self._assumption_values(self.n_periods, X): + if ( + isinstance(value, (bool, np.bool_)) + or not isinstance(value, (int, np.integer)) + or not (value == -1 or value >= 1) + ): + raise ValueError( + f"n_periods must be -1 or a positive integer, got {value!r}." + ) + if self.sigma_interpolation not in ("log-linear", "mack"): + raise ValueError( + "sigma_interpolation must be 'log-linear' or 'mack', " + f"got {self.sigma_interpolation!r}." + ) + def fit(self, X: TriangleLike, y: None = None, sample_weight: None = None): """ Fit the model with X. @@ -375,6 +407,7 @@ def fit(self, X: TriangleLike, y: None = None, sample_weight: None = None): Returns the instance itself. """ + self._validate_params(X) # Triangle must be cumulative and in "development" mode. obj: TriangleLike = self._set_fit_groups(X).incr_to_cum().val_to_dev().copy() xp: ModuleType = obj.get_array_module() diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index 4e3086a1a..fc5f89d50 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1090,3 +1090,53 @@ def test_pct_reported_requires_ldf(raa): _ = raa.pct_reported_ with pytest.raises(AttributeError): _ = raa.pct_unreported_ + + +@pytest.mark.parametrize( + "params, name", + [ + ({"average": "bogus"}, "average"), + ({"average": 1.5}, "average"), + ({"average": ["volume"] * 8 + ["bogus"]}, "average"), + ({"average": lambda age: "bogus" if age == 12 else "volume"}, "average"), + ({"n_periods": -5}, "n_periods"), + ({"n_periods": 0}, "n_periods"), + ({"n_periods": 2.5}, "n_periods"), + ({"n_periods": True}, "n_periods"), + ({"sigma_interpolation": "bogus"}, "sigma_interpolation"), + ], +) +def test_fit_rejects_invalid_params(raa, params, name): + """ + Check that ``fit`` raises a ``ValueError`` naming a parameter whose value + it cannot use, instead of failing later or fitting silently (#1569). + """ + with pytest.raises(ValueError, match=name): + cl.Development(**params).fit(raa) + + +@pytest.mark.parametrize( + "params, name", + [({"average": "bogus"}, "average"), ({"n_periods": -5}, "n_periods")], +) +def test_read_json_rejects_invalid_params(params, name): + """Check that ``read_json`` checks the parameter values it loads (#1569).""" + import json + + json_str = json.dumps({"params": params, "__class__": "Development"}) + with pytest.raises(ValueError, match=name): + cl.read_json(json_str) + with pytest.raises(ValueError, match=name): + cl.read_json(json.dumps([{"name": "dev", **json.loads(json_str)}])) + + +def test_valid_params_pass_validation(raa): + """Check that lists, dicts and callables of valid values still fit.""" + cl.Development( + average=["volume", "simple"] * 4 + ["regression"], + n_periods={age: -1 if age < 60 else 3 for age in raa.development}, + sigma_interpolation="mack", + ).fit(raa) + cl.Development(average=lambda age: "geometric", n_periods=[-1] * 9).fit(raa) + dev = cl.Development(n_periods=4, average="simple") + assert cl.read_json(dev.to_json()).get_params() == dev.get_params() diff --git a/chainladder/methods/base.py b/chainladder/methods/base.py index 543d8c815..c43ad3c4f 100644 --- a/chainladder/methods/base.py +++ b/chainladder/methods/base.py @@ -20,6 +20,14 @@ class MethodBase(BaseEstimator, EstimatorIO, Common): _estimator_type = "chainladder" + def _validate_params(self, X: Triangle | None = None) -> None: + """ + Check the estimator's parameter values and raise a ``ValueError`` that + names the first parameter with a value the estimator cannot use. + + Subclasses override it; the base class checks nothing. + """ + def validate_X(self, X): # noqa: N802 obj = X.copy() if "ldf_" not in obj: diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index e581face7..dda7086e1 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -393,13 +393,11 @@ def read_json(json_str, array_backend=None): array_backend = options.ARRAY_BACKEND json_dict = json.loads(json_str) if type(json_dict) is list: - import chainladder as cl - return Pipeline( steps=[ ( item["name"], - cl.__dict__[item["__class__"]]().set_params(**item["params"]), + _estimator_from_json(item["__class__"], item["params"]), ) for item in json_dict ] @@ -444,9 +442,20 @@ def read_json(json_str, array_backend=None): else: return tri else: - import chainladder as cl + return _estimator_from_json(json_dict["__class__"], json_dict["params"]) + + +def _estimator_from_json(class_name, params): + """ + Build the chainladder estimator named ``class_name`` with ``params`` and + check the parameter values it can check without a triangle. + """ + import chainladder as cl - return cl.__dict__[json_dict["__class__"]]().set_params(**json_dict["params"]) + estimator = cl.__dict__[class_name]().set_params(**params) + if hasattr(estimator, "_validate_params"): + estimator._validate_params() + return estimator def _origin_periods(index, grain): From d420695131158e3ded61499947f9941c4d94723e Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Fri, 13 Nov 2026 18:02:10 +0000 Subject: [PATCH 02/11] [FIX] Call only chainladder's _validate_params in read_json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/tests/test_development.py | 11 +++++++++++ chainladder/utils/utility_functions.py | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index fc5f89d50..037c47862 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1140,3 +1140,14 @@ def test_valid_params_pass_validation(raa): cl.Development(average=lambda age: "geometric", n_periods=[-1] * 9).fit(raa) dev = cl.Development(n_periods=4, average="simple") assert cl.read_json(dev.to_json()).get_params() == dev.get_params() + + +@pytest.mark.parametrize("name", ["Trend", "BerquistSherman", "ParallelogramOLF"]) +def test_read_json_loads_estimators_without_the_hook(name): + """ + Check that ``read_json`` still loads estimators outside ``DevelopmentBase`` + and ``MethodBase``, whose ``_validate_params`` is sklearn's and needs a + ``_parameter_constraints`` attribute they do not have. + """ + estimator = getattr(cl, name)() + assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index dda7086e1..d4a1ccf7b 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -451,9 +451,14 @@ def _estimator_from_json(class_name, params): check the parameter values it can check without a triangle. """ import chainladder as cl + from chainladder.development.base import DevelopmentBase + from chainladder.methods.base import MethodBase estimator = cl.__dict__[class_name]().set_params(**params) - if hasattr(estimator, "_validate_params"): + # sklearn's BaseEstimator has a _validate_params of its own, which needs a + # _parameter_constraints attribute that chainladder estimators do not set, + # so only the chainladder hook is called. + if isinstance(estimator, (DevelopmentBase, MethodBase)): estimator._validate_params() return estimator From 1f99121f7b773bdfdabb9d6754d6886530eb0489 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Fri, 13 Nov 2026 18:02:20 +0000 Subject: [PATCH 03/11] [TST] Cover read_json with a dict assumption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/tests/test_development.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index 037c47862..d58c60f19 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1151,3 +1151,14 @@ def test_read_json_loads_estimators_without_the_hook(name): """ estimator = getattr(cl, name)() assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() + + +def test_read_json_leaves_dict_params_to_fit(): + """ + Check that ``read_json`` loads a dict assumption without a triangle to + resolve it against, leaving its values to be checked in ``fit``. + """ + dev = cl.Development(n_periods={12: 2, 24: -1}) + loaded = cl.read_json(dev.to_json()) + assert isinstance(loaded, cl.Development) + assert list(loaded.n_periods.values()) == [2, -1] From 28bcb045ee83945b82b508f5f3f1c89a457d767c Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 18:22:07 +0000 Subject: [PATCH 04/11] [FIX] Rename the parameter check hook to _check_param_values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scikit-learn's BaseEstimator defines _validate_params, so the chainladder hook takes a name scikit-learn does not use. read_json still calls the hook only on DevelopmentBase and MethodBase estimators, the classes that define it. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/base.py | 2 +- chainladder/development/development.py | 6 +++--- chainladder/development/tests/test_development.py | 3 +-- chainladder/methods/base.py | 2 +- chainladder/utils/utility_functions.py | 6 ++---- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/chainladder/development/base.py b/chainladder/development/base.py index fd2a62357..8d3b15dd3 100644 --- a/chainladder/development/base.py +++ b/chainladder/development/base.py @@ -22,7 +22,7 @@ class DevelopmentBase(BaseEstimator, TransformerMixin, EstimatorIO, Common): - def _validate_params(self, X: Triangle | None = None) -> None: + def _check_param_values(self, X: Triangle | None = None) -> None: """ Check the estimator's parameter values and raise a ``ValueError`` that names the first parameter with a value the estimator cannot use. diff --git a/chainladder/development/development.py b/chainladder/development/development.py index 5f8cd2eb8..06ec24a7e 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -356,7 +356,7 @@ def __init__( # Undeclared until fitted attributes - scikit-learn convention. self.average_: np.ndarray - def _validate_params(self, X: TriangleLike | None = None) -> None: + def _check_param_values(self, X: TriangleLike | None = None) -> None: """ Check ``average``, ``n_periods`` and ``sigma_interpolation``. @@ -364,7 +364,7 @@ def _validate_params(self, X: TriangleLike | None = None) -> None: ``n_periods`` takes -1 or a positive integer, and ``sigma_interpolation`` takes 'log-linear' or 'mack'. A list, dict or callable is checked value by value; see - ``DevelopmentBase._validate_params``. + ``DevelopmentBase._check_param_values``. """ averages = ("volume", "simple", "regression", "geometric") for value in self._assumption_values(self.average, X): @@ -407,7 +407,7 @@ def fit(self, X: TriangleLike, y: None = None, sample_weight: None = None): Returns the instance itself. """ - self._validate_params(X) + self._check_param_values(X) # Triangle must be cumulative and in "development" mode. obj: TriangleLike = self._set_fit_groups(X).incr_to_cum().val_to_dev().copy() xp: ModuleType = obj.get_array_module() diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index d58c60f19..b1fbf8a00 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1146,8 +1146,7 @@ def test_valid_params_pass_validation(raa): def test_read_json_loads_estimators_without_the_hook(name): """ Check that ``read_json`` still loads estimators outside ``DevelopmentBase`` - and ``MethodBase``, whose ``_validate_params`` is sklearn's and needs a - ``_parameter_constraints`` attribute they do not have. + and ``MethodBase``, which do not define ``_check_param_values``. """ estimator = getattr(cl, name)() assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() diff --git a/chainladder/methods/base.py b/chainladder/methods/base.py index c43ad3c4f..a1206086f 100644 --- a/chainladder/methods/base.py +++ b/chainladder/methods/base.py @@ -20,7 +20,7 @@ class MethodBase(BaseEstimator, EstimatorIO, Common): _estimator_type = "chainladder" - def _validate_params(self, X: Triangle | None = None) -> None: + def _check_param_values(self, X: Triangle | None = None) -> None: """ Check the estimator's parameter values and raise a ``ValueError`` that names the first parameter with a value the estimator cannot use. diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index d4a1ccf7b..ce770ee7c 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -455,11 +455,9 @@ def _estimator_from_json(class_name, params): from chainladder.methods.base import MethodBase estimator = cl.__dict__[class_name]().set_params(**params) - # sklearn's BaseEstimator has a _validate_params of its own, which needs a - # _parameter_constraints attribute that chainladder estimators do not set, - # so only the chainladder hook is called. + # Only DevelopmentBase and MethodBase estimators define the hook. if isinstance(estimator, (DevelopmentBase, MethodBase)): - estimator._validate_params() + estimator._check_param_values() return estimator From fa0d67aef9429cddd6b9a173d08fb16df8d47f83 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 18:22:29 +0000 Subject: [PATCH 05/11] [FIX] Keep n_periods=0 as an alias for -1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Development fitted n_periods=0 as -1 before the parameter check, and the check now accepts 0 again so that behavior is unchanged. Negative values other than -1 still raise. A test checks that n_periods=0 gives the same ldf_ and sigma_ as n_periods=-1 on raa. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/development.py | 8 +++++--- chainladder/development/tests/test_development.py | 9 ++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/chainladder/development/development.py b/chainladder/development/development.py index 06ec24a7e..2367e9be0 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -361,7 +361,8 @@ def _check_param_values(self, X: TriangleLike | None = None) -> None: Check ``average``, ``n_periods`` and ``sigma_interpolation``. ``average`` takes 'volume', 'simple', 'regression' or 'geometric', - ``n_periods`` takes -1 or a positive integer, and + ``n_periods`` takes -1 or a positive integer (0 is kept as an alias + for -1), and ``sigma_interpolation`` takes 'log-linear' or 'mack'. A list, dict or callable is checked value by value; see ``DevelopmentBase._check_param_values``. @@ -377,10 +378,11 @@ def _check_param_values(self, X: TriangleLike | None = None) -> None: if ( isinstance(value, (bool, np.bool_)) or not isinstance(value, (int, np.integer)) - or not (value == -1 or value >= 1) + or not (value == -1 or value >= 0) ): raise ValueError( - f"n_periods must be -1 or a positive integer, got {value!r}." + "n_periods must be -1 or a positive integer (0 is read as " + f"-1), got {value!r}." ) if self.sigma_interpolation not in ("log-linear", "mack"): raise ValueError( diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index b1fbf8a00..6ea91f973 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1100,7 +1100,6 @@ def test_pct_reported_requires_ldf(raa): ({"average": ["volume"] * 8 + ["bogus"]}, "average"), ({"average": lambda age: "bogus" if age == 12 else "volume"}, "average"), ({"n_periods": -5}, "n_periods"), - ({"n_periods": 0}, "n_periods"), ({"n_periods": 2.5}, "n_periods"), ({"n_periods": True}, "n_periods"), ({"sigma_interpolation": "bogus"}, "sigma_interpolation"), @@ -1130,6 +1129,14 @@ def test_read_json_rejects_invalid_params(params, name): cl.read_json(json.dumps([{"name": "dev", **json.loads(json_str)}])) +def test_n_periods_zero_fits_as_all_periods(raa): + """Check that ``n_periods=0`` still fits as ``n_periods=-1`` (#1606).""" + zero = cl.Development(n_periods=0).fit(raa) + all_periods = cl.Development(n_periods=-1).fit(raa) + assert np.allclose(zero.ldf_.values, all_periods.ldf_.values) + assert np.allclose(zero.sigma_.values, all_periods.sigma_.values) + + def test_valid_params_pass_validation(raa): """Check that lists, dicts and callables of valid values still fit.""" cl.Development( From 23d13b081aee2252bacc225fffdb128375b2d08e Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 18:22:35 +0000 Subject: [PATCH 06/11] [DOCS] Drop the numeric average option from Development's docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The docstring said a numeric average sets the exponent of the regression weights, but average=1.0, 1.5 and other numbers raise a KeyError on main, because the weight lookup in weighted_regression.py accepts only the string names. The docstring now lists only those names. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/development.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chainladder/development/development.py b/chainladder/development/development.py index 2367e9be0..fed339f9a 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -37,11 +37,9 @@ class Development(DevelopmentBase): Setting n_periods to 1 does not allow enough degrees of freedom to support calculation of all regression statistics; only LDFs would be calculated. - average: literal (or list of literals), or float, optional (default = 'volume') + average: literal (or list of literals), optional (default = 'volume') type of averaging to use for ldf average calculation. - Options include 'volume', 'simple', 'regression', and 'geometric'. If numeric values are supplied, - then (2-average) in the style of Zehnwirth & Barnett is used - for the exponent of the regression weights. + Options include 'volume', 'simple', 'regression', and 'geometric'. sigma_interpolation: string optional (default = 'log-linear') Options include 'log-linear' and 'mack' drop: tuple or list of tuples From c6cba5f582fc37235d22250f14b1e4718c758016 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 19:10:50 +0000 Subject: [PATCH 07/11] [FIX] Declare Development's allowed string values as Literal aliases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Average and SigmaInterpolation are module-level Literal aliases. Development.__init__ annotates average and sigma_interpolation with them, and _check_param_values reads the allowed values with typing.get_args, so the annotation, the check and the error message share one list. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/development.py | 29 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/chainladder/development/development.py b/chainladder/development/development.py index fed339f9a..e0c1eb3a3 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -16,6 +16,7 @@ Literal, # Self, # Make use of this once Python 3.10 is deprecated. TYPE_CHECKING, + get_args, ) if TYPE_CHECKING: @@ -24,6 +25,18 @@ from pandas import Series from types import ModuleType +# Allowed values of Development's string parameters. The annotations in +# Development.__init__ use them, and _check_param_values reads them with +# get_args. +Average = Literal["volume", "simple", "regression", "geometric"] +SigmaInterpolation = Literal["log-linear", "mack"] + + +def _one_of(values: tuple) -> str: + """Return ``values`` as "'a', 'b' or 'c'" for an error message.""" + quoted = [repr(v) for v in values] + return ", ".join(quoted[:-1]) + " or " + quoted[-1] + class Development(DevelopmentBase): """ @@ -326,8 +339,8 @@ class Development(DevelopmentBase): def __init__( self, n_periods: int = -1, - average: Literal["volume", "simple", "regression", "geometric"] = "volume", - sigma_interpolation: Literal["log-linear", "mack"] = "log-linear", + average: Average = "volume", + sigma_interpolation: SigmaInterpolation = "log-linear", drop: tuple | list[tuple] | None = None, drop_high: bool | int | list[bool] | list[int] | None = None, drop_low: bool | int | list[bool] | list[int] | None = None, @@ -365,13 +378,10 @@ def _check_param_values(self, X: TriangleLike | None = None) -> None: callable is checked value by value; see ``DevelopmentBase._check_param_values``. """ - averages = ("volume", "simple", "regression", "geometric") + averages = get_args(Average) for value in self._assumption_values(self.average, X): if not (isinstance(value, str) and value in averages): - raise ValueError( - "average must be 'volume', 'simple', 'regression' or " - f"'geometric', got {value!r}." - ) + raise ValueError(f"average must be {_one_of(averages)}, got {value!r}.") for value in self._assumption_values(self.n_periods, X): if ( isinstance(value, (bool, np.bool_)) @@ -382,9 +392,10 @@ def _check_param_values(self, X: TriangleLike | None = None) -> None: "n_periods must be -1 or a positive integer (0 is read as " f"-1), got {value!r}." ) - if self.sigma_interpolation not in ("log-linear", "mack"): + interpolations = get_args(SigmaInterpolation) + if self.sigma_interpolation not in interpolations: raise ValueError( - "sigma_interpolation must be 'log-linear' or 'mack', " + f"sigma_interpolation must be {_one_of(interpolations)}, " f"got {self.sigma_interpolation!r}." ) From 5d1fbcaa14149c7f95893bd1b1586e67e80ca114 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 19:12:26 +0000 Subject: [PATCH 08/11] [CHORE] Move _assumption_values to Common next to _validate_assumption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _assumption_values resolves an assumption the same way _validate_assumption does and is not specific to development estimators, so it moves from DevelopmentBase to Common. Behavior is unchanged. Developed with an AI assistant (Árbol, running Muse). --- chainladder/core/common.py | 19 +++++++++++++++++++ chainladder/development/base.py | 19 ------------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/chainladder/core/common.py b/chainladder/core/common.py index 8b73648b8..d84fad6f9 100644 --- a/chainladder/core/common.py +++ b/chainladder/core/common.py @@ -435,3 +435,22 @@ def _validate_assumption( if axis == 2: arr = arr[None, None, :, None] return arr + + def _assumption_values(self, value, X: TriangleLike | None, axis: int = 3) -> list: + """ + Return the individual values of an assumption that can be checked. + + A scalar gives itself and a list, tuple, set or array gives its + elements. A dict or callable gives the values it resolves to against + ``X`` along ``axis``, or nothing when ``X`` is ``None``. + """ + if isinstance(value, dict) or callable(value): + if X is None: + return [] + values = self._validate_assumption(X, value, axis=axis).flatten() + elif isinstance(value, (list, tuple, set, np.ndarray)): + values = np.asarray(list(value) if isinstance(value, set) else value) + values = values.flatten() + else: + return [value] + return [v.item() if isinstance(v, np.generic) else v for v in values] diff --git a/chainladder/development/base.py b/chainladder/development/base.py index 8d3b15dd3..fc404577b 100644 --- a/chainladder/development/base.py +++ b/chainladder/development/base.py @@ -33,25 +33,6 @@ def _check_param_values(self, X: Triangle | None = None) -> None: Subclasses override it; the base class checks nothing. """ - def _assumption_values(self, value, X: Triangle | None, axis: int = 3) -> list: - """ - Return the individual values of an assumption that can be checked. - - A scalar gives itself and a list, tuple, set or array gives its - elements. A dict or callable gives the values it resolves to against - ``X`` along ``axis``, or nothing when ``X`` is ``None``. - """ - if isinstance(value, dict) or callable(value): - if X is None: - return [] - values = self._validate_assumption(X, value, axis=axis).flatten() - elif isinstance(value, (list, tuple, set, np.ndarray)): - values = np.asarray(list(value) if isinstance(value, set) else value) - values = values.flatten() - else: - return [value] - return [v.item() if isinstance(v, np.generic) else v for v in values] - def fit(self, X, y=None, sample_weight=None): average_ = self._validate_assumption(y, self.average, axis=3) self.average_ = average_.flatten() From 27827166a371fe5802e95261582e894cd796eea7 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 19:12:51 +0000 Subject: [PATCH 09/11] [CHORE] Drop the empty parameter check hook from MethodBase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No method estimator checks its parameters yet, so MethodBase does not declare the hook. read_json now calls _check_param_values on any estimator that defines it; scikit-learn 1.7.2 defines no method of that name. Developed with an AI assistant (Árbol, running Muse). --- chainladder/methods/base.py | 8 -------- chainladder/utils/utility_functions.py | 6 ++---- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/chainladder/methods/base.py b/chainladder/methods/base.py index a1206086f..543d8c815 100644 --- a/chainladder/methods/base.py +++ b/chainladder/methods/base.py @@ -20,14 +20,6 @@ class MethodBase(BaseEstimator, EstimatorIO, Common): _estimator_type = "chainladder" - def _check_param_values(self, X: Triangle | None = None) -> None: - """ - Check the estimator's parameter values and raise a ``ValueError`` that - names the first parameter with a value the estimator cannot use. - - Subclasses override it; the base class checks nothing. - """ - def validate_X(self, X): # noqa: N802 obj = X.copy() if "ldf_" not in obj: diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index ce770ee7c..1e92d9b59 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -451,12 +451,10 @@ def _estimator_from_json(class_name, params): check the parameter values it can check without a triangle. """ import chainladder as cl - from chainladder.development.base import DevelopmentBase - from chainladder.methods.base import MethodBase estimator = cl.__dict__[class_name]().set_params(**params) - # Only DevelopmentBase and MethodBase estimators define the hook. - if isinstance(estimator, (DevelopmentBase, MethodBase)): + # Only estimators with parameter checks define the hook. + if hasattr(estimator, "_check_param_values"): estimator._check_param_values() return estimator From 8f917955f8f6aa87db642de98652d3526b417982 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 19:12:57 +0000 Subject: [PATCH 10/11] [CHORE] Keep the parameter check on Development only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Development.fit does not call DevelopmentBase.fit, so an empty hook on the base class added nothing. The hook now lives only on Development, and its docstring says how fit and read_json call it. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/base.py | 11 ----------- chainladder/development/development.py | 17 +++++++++-------- .../development/tests/test_development.py | 4 ++-- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/chainladder/development/base.py b/chainladder/development/base.py index fc404577b..5ee8614a1 100644 --- a/chainladder/development/base.py +++ b/chainladder/development/base.py @@ -22,17 +22,6 @@ class DevelopmentBase(BaseEstimator, TransformerMixin, EstimatorIO, Common): - def _check_param_values(self, X: Triangle | None = None) -> None: - """ - Check the estimator's parameter values and raise a ``ValueError`` that - names the first parameter with a value the estimator cannot use. - - ``fit`` calls it with the triangle, so values given as a dict or a - callable are checked after they are resolved against it. - ``read_json`` calls it with no triangle and checks the other values. - Subclasses override it; the base class checks nothing. - """ - def fit(self, X, y=None, sample_weight=None): average_ = self._validate_assumption(y, self.average, axis=3) self.average_ = average_.flatten() diff --git a/chainladder/development/development.py b/chainladder/development/development.py index e0c1eb3a3..e67e08575 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -369,14 +369,15 @@ def __init__( def _check_param_values(self, X: TriangleLike | None = None) -> None: """ - Check ``average``, ``n_periods`` and ``sigma_interpolation``. - - ``average`` takes 'volume', 'simple', 'regression' or 'geometric', - ``n_periods`` takes -1 or a positive integer (0 is kept as an alias - for -1), and - ``sigma_interpolation`` takes 'log-linear' or 'mack'. A list, dict or - callable is checked value by value; see - ``DevelopmentBase._check_param_values``. + Check ``average``, ``n_periods`` and ``sigma_interpolation`` and raise + a ``ValueError`` naming the first one with a value ``fit`` cannot use. + + ``average`` takes the values of ``Average``, ``n_periods`` takes -1 or + a positive integer (0 is kept as an alias for -1), and + ``sigma_interpolation`` takes the values of ``SigmaInterpolation``. A + list is checked value by value. ``fit`` calls the method with the + triangle, so a dict or callable is checked once resolved against it; + ``read_json`` calls it with no triangle and skips those. """ averages = get_args(Average) for value in self._assumption_values(self.average, X): diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index 6ea91f973..1fcc6318f 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1152,8 +1152,8 @@ def test_valid_params_pass_validation(raa): @pytest.mark.parametrize("name", ["Trend", "BerquistSherman", "ParallelogramOLF"]) def test_read_json_loads_estimators_without_the_hook(name): """ - Check that ``read_json`` still loads estimators outside ``DevelopmentBase`` - and ``MethodBase``, which do not define ``_check_param_values``. + Check that ``read_json`` still loads estimators that do not define + ``_check_param_values``. """ estimator = getattr(cl, name)() assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() From a9def7128e5c93011ca59b506b5f0d4da29b2097 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Sun, 15 Nov 2026 19:13:13 +0000 Subject: [PATCH 11/11] [TST] Start test_io.py for development estimator JSON tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The three read_json tests added for #1569 move from test_development.py to a new chainladder/development/tests/test_io.py, unchanged, so later I/O tests for development estimators have one place to go. Developed with an AI assistant (Árbol, running Muse). --- .../development/tests/test_development.py | 36 ---------------- chainladder/development/tests/test_io.py | 41 +++++++++++++++++++ 2 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 chainladder/development/tests/test_io.py diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index 1fcc6318f..c6e8b78b9 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1114,21 +1114,6 @@ def test_fit_rejects_invalid_params(raa, params, name): cl.Development(**params).fit(raa) -@pytest.mark.parametrize( - "params, name", - [({"average": "bogus"}, "average"), ({"n_periods": -5}, "n_periods")], -) -def test_read_json_rejects_invalid_params(params, name): - """Check that ``read_json`` checks the parameter values it loads (#1569).""" - import json - - json_str = json.dumps({"params": params, "__class__": "Development"}) - with pytest.raises(ValueError, match=name): - cl.read_json(json_str) - with pytest.raises(ValueError, match=name): - cl.read_json(json.dumps([{"name": "dev", **json.loads(json_str)}])) - - def test_n_periods_zero_fits_as_all_periods(raa): """Check that ``n_periods=0`` still fits as ``n_periods=-1`` (#1606).""" zero = cl.Development(n_periods=0).fit(raa) @@ -1147,24 +1132,3 @@ def test_valid_params_pass_validation(raa): cl.Development(average=lambda age: "geometric", n_periods=[-1] * 9).fit(raa) dev = cl.Development(n_periods=4, average="simple") assert cl.read_json(dev.to_json()).get_params() == dev.get_params() - - -@pytest.mark.parametrize("name", ["Trend", "BerquistSherman", "ParallelogramOLF"]) -def test_read_json_loads_estimators_without_the_hook(name): - """ - Check that ``read_json`` still loads estimators that do not define - ``_check_param_values``. - """ - estimator = getattr(cl, name)() - assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() - - -def test_read_json_leaves_dict_params_to_fit(): - """ - Check that ``read_json`` loads a dict assumption without a triangle to - resolve it against, leaving its values to be checked in ``fit``. - """ - dev = cl.Development(n_periods={12: 2, 24: -1}) - loaded = cl.read_json(dev.to_json()) - assert isinstance(loaded, cl.Development) - assert list(loaded.n_periods.values()) == [2, -1] diff --git a/chainladder/development/tests/test_io.py b/chainladder/development/tests/test_io.py new file mode 100644 index 000000000..bb20d8d0d --- /dev/null +++ b/chainladder/development/tests/test_io.py @@ -0,0 +1,41 @@ +"""Tests for saving and loading development estimators with JSON.""" + +import json + +import pytest + +import chainladder as cl + + +@pytest.mark.parametrize( + "params, name", + [({"average": "bogus"}, "average"), ({"n_periods": -5}, "n_periods")], +) +def test_read_json_rejects_invalid_params(params, name): + """Check that ``read_json`` checks the parameter values it loads (#1569).""" + json_str = json.dumps({"params": params, "__class__": "Development"}) + with pytest.raises(ValueError, match=name): + cl.read_json(json_str) + with pytest.raises(ValueError, match=name): + cl.read_json(json.dumps([{"name": "dev", **json.loads(json_str)}])) + + +@pytest.mark.parametrize("name", ["Trend", "BerquistSherman", "ParallelogramOLF"]) +def test_read_json_loads_estimators_without_the_hook(name): + """ + Check that ``read_json`` still loads estimators that do not define + ``_check_param_values``. + """ + estimator = getattr(cl, name)() + assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() + + +def test_read_json_leaves_dict_params_to_fit(): + """ + Check that ``read_json`` loads a dict assumption without a triangle to + resolve it against, leaving its values to be checked in ``fit``. + """ + dev = cl.Development(n_periods={12: 2, 24: -1}) + loaded = cl.read_json(dev.to_json()) + assert isinstance(loaded, cl.Development) + assert list(loaded.n_periods.values()) == [2, -1]