From e34e45536fb8ae7f4d966e3604e3ca5f77e2b486 Mon Sep 17 00:00:00 2001 From: Thomas Tanon Date: Fri, 8 Jan 2021 14:10:44 +0100 Subject: [PATCH] Makes HfArgumentParser compatible with Python 3.9 (#9479) Python 3.9 changed the format of the string serialization of `typing.Optional`. For example, `str(typing.Optional[str])` is `typing.Union[str, NoneType]` in python 3.8 and `typing.Optional[str]` in Python 3.9. --- src/transformers/hf_argparser.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/transformers/hf_argparser.py b/src/transformers/hf_argparser.py index f686121263..5583602254 100644 --- a/src/transformers/hf_argparser.py +++ b/src/transformers/hf_argparser.py @@ -66,9 +66,15 @@ class HfArgumentParser(ArgumentParser): typestring = str(field.type) for prim_type in (int, float, str): for collection in (List,): - if typestring == f"typing.Union[{collection[prim_type]}, NoneType]": + if ( + typestring == f"typing.Union[{collection[prim_type]}, NoneType]" + or typestring == f"typing.Optional[{collection[prim_type]}]" + ): field.type = collection[prim_type] - if typestring == f"typing.Union[{prim_type.__name__}, NoneType]": + if ( + typestring == f"typing.Union[{prim_type.__name__}, NoneType]" + or typestring == f"typing.Optional[{prim_type.__name__}]" + ): field.type = prim_type if isinstance(field.type, type) and issubclass(field.type, Enum):