lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
384 stars 46 forks source link

Union handles lists #308

Open grahamannett opened 3 months ago

grahamannett commented 3 months ago

For #297

There might be a more elegant way to handle this but I think it would require redoing a lot more than this?

I also am not sure if this is the correct place to put this as there seemed like 2-3 other potential places and maybe more if i kept looking.

The if/else stuff is basically just trying to handle the equivalent of below (which probably makes more sense if there are more cases to worry about for UnionTypes):


  if any(utils.is_list(o) for o in utils.get_args(self.type)):
      # for: Union[list[int], list[str]] keep as list
      if all(utils.is_list(o) for o in utils.get_args(self.type)):
          return raw_parsed_value
      # for Union[str, list[str]] if single value, then dont keep as list
      elif len(raw_parsed_value) == 1:
          return raw_parsed_value[0]
  # are there other cases?
grahamannett commented 3 months ago

Seems like the linter or pre-commit checks failed? Not sure if that was because of me, I didn't see any info on what to do and the default hooks/pre-commit stuff did not work for me but didn't spend too much time trying.

I can go back and try and fix the PR to see if that would be helpful and besides line length I am not sure what other things in the pre-commit might be different (I use black but probably should switch to ruff it seems, just had issues getting it to work as I wanted when I had looked into using it prior)

lebrice commented 3 months ago

Hey @grahamannett , yes its the pre-commit hooks which are failing. Can you do

pip install pre-commit
pre-commit run --all-files

And commit the changes to the files you changed? That should fix it.

Thanks again!

grahamannett commented 3 months ago

@lebrice just ran it and commit the file that changed related to what I PR'ed earlier but still says ruff-format is failing from my setup.

lebrice commented 3 months ago

@lebrice just ran it and commit the file that changed related to what I PR'ed earlier but still says ruff-format is failing from my setup.

Ok no prob I'll take a look tomorrow. Thanks again!