You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the scenario, where an argument could have one of multiple types (see code below). Under version 0.2.1 this code worked. Now under 1.0.0 it breaks with "TypeError: Union types other than 'Optional' are not supported"
from dataclasses import dataclass, field
from typing import Union
from argparse_dataclass import ArgumentParser
def str_or_int(value: str) -> Union[str, int]:
try:
return int(value)
except ValueError:
return value
@dataclass
class Options:
str_or_int: Union[str, int] = field(metadata=dict(type=str_or_int))
parser = ArgumentParser(Options)
print(parser.parse_args(["--str-or-int", "John"]))
print(parser.parse_args(["--str-or-int", "42"]))
I think this is a legitimate use case and by having the 'type' field in metadata the user can take up the burden of transforming those. We are currently using argparse-dataclass 0.2.1 with Union types so this change in behavior is preventing us from updating at the moment.
It would be great to have this support for Union because I would really like to update and get support for Optional types as well.
The text was updated successfully, but these errors were encountered:
Consider the scenario, where an argument could have one of multiple types (see code below). Under version 0.2.1 this code worked. Now under 1.0.0 it breaks with "TypeError: Union types other than 'Optional' are not supported"
I think this is a legitimate use case and by having the 'type' field in metadata the user can take up the burden of transforming those. We are currently using argparse-dataclass 0.2.1 with Union types so this change in behavior is preventing us from updating at the moment.
It would be great to have this support for Union because I would really like to update and get support for Optional types as well.
The text was updated successfully, but these errors were encountered: