Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
[MRG + 1] Revive "don't set trailing _ attrs in __init__" PR #7464
I'm okay with this as a test and as fixes for tests (plus some additional
Out of curiosity, do you have a use case in mind or is it just a theoretical possibility for now?
That's the option which would have my vote. the descriptor option seems a little bit like overkill to me, especially if there is no precise use case that we can't think of where the attribute would be accessed or modified prior to the fit.
Although certainly possible, it doesn't seem that likely and there is an easy fix in case people do use it. Just not entirely convinced this edge case is worth bothering about the descriptor solution. Out of completeness though, how would the descriptor solution look like, something like this?
def __init__(...): ... self._params_ = None @property def param_(self): try: check_fitted(self, 'blessed_param_') except NotFittedError: # deprecation warning saying do not access 'param_' before fit. # That will raise an error in 0.21 return self._param_ @param.setter def setter(self, value): try: check_fitted(self, 'blessed_param_') except NotFittedError: # deprecation warning self._param_ = value
The setter would need some tweaking since we are setting the attribute prior to the fit sometimes in our code (maybe we can do the setting through the private
how about something like
class DeprecatedInitializer(object): def __init__(self, name, default=None): self.name = name self.default = default def __get__(self, obj): if not hasattr(obj, '_val_' + self.name): warnings.warn(...) return getattr(obj, '_val_' + self.name) def __set__(self, obj, val): setattr(obj, '_val_' + self.name, self.name) class MyEstimator(object): attr_ = DeprecatedInitializer('attr_', default=None)
Still not sure it's the best idea, but just so you can see what it might look like. Why does it need excluding from tests?
referenced this pull request
Oct 3, 2016
Besides the following two comments, this LGTM.
Otherwise LGTM. Please write a what's new entry.