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

Improve error message Union not allowed (#17769)



* Improve error message Union not allowed

* make style

* Update src/transformers/hf_argparser.py
Co-authored-by: default avatarSylvain Gugger <35901082+sgugger@users.noreply.github.com>
Co-authored-by: default avatarSylvain Gugger <35901082+sgugger@users.noreply.github.com>
parent abc400b0
...@@ -85,7 +85,11 @@ class HfArgumentParser(ArgumentParser): ...@@ -85,7 +85,11 @@ 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 len(field.type.__args__) != 2 or type(None) not in field.type.__args__:
raise ValueError("Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union`") raise ValueError(
"Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because"
" the argument parser only supports one type per argument."
f" Problem encountered in field '{field.name}'."
)
if bool not in field.type.__args__: if bool not in field.type.__args__:
# filter `NoneType` in Union (except for `Union[bool, NoneType]`) # filter `NoneType` in Union (except for `Union[bool, NoneType]`)
field.type = ( field.type = (
......
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