Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Summary
There's been some discussion how the "best" way to write if-clauses depending on a text parameter in combination with_api.check_in_list
, e.g.#24486 (comment)
Proposed fix
There are three primary ways in the code base:
- Explicit checking all arguments twice
_api.check_in_list(["foo","bar"],param=param)ifparam=="foo": ...elifparam=="bar": ...
- Skip explicit checking of the last argument
_api.check_in_list(["foo","bar"],param=param)ifparam=="foo": ...else:# "bar" ...
- Use
_api.check_in_list
for generating an error message once we know that no option is matching.
ifparam=="foo": ...elifparam=="bar": ...else:_api.check_in_list(["foo","bar"],param=param)
Personally, I'm in favor of 2. Primarily as it skips a redundant check and that it is not possible to get 100% coverage in case 1. Also, it is often the case that the_api.check_in_list
is done in another place in the code (like in the constructor), so option 3 may lead to a delayed error.
I know that@timhoffm prefers 1 as that is more likely to lead to errors if a case is missed in the if-part.
(In addition, this should really be called_api.check_in_tuple
and tuples being used as this will have some performance benefits when it comes to compiled code. Parts of the code use tuples already though...)