mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-31 02:02:21 +06:00
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.
This commit is contained in:
parent
1bdf42409c
commit
e34e45536f
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user