I am working on a codebase where many functions expected either an integer or a list of integers as the input. We'd like to support both if possible.
This is leading to a lot of duplicated code, and an occasional bug when that code is missing.
def get_usernames(userIds:list[int])->pd.DataFrame:
if isinstance(userIds, int): userIds=[userIds]
...
def get_settings(userIds:list[int])->pd.DataFrame:
if isinstance(userIds, int): userIds=[userIds]
...
def get_devices(userIds:list[int])->pd.DataFrame:
...
In the example above, I could get the usernames by running get_username(38)
or get_username([38,39,40])
. But running get_devices(38)
results in an error later in the code because it's not being cast into a list.
Is there any Python feature that can make this code more DRY?