"...git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "d8049331dc087f4afeafce2be8e34d4222bcc9f2"
Unverified Commit bc019b0e authored by Bram Vanroy's avatar Bram Vanroy Committed by GitHub
Browse files

ExplicitEnum subclass str (JSON dump compatible) (#17933)

* ExplicitEnum subclass str (JSON dump compatible)

* allow union if one of the types is str
parent b089cca3
...@@ -84,7 +84,9 @@ class HfArgumentParser(ArgumentParser): ...@@ -84,7 +84,9 @@ class HfArgumentParser(ArgumentParser):
origin_type = getattr(field.type, "__origin__", field.type) origin_type = getattr(field.type, "__origin__", field.type)
if origin_type is Union: if origin_type is Union:
if len(field.type.__args__) != 2 or type(None) not in field.type.__args__: if str not in field.type.__args__ and (
len(field.type.__args__) != 2 or type(None) not in field.type.__args__
):
raise ValueError( raise ValueError(
"Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because"
" the argument parser only supports one type per argument." " the argument parser only supports one type per argument."
......
...@@ -240,7 +240,7 @@ class ModelOutput(OrderedDict): ...@@ -240,7 +240,7 @@ class ModelOutput(OrderedDict):
return tuple(self[k] for k in self.keys()) return tuple(self[k] for k in self.keys())
class ExplicitEnum(Enum): class ExplicitEnum(str, Enum):
""" """
Enum with more explicit error message for missing values. Enum with more explicit error message for missing values.
""" """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment