question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Show possible values of a list field, if the amount of values is finite

See original GitHub issue

Is your feature request related to a problem? Please describe. I’d like to be able to pass a list of flags to my program. The flags are all listed in an enum and the variable in the dataclass is defined as flags: List[MyEnum] = ... Argument parsing works, but no proper help text is generated.

Describe the solution you’d like If the argument of the List annotation has a finite amount of possible values (e.g. enum values), it would be nice to list those. This is already done, when a field is directly annotated as an enum.

Possible output:

  --my_config [MyEnum [MyEnum ...]]
                        Configure some values here
                        (default: [SomeValue, OneMore])
                        (possible: {SomeValue, AnotherValue, YetAnotherValue, OneMore})

Although I’m not sure, where/how the values should be listed

Describe alternatives you’ve considered I could manually update the help text when the enum values change, but that violates the DRY principle.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Woozacommented, Apr 16, 2020

Hey @lebrice, thanks for your fast feedback and effort. The amount of flags is around 20 to 30. I think your solution with manually specifying the help string is ok, since it can be automatically generated. I only have to work out how to deal with the line breaks, to avoid a single long line. I’ll close this issue and might reopen it, if I stumble into any further problems 😄

1reaction
lebricecommented, Apr 15, 2020

Hey @Wooza, out of curiosity, in your context is the number of flags used completely arbitrary? Could you give me a bit more detail about some scenarios where you think this could be useful?

For instance this is what is currently supported:

import enum
from dataclasses import dataclass
from typing import List, Tuple
from simple_parsing import ArgumentParser, list_field, field

class Color(enum.Enum):
    RED = "RED"
    ORANGE = "ORANGE"
    BLUE = "BLUE"
    MAGENTA = "MAGENTA"

@dataclass
class MyPreferences:
    """You can use Enums"""
    color: Color = Color.BLUE # my favorite colour
    # A list of colors I hate.
    hated_colors: List[Color] = list_field(Color.MAGENTA, Color.ORANGE)

parser = ArgumentParser()
parser.add_arguments(MyPreferences, "my_preferences")
args = parser.parse_args()
prefs: MyPreferences = args.my_preferences
print(prefs)

Executing this with the “–help flag”:

python .\examples\enums\enums_example.py --help
usage: enums_example.py [-h] [--color {RED,ORANGE,BLUE,MAGENTA}] [--hated_colors [Color [Color ...]]]

optional arguments:
  -h, --help            show this help message and exit

MyPreferences ['my_preferences']:
  You can use Enums

  --color {RED,ORANGE,BLUE,MAGENTA}
                        my favorite colour (default: BLUE)
  --hated_colors [Color [Color ...]]
                        A list of colors I hate. (default: [<Color.MAGENTA: 'MAGENTA'>, <Color.ORANGE: 'ORANGE'>])

As you can see, the help string defaults for enums are quite ugly. I was able to achieve the kind of formatting I think you’re describing with this:

@dataclass
class MyPreferences:
    """You can use Enums"""
    color: Color = Color.BLUE # my favorite colour
    # A list of colors I hate.
    hated_colors: List[Color] = list_field(
        Color.MAGENTA, Color.ORANGE, # the default values
        choices=[e.name for e in Color], # the available values
    )

which gives:

python .\examples\enums\enums_example.py --help
usage: enums_example.py [-h] [--color {RED,ORANGE,BLUE,MAGENTA}] [--hated_colors [{RED,ORANGE,BLUE,MAGENTA} [{RED,ORANGE,BLUE,MAGENTA} ...]]]

optional arguments:
  -h, --help            show this help message and exit

MyPreferences ['my_preferences']:
  You can use Enums

  --color {RED,ORANGE,BLUE,MAGENTA}
                        my favorite colour (default: BLUE)
  --hated_colors [{RED,ORANGE,BLUE,MAGENTA} [{RED,ORANGE,BLUE,MAGENTA} ...]]
                        A list of colors I hate. (default: [<Color.MAGENTA: 'MAGENTA'>, <Color.ORANGE: 'ORANGE'>])

However this doesn’t currently work (apart from the “–help” option), as its impossible to parse the values:

python .\examples\enums\enums_example.py --hated_colors ORANGE RED
usage: enums_example.py [-h] [--color {RED,ORANGE,BLUE,MAGENTA}] [--hated_colors [{RED,ORANGE,BLUE,MAGENTA} [{RED,ORANGE,BLUE,MAGENTA} ...]]]
enums_example.py: error: argument --hated_colors: invalid choice: <Color.ORANGE: 'ORANGE'> (choose from 'RED', 'ORANGE', 'BLUE', 'MAGENTA')

Also, for the help string formatting, you could also easily do this, which would totally work! (you’d lose the auto-generated help strings and would have to add it manually to the help argument though):

@dataclass
class MyPreferences:
    """You can use Enums"""
    color: Color = Color.BLUE # my favorite colour
    # A list of colors I hate.
    hated_colors: List[Color] = list_field(
        Color.MAGENTA, Color.ORANGE, # the default values, if any.
        help=f"Some Colors I hate. (Possible: {set(e.name for e in Color)})",
    )

Which would get you what you want, in the end:

(...)
  --hated_colors [Color [Color ...]]
                        Some Colors I hate. (Possible: {'BLUE', 'RED', 'MAGENTA', 'ORANGE'}) (default: [<Color.MAGENTA: 'MAGENTA'>, <Color.ORANGE: 'ORANGE'>])

This sounds like the best option, in my opinion. Let me know what you think.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to show the only absolute value on a finite field is the ...
The textbook I'm currently reading (Gouvêa - P-adic Numbers An Introduction) asked me to show that for a finite field K the only...
Read more >
Add a lookup or values list field to an Access web app
Use a lookup field to find ("look up") values in one table that you can use in another table. A lookup field can...
Read more >
isFinite() - JavaScript - MDN Web Docs - Mozilla
The global isFinite() function determines whether the passed value is a finite number. If needed, the parameter is first converted to a number....
Read more >
Finite field - Wikipedia
In mathematics, a finite field or Galois field (so-named in honor of Évariste Galois) is a field that contains a finite number of...
Read more >
4.2 Types of variables - Statistique Canada
A variable is a characteristic that can be measured and that can assume different values. Height, age, income, province or country of birth, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found