Loss of type-hinting benefits #70
Replies: 5 comments
-
| I don't think it's possible.  | 
Beta Was this translation helpful? Give feedback.
-
| Not sure. I'm not familiar with how it works on your end. There are several NumPy methods/functions that can be overloaded.  I'll dig into it if I have time. In any case, thanks for responding :) | 
Beta Was this translation helpful? Give feedback.
-
| Here's an example of  Note how it doesn't provide a Union of returns, but rather multiple versions of the overloaded method/function. This is done through the use of the  I tried manually creating the overloads for the  | 
Beta Was this translation helpful? Give feedback.
-
| Hi @edan-bainglass, I might be able to provide a tricky method for you to try. @innocent(multimethod)
def to_chunks(
    data: Union[Dict, List], chunk_size: int = 1 << 16
) -> Iterable[Tuple[List, List]]:
    raise NotImplementedError("Unknown data type")
@innocent(to_chunks.register)
def _(data: list, chunk_size: int = 1 << 16) -> Iterable[Tuple[List, List]]:
    pass
@innocent(to_chunks.register)
def _(data: dict, chunk_size: int = 1 << 16) -> Iterable[Tuple[List, List]]:
    passwhich the decorator is: P = ParamSpec("P")
R = TypeVar("R")
def innocent(wrapper: Callable) -> Callable:
    def _innocent(f: Callable[P, R]) -> Callable[P, R]:
        nonlocal wrapper
        @wrapper
        @functools.wraps(f)
        def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
            return f(*args, **kwargs)
        return _wrapper
    return _innocentnow the type hint works, it should also work on overloading. Here is overloading version(but this version will cause the mypy check to fail, maybe need some cast or bound): @overload
def to_chunks(data: Dict, chunk_size: int) -> Iterable[Tuple[List, List]]:
    ...
@overload
def to_chunks(data: List, chunk_size: int) -> Iterable[Tuple[List, List]]:
    ...
#Here is the code above | 
Beta Was this translation helpful? Give feedback.
-
| AFAICT,   | 
Beta Was this translation helpful? Give feedback.



Uh oh!
There was an error while loading. Please reload this page.
-
I have two methods in my
Gradientclass that I would like to overload asscalar_product:and
I decorated both with
multimethodas follows:and
This works, i.e. calling
scalar_productwithGradientorDensityarguments yields the expected respective results. However, I seem to have lost my type-hinting benefits. My editor (VS code) now thinksscalar_productis a property with a return type ofmultimethodorMethodType.Am I missing something? Is there some way of gaining the overloading functionality without losing type-hinting?
Thanks in advance 🙂
Beta Was this translation helpful? Give feedback.
All reactions