siuba
siuba copied to clipboard
Implement process for importing backends
Related to
- #86
- #54
- #141
Problem
We might have generic singledispatch function, with a concrete implementation in another file.
generics.py:
from functools import singledispatch
@singledispatch
def f(x):
raise NotImplementedError()
pandas_backend.py:
import pandas as pd
from generics import f
@f.register(pd.DataFrame)
def _f_df(x):
print("some DF function")
return x
Where pandas_backend is optional, as one of several backends. This setup lets us avoid importing pandas when it's not needed.
However, if a user has imported pandas, it's not a guarantee they'll get the concrete implementations they need, or an informative error message:
import pandas as pd
df = pd.DataFrame({'x': [1,2,3]})
from generics import f
# generic NotImplementedError message, no knowledge to tell users about pandas_backend.
f(df)
Existing approaches
- sqlalchemy dialects - setuptools entrypoints
- ibis setuptools entrypoints
- import hooks - probably a bad idea