-
-
Notifications
You must be signed in to change notification settings - Fork 573
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
Improved option checking for tuples #4707
base: develop
Are you sure you want to change the base?
Conversation
I have implemented better option checking by creating an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for starting this!
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4707 +/- ##
========================================
Coverage 99.22% 99.22%
========================================
Files 303 303
Lines 23070 23103 +33
========================================
+ Hits 22891 22924 +33
Misses 179 179 ☔ View full report in Codecov by Sentry. |
_ensure_tuple(options["open-circuit potential"]) | ||
_ensure_tuple(options["particle"]) | ||
_ensure_tuple(options["intercalation kinetics"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ensure_tuple(options["open-circuit potential"]) | |
_ensure_tuple(options["particle"]) | |
_ensure_tuple(options["intercalation kinetics"]) | |
options["open-circuit potential"] = _ensure_tuple(options["open-circuit potential"]) | |
options["particle"] = _ensure_tuple(options["particle"]) | |
options["intercalation kinetics"] = _ensure_tuple(options["intercalation kinetics"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agriyakhetarpal I might be wrong but shouldn't we do these changes instead everywhere? With _ensure_tuple(options["open-circuit potential"])
we are not really changing anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If true we must then rename the helper function to something like _make_tuple
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! This is not editing things in-place.
@@ -653,6 +699,7 @@ def __init__(self, extra_options): | |||
|
|||
# Check options are valid | |||
for option, value in options.items(): | |||
_ensure_tuple(value) | |||
if isinstance(value, str) or option in [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we check for
if isinstance(value, tuple)
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me functionality-wise, thanks. I have a few more suggestions. I'll let @rtimms advise on any other changes, since the issue was originally his idea.
if isinstance(value, str): | ||
return (value,) | ||
elif isinstance(value, tuple): | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if-elif
constructs should always end with an else:
statement to catch unwarranted behaviour. But since we are the only users of this private method, it's probably fine here...
We could simplify this to something like (since we know the inputs and outputs and would never hit the None
case):
if isinstance(value, str): | |
return (value,) | |
elif isinstance(value, tuple): | |
return value | |
return (value,) if isinstance(value, str) else value if isinstance(value, tuple) else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would returning none be the best approach as , there can be cases where the values can be numeric or boolean, should those cases be handled as they come.
Like the part @prady0t pointed out
for option, value in options.items():
_ensure_tuple(value)
if isinstance(value, str) or option in [
"dimensionality",
"operating mode",
]: # some options accept non-strings
value = (value,)
Should i remove ensure_tuple
here and handle such cases seperately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think what happened was that @prady0t and I added in our reviews at the same time, and I didn't see his review (sorry!)
Yes, handling numeric or boolean options is needed – please feel free to ignore my suggestion in this case. Thanks!
_ensure_tuple(options["open-circuit potential"]) | ||
_ensure_tuple(options["particle"]) | ||
_ensure_tuple(options["intercalation kinetics"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! This is not editing things in-place.
return ( | ||
(value,) | ||
if isinstance(value, str) | ||
else value | ||
if isinstance(value, tuple) | ||
else None | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there cases where value
can be something other than str
or tuple
? If yes, then the type signature of this function is wrong. If not, None
should not be returned. (It is always preferred to not have the return value of a function depend on an if
condition, so removing the None
would be the ideal case.)
Also, I would really prefer a nested if-else
block here for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, just saw @agriyakhetarpal's comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type signature should still be fixed + it should be converted into an if-else
block.
Would it be wrong to assume that all options passed can be expressed in two ways: If a single value is passed for an option, it would look like this: options = {
"working electrode": "both",
} If multiple values are passed, it would be structured like this: options = {
"SEI": ("none", "constant"),
} Are these the only two possibilities? |
@medha-14 yes, those are the only two options. I think what I had in mind here is that all the options that can be tuples should be converted to tuples in-place at the start, then we can always just treat them like they will be tuples. You could add a new constant that is a list of all the options that can be tuples. |
Is it essential to convert the options to tuples beforehand? In my recent commit, I addressed the issue by updating: if options["particle size"] == "distribution" to this if "distribution" in options["particle size"] : Like in the commit i made |
Description
Fixes #3303
Type of change
Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.
Key checklist:
$ pre-commit run
(or$ nox -s pre-commit
) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)$ python -m pytest
(or$ nox -s tests
)$ python -m pytest --doctest-plus src
(or$ nox -s doctests
)You can run integration tests, unit tests, and doctests together at once, using
$ nox -s quick
.Further checks: