-
-
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
Adds support for required/ mandatory parameter values #724
base: main
Are you sure you want to change the base?
Conversation
As I don't know the inner workings of the There is no support for I would need some strong guidance to proceed. Thanks. |
There is a Long discussion about required for typing here https://mail.python.org/archives/list/[email protected]/thread/PYHMQEL7KKV7CJOPLY5BR6PTR4O5K7VU/ |
Thanks. Let's hold off on this until #605 is merged, which should make this go more smoothly. |
Considering the API only of this feature, there are two options available with a) passing a sentinel value to Overall I think I prefer b), the API looks cleaner to me eyes, it's easy to document, and I believe the implementation should be more straightforward (not having to fiddle with another sentinel value now #605 is merged and has added @jlstevens what's your opinion? |
I'll review this PR in more detail tomorrow... That said, my initial reaction is that while I don't object to |
I see your point. I think the point made by Marc on Discourse is that he wanted to avoid having to write boiler plate code. What this PR doesn't address is what it means to pass the default value, e.g. import param
class P(param.Parameterized):
x = param.Parameter(required=True) # default is None
p = P(x=None) # should this raise an error? Maybe you have an opinion on this @MarcSkovMadsen ? |
@jlstevens a potential advantage of having |
Good point, that is definitely a very common use case for this functionality. It really requires a secondary piece of information, since it implies some action you need to know what the parameter is required for. The most common case is a form submission. For that case I could see an |
I can live with having to write a custom init function for now. I believe it would improve Param if this was documented as the recommended approach (for now). I think that long term/ theoretically Params Parameters should have an api similar to Typing/ Dataclasses/ Pydantic such that eventually static type checking can be supported. If that enables supporting required parameters in forms it would be perfect. |
Just for reference there is a request here for Panel to support an api for forms here holoviz/panel#3687. |
I'm not sure I understand. Are you saying that you actually don't need the feature you started to implement in this PR?
Indeed, but that is out of scope of this PR and discussion I believe. To rephrase what I was asking you, do you believe that in the example below instantiating import param
class P(param.Parameterized):
x = param.Parameter(required=True) # default is None
p = P(x=None) # should this raise an error? |
The example should not raise an error because of required=True as you provide a parameter. But if allow_None=False it should of course raise an exception. |
Ok thanks for your feedback, this is the behavior implemented in this PR. |
Thanks, @marc for the implementation and @maxime for the adjustments to it. I'm happy for this PR to be merged as-is; looks good to me! For completeness, though, I think there is a third way to enforce required parameters not considered above, beyond (1) specifying a I'm not actually arguing for approach (3), because e.g. it wouldn't allow someone to accept |
In some ways I don't think something like this would be insane: class Foo(param.Parameterized):
foo = param.Number(default=param.RequiredValue(52.4)) The main reason I say this is that for all the years I have used param, I think this may be the first time where a new argument has been proposed to apply for all parameters. While I am not suggesting that I want to block this PR given that some people may find it useful, I know that (personally) I will continue to enforce mandatory arguments in the class constructor explicitly. I think I find this a lot easier than having to scan through all the parameters for the required flag, especially as parameters can be inherited from super classes imported from different spots in the code. |
We had a chat with Philipp, this feels like it needs a bit more discussion and is a pretty minor feature that doesn't necessarily have to be bound to Param 2.0, so it's going to be deferred for a little while. |
Fixes #1
The first commit is a Proof of concept to spark the design discussion. I've added a
required
argument as suggested by @philippjfr. I can see that @jbednar also has an interested in this issue.