swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
505 stars 40 forks source link

Tuple parsing with literals #118

Closed swansonk14 closed 1 year ago

swansonk14 commented 1 year ago

Tuple parsing is failing with Literals. See the example below.

from tap import Tap
from typing import Literal

class Args(Tap):
    arg: tuple[Literal['a', 'b'], ...]

args = Args().parse_args(['--arg', 'a', 'b'])

Running the above code produces the error argument --arg: invalid <tap.utils.TupleTypeEnforcer object at 0x7fe3937cb2e0> value: 'a'

In contrast, the following code works correctly, indicating that it is specifically a problem with the Literal type.

from tap import Tap
from typing import Literal

class Args(Tap):
    arg: tuple[str, ...]

args = Args().parse_args(['--arg', 'a', 'b'])

Additionally, the following code, which uses list in place of tuple, also works, so it is specifically the combination of tuples and Literals that has an issue.

from tap import Tap
from typing import Literal

class Args(Tap):
    arg: list[Literal['a', 'b']]

args = Args().parse_args(['--arg', 'a', 'b'])
swansonk14 commented 1 year ago

This has been fixed: 18b13d18e69dc948a3a8cc6b5372f25ccf5d3499