0

I have a case where I would like users of our package to be able to select from a list of options, for example

run_package(some_settings=("setting_1", "session_2", "setting_3"))

I have used a Tuple as the default argument on this function

def run_package(some_settings=("setting_1",):
    ...

To avoid the issue with having a mutable list as default argument. I prefer this form to the alternative:

def run_package(some_setting=None):

    if some_setting is None:
          # set default
          some_setting = ["setting_1"]

as it is more explicit.

However, now I want to type this argument, e.g.

def run_package(some-settings: Tuple[Union[Literal["setting_1"], Literal["setting_2"], Literal["setting_3"]] = ("setting_1",)):
    ...

I am running into problems because, if I understand correctly, Tuple type must be of fixed length as discussed here.

This makes sense as Tuple is immutable. I guess this precludes its use as a default argument for a function as I have done? Are there any workarounds except for using None as default?

Many Thanks

Joseph
  • 578
  • 3
  • 15
  • 1
    Does this answer your question? [How to annotate function that takes a tuple of variable length? (variadic tuple type annotation)](https://stackoverflow.com/questions/54747253/how-to-annotate-function-that-takes-a-tuple-of-variable-length-variadic-tuple) – matszwecja Aug 11 '23 at 11:31
  • 1
    tl;dr: `some_settings: tuple[Literal["setting_1"] | Literal["setting_2"] | Literal["setting_3"], ...]` – matszwecja Aug 11 '23 at 11:33
  • Perfect, thanks! – Joseph Aug 11 '23 at 11:34

0 Answers0