-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for instance parameters #306
Conversation
3f55fcc
to
b20f41b
Compare
Another thing to decide, a parameter already has an |
That seems like a good approach, but it seems to me that user code could use params() on an instance to get a Parameter object from the class, then one of the cases you outlined above would result in a per-instance copy to be made of that Parameter, and then the saved Parameter object would now be stale. I guess the expectation is that such passing around of Parameter objects outside the class is not to be encouraged or supported anyway? I guess making the owner be public would let you detect if this has happened... |
Yes, that is a worry I share, I would indeed not encourage users to store a cache of parameters outside the instance and cannot envision any time I would have done this but then we can't prevent users from doing it either. This is also related to questions about the future API, do we expect to keep |
That all sounds reasonable to me. |
Does that means this isn't compatible with #317? I think there are valid uses of Parameters not bound to parameterized classes (and in that PR I use Note, there is a distinction between storing/copying/working with parameter instances that were never bound to a class and caching parameters that were bound to a parameterized class. I don't expect I would ever have need of the latter... |
Anyway, having stared at this a bit longer, I think the overall approach is sound. The tricky bit is figuring out the possible consequences of this API but I think the way the instance parameters are created and stored is reasonable. |
@jbednar That's what I thought. I just wanted to double check! |
092658c
to
b65a321
Compare
Okay I just wanted to summarize the outcome of our discussions today. The main starting point of our discussion was that the Therefore we decided we should replace with new API that is less ambiguous. In addition to the newly added Make foo.param an iterator over parameter nameslist(parameterized.param) This replaces code like this: list(parameterized.param.params().keys()) Add a
|
Also I apologize for some of the diffs, I can't work on param without getting annoyed by the inconsistencies in spacing, outdated comments etc. so I ended up updating things that I touched. |
That all sounds like a good plan. I agree that the .params method should be deprecated and then removed in 2.0, but it seems like we can do so in a compatible way that doesn't currently raise any warnings for old code. Basically, I don't think any old code will cause instance parameters to be created, so can we make .params() return class parameters only, but warn loudly if there are any instance parameters defined? That way old code keeps working fine (and quietly) with param 1.x, but when we move to 2.0 we remove .params entirely. |
That's exactly what we decided and what's implemented here. |
Ok, great. I misread your description as warning if there are instance parameters allowed, but it sounds like it's only warning if there are actually any created on this object, which is what I was after. I'm still sorting through the diffs... |
I ended up renaming the |
Oh, one more thing, we added |
Seems fine, though it seems simpler just to rename |
True, it's private API so it should be safe. |
For convenience we should also make the |
Any objection to simply calling that |
Sounds good. |
This is ready to review and merge from my perspective. I'd like to merge and cut a dev release today. |
I've also explicitly disallowed reusing Parameter instances on multiple classes, this would already have caused various issues but wasn't explicitly disallowed. |
Co-Authored-By: philippjfr <prudiger@anaconda.com>
How about |
It's already been renamed to that. |
That's precisely why I suggested |
Going to be very happy with the functionality offered by this PR. There is a potential pickling issue that may need to be addressed but once that is done and the tests pass, I am happy to see this merged. |
b0332f4
to
437cb55
Compare
This PR implements the notion of an instance parameter which can be accessed using a new API. All parameters can in theory become instance parameters (as long as the new
per_instance
slot is set to True). The main balance to strike here is that a) we do not want to pay the cost of copying parameters for all instances and b) we do not want users to have to add any special boilerplate to get instance parameters.The approach I've therefore taken is to only make a copy of a parameter when it is actually needed. This means that all existing param code will never pay the cost of making instance parameters, there's only two situations where the per instance copy is made:
instance.param.param_name
orinstance.param['param_name']
. This API will make it easy to change the attributes of instance parameters, but the oldobj.params()
/obj.param.params()
method is unaffected by this.Leaving the old API unaffected will ensure that old param code never pays the costs associated with instance parameters.
parameterized.param.__getitem__
andparameterized.param.__getattr__
accessors for parameters