swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
494 stars 40 forks source link

typing-extensions 4.6.0 break literal arguments #106

Closed grochmal closed 1 year ago

grochmal commented 1 year ago

With the release of typing-extensions==4.6.0 the Literal type behaves in a weird way, i.e. it does not work at all on python 3.8.x and python3.9.x . Minimal example, let's call it mytap.py

from tap import Tap
from typing import Literal

class Parse(Tap):
    pet: Literal["cat", "dog", "chinchilla"]

args = Parse().parse_args()

Then we go

$ pip install typed-argument-parser typing-extensions==4.5.0
...
$ python mytap.py --pet cat  # nothing happens, as expected
$ python mytap.py --pet horse
usage: mytap.py --pet {cat,dog,chinchilla} [-h]
mytap.py: error: argument --pet: invalid choice: 'horse' (choose from 'cat', 'dog', 'chinchilla')  # correct error message

But with typing extensions 4.6.0

$ pip install typing-extensions==4.6.0
...
$ python mytap.py --pet cat
usage: mytap.py --pet PET [-h]
mytap.py: error: argument --pet: invalid typing.Literal['cat', 'dog', 'chinchilla'] value: 'cat'

The issue popped out on pydantic yesterday. The issue happens because typing extensions changed:

# 3.8+:
if hasattr(typing, 'Literal'):
    Literal = typing.Literal

to

if sys.version_info >= (3, 10, 1):
    Literal = typing.Literal

Now python 3.8.x and python 3.9.x get broken Literal types.

My hopes are that this will get fixed within typing-extensions soon. But in the meantime, could we fix typing-extensions in typing-argument-parser to typing-extensions>=3.7.4,<4.6.0 ?

swansonk14 commented 1 year ago

Hi @grochmal,

Thank you for raising this issue! We previously required the typing_extensions package to have support for the Literal type in Python version 3.7. However, as of yesterday (6/27/23), Python 3.7 is no longer supported. Therefore, we have removed the dependency on typing_extensions in this commit: https://github.com/swansonk14/typed-argument-parser/commit/c48ef74a489917fbb3ab9dea1ffed6c08c112bd7. This fix will be included in the next release.

Best, Kyle and Jesse