Giter VIP home page Giter VIP logo

Comments (5)

coady avatar coady commented on June 2, 2024

I don't think it's possible. scalar_product is a single multimethod object. So even if it did some sort of code generation with annotations on the __call__ method, it's not clear what it they would be. E.g., would the return type be Union[Density, ndarray].

from multimethod.

edan-bainglass avatar edan-bainglass commented on June 2, 2024

Not sure. I'm not familiar with how it works on your end. There are several NumPy methods/functions that can be overloaded. np.array and np.zero for example. Not sure how NumPy implements the overloading.

I'll dig into it if I have time. In any case, thanks for responding :)

from multimethod.

edan-bainglass avatar edan-bainglass commented on June 2, 2024

Here's an example of np.array introspection...

image

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 typing module's @overload decorator in the respective stub file.

I tried manually creating the overloads for the scalar_product methods in the corresponding stub file, but it is still not quite doing what I want it to do, i.e. the above behavior for np.array.

from multimethod.

uuirs avatar uuirs commented on June 2, 2024

Hi @edan-bainglass, I might be able to provide a tricky method for you to try.
Add a decorator to pass through the type, in my case, it just like:

@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]]:
    pass

which 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 _innocent

now the type hint works, it should also work on overloading.

image

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

Screenshot 2022-04-29 at 12 10 02 PM

from multimethod.

coady avatar coady commented on June 2, 2024

AFAICT, typing.overload doesn't do anything; editors must be statically checking for it. Tried the same example with functools.singledispatch and VSCode showed

instance _SingleDispatchCallable(*args: Any, **kwargs: Any) -> _T

from multimethod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.