Repository navigation
MAINT: Add _NoValue sentinel and initialize OLS conditional attributes (GH#9880) - #10024
ChinesePrince07 wants to merge 1 commit into
Conversation
Add statsmodels.tools._no_value._NoValue, a singleton sentinel modelled on numpy._NoValue, for marking instance attributes that have not been set. Unlike None it cannot collide with a legitimate attribute value, and it preserves identity across pickle round-trips so is-checks hold on loaded results. Use it in regression.linear_model: OLS now always initializes offset and the score/hessian intermediates (_wendog_xprod, _wexog_xprod, _wexog_x_wendog), replacing the hasattr probes in loglike, score, hessian and _setup_score_hess with is-_NoValue checks. Behavior is unchanged. First module of GH#9880.
|
Not sure about this. None is fine as long as it is the sentinel. The issue for _NoValue occurs when None indicates to do one thing, and a different value something else. Is there a difference between |
|
The point of that thread is that attributes often magically appear in classes, and so there is a lot of |
|
This fix asked for in that thread is to be model by model and to make sure that attributes are created during This is an important way forward as it will allow for typing. statsmodels in its current approach is impossible to type. |
| # Disallow reloading this module so as to preserve the identity of _NoValue. | ||
| if "_is_loaded" in globals(): | ||
| raise RuntimeError("Reloading statsmodels.tools._no_value is not allowed") | ||
| _is_loaded = True |
|
Going to close. We don't need a _NoValue. The intent of the issue referenced is to ensure that attributes are always initialized in init of classes,.possible with None. We can then check for None. If there is a case where None is a permitted value to indicate something, then a _NoValue or _NoDefukatValue can make sense. I'm still happy to have changes that will make sure attributes always exist in models so that hasattr and getattr are not needed. |
First slice of #9880, following the suggestion there to use a
_NoValuesentinel rather thanNone. I kept it to one module so the pattern can be reviewed before sweeping the rest of the codebase.Infrastructure:
statsmodels/tools/_no_value.pydefines a_NoValuesingleton modelled onnumpy._NoValue(numpy/_globals.py): singleton__new__, a reload guard, and__reduce__so identity survives pickle round-trips under every protocol. Results objects are pickled bysave/load, soattr is _NoValuemust still hold after loading.Application (
regression.linear_model): OLS now always initializes its conditionally-set attributes -offsetand the score/hessian intermediates_wendog_xprod,_wexog_xprod,_wexog_x_wendog- to_NoValue, and the fourhasattrprobes inloglike,score,hessianand_setup_score_hessbecomeis _NoValuechecks. Behavior is unchanged, includingOLS(y, x, offset=None), which still attachesNone(a passed value, distinguishable from an unset attribute - the distinction the sentinel exists for).I deliberately left the remaining
hasattr/getattrsites in the module, since a sentinel onselfcannot fix them:hasattr(model, "wexog_singular_values")inRegressionResults.__init__:modelis duck-typed - GLM, GAM and IV2SLS instances also pass through here, so this site needs those modules converted first.cov_typeguards:cov_typeis always set byRegressionResults.__init__, but subclasses in sandbox/emplike/quantile construct results in ways I did not want to audit in this PR; removing those guards is a separate cleanup.getattr(restricted, ...)incompare_f_test/compare_lr_test:restrictedis a caller-supplied results object.hasattr(self, "resid")inresid_pearson: probes whether a cached property can still be computed afterremove_data, not a conditionally-set attribute.hasattr(groups, ...): duck-typing of user input.Tests:
tools/tests/test_no_value.pycovers singleton identity, repr and pickle identity under every protocol. Two new tests inregression/tests/test_regression.pycover sentinel initialization, unchanged loglike/score/hessian with and without offset, the offset=None distinction, and a pickle round-trip.pytest statsmodels/regression/ statsmodels/tools/ statsmodels/emplike/passes locally: 1374 passed, 16 skipped. flake8 is clean on the touched files.If this shape looks right I will convert the remaining modules (discrete, base, tsa, genmod, ...) in follow-up PRs of similar size.