-
Notifications
You must be signed in to change notification settings - Fork 49
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 type annotations to siuba verbs and improve type compliance #447
Comments
I just played with The combination of Here is a minimal example (still quite verbose) with the right type annotations. The implementation is not as nice as from functools import singledispatch
from typing import overload
from typing_extensions import reveal_type
import pandas as pd
@singledispatch
def _mutate(x: None | int | pd.DataFrame | pd.Series) -> None | int | pd.DataFrame:
raise TypeError(type(x))
@_mutate.register
def _(x: None) -> None:
return x
@_mutate.register
def _(x: int) -> int:
return x + 1
@_mutate.register
def _(x: pd.DataFrame) -> pd.DataFrame:
return x
@_mutate.register
def _(x: pd.Series) -> pd.DataFrame:
return x.to_frame()
@overload
def mutate(x: None) -> None: ...
@overload
def mutate(x: int) -> int: ...
@overload
def mutate(x: pd.Series) -> pd.DataFrame: ...
@overload
def mutate(x: pd.DataFrame) -> pd.DataFrame: ...
def mutate(x: None | int | pd.DataFrame | pd.Series) -> None | int | pd.DataFrame:
return _mutate(x)
if __name__ == "__main__":
res = mutate(None)
reveal_type(res)
res = mutate(15)
reveal_type(res)
data = pd.DataFrame({})
# assert isinstance(data, pd.DataFrame)
res = mutate(data)
reveal_type(res)
data = pd.Series(data=[1.0], index=[0])
res = mutate(data)
reveal_type(res) This works with pyright
but mypy complaints
I guess in a perfect world one would like a decorator |
Thanks for the nudge and overload example! I'll add overloads to at least resolve #418. There are some very relevant mypy issues open for better support singledispatch support:
I think the issue with overload is that it needs to be with the initial function declaration, but singledispatch.register can happen across modules / libraries. From the issues though, it seems like there many be a few reasonable solutions.. |
First of all, I really like
siuba
and I use it a lot 🙏siuba
would be even better, if it a) had type annotations for the core verbs and b) type checkers like pyright/mypy/etc would not complain so much. (I tend to turn off type checking for my siuba code blocks.)This ticket is related to #418
Note:
siuba
uses decorators and singledispatch quite a bit. Sadly, getting type annotations right with decorators can be a bit painful in my experience :/The text was updated successfully, but these errors were encountered: