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

Add Literal type for python 3.7 #217

Closed
erikmannerfelt opened this issue Jul 2, 2021 · 0 comments · Fixed by #218
Closed

Add Literal type for python 3.7 #217

erikmannerfelt opened this issue Jul 2, 2021 · 0 comments · Fixed by #218
Labels
enhancement Feature improvement or request priority

Comments

@erikmannerfelt
Copy link
Contributor

As of python 3.8, the Literal type hint exists by default. It is extremely useful, but cannot be used in geoutils directly as it is not supported by python 3.7! Luckily, there's a solution...

Why it's important

If I have a function that returns a different type depending on a boolean flag, types should be annotated accordingly:

def func(flag: bool = True) -> int | str:
    if flag:
         return 1
    return "one"

The problem without py38+ is that the type will be int | str no matter what flag is given.
This will always return an int if flag==True and str if not. The solution is overload and Literal:

from typing import overload, Literal

@overload
def func(flag: Literal[True])  -> int: ...

@overload
def func(flag: Literal[False])  -> str: ...

 # here goes the function definition

The solution

typing-extensions , the official backport of new typing functions!
Using conditionals in the pip and conda requirements, we can make sure that it is only installed for python <=3.7.

This has already been a big problem in #208 and #216 , so this should be fixed soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature improvement or request priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant