Skip to content
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

PluginRequirements: Fix optional and no default value behaviour #1142

Open
gcmoreira opened this issue May 6, 2024 · 3 comments
Open

PluginRequirements: Fix optional and no default value behaviour #1142

gcmoreira opened this issue May 6, 2024 · 3 comments

Comments

@gcmoreira
Copy link
Contributor

IMO the current implementation of an optional argument requirement with no default value is not working as expected.
For instance, if I use the following and no --output is used, I would expect the self.config doesn't contain the output key.

            requirements.StringRequirement(
                name="output",
                description="Output filename",
                optional=True,
            ),

So when I do this:

output_name = self.config.get("output", default_output_name)

In HierarchicalDict::getitem() it will raise a KeyError exception and the getter will assign the default value.

However, that is not working. The keys are created anyway, regardless of the optional=True and the absence of a default value.
That means, in this case, calling self.config.get("output", "foo") from a plugin still returns None.

@gcmoreira
Copy link
Contributor Author

One possible fix for this is only to set the default value if optional is False here. Thoughts?

@ikelos
Copy link
Member

ikelos commented May 8, 2024

I'm not keen on changing this behaviour, because there may be existing plugins that expect all keys to always exist, which will then throw an exception when they don't. The configuration value is set by the plugin, so moving the default value from a self.config.get to a default = seems like a straight forward work around?

It's basically two ways of doing the same thing. The current established way of doing this is that if a configuration option is defined, you can guarantee the key will be present. It should default to None, meaning that self.config.get('output') or foo should have a similar effect. Unless there's a very compelling reason why this way of doing things is wrong rather than just different, I don't want to introduce a change to our existing system.

@ikelos
Copy link
Member

ikelos commented May 8, 2024

Also, please see the discussion on #324 and the solution #354 which may be relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants