Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MNT]: Where to use _api.check_in_list #24828

Open
@oscargus

Description

@oscargus

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:

  1. Explicit checking all arguments twice
_api.check_in_list(["foo", "bar"], param=param)
if param == "foo":
     ...
elif param == "bar":
    ...
  1. Skip explicit checking of the last argument
_api.check_in_list(["foo", "bar"], param=param)
if param == "foo":
     ...
else: # "bar"
    ...
  1. Use _api.check_in_list for generating an error message once we know that no option is matching.
if param == "foo":
     ...
elif param == "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...)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions