Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Improve literal_eval type definition #4452

Closed
sk- opened this issue Aug 16, 2020 · 2 comments
Closed

[FR] Improve literal_eval type definition #4452

sk- opened this issue Aug 16, 2020 · 2 comments

Comments

@sk-
Copy link
Contributor

sk- commented Aug 16, 2020

ast.literal_eval can only return Dict, Set, Tuple, int, str, bool, float, complex and None. However its return type is set as Any.

The problem with that type definition is that mypy (with warn_return_any) complains when you compare against Any, and the following code:

def comparator(value: str) -> bool:
    return ast.literal_eval(value) == expected_value

raises

Returning Any from function declared to return "bool"
@hauntsaninja
Copy link
Collaborator

Generally speaking, Union return types are problematic. For more discussion, see https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions I don't think literal_eval is an exception here, anecdotally and based on https://grep.app/search?q=literal_eval%28&filter[lang][0]=Python

@srittau
Copy link
Collaborator

srittau commented Aug 17, 2020

Another example where something like python/typing#566 could help, but at the moment there is not much we can do in typeshed, unfortunately.

@srittau srittau closed this as completed Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants