Comments (5)
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.
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.
Here's an example of np.array
introspection...
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.
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.
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
from multimethod.
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)
- Regression in 1.10 regarding typing.Literal HOT 6
- Clarify if `@multimethod` works with position-or-keyword arguments HOT 1
- Clarify "Positional-only" in Readme HOT 1
- type `__args__` should be retrieved using `typing.get_args()` HOT 2
- Is there a way to dispatch superclass methods? HOT 1
- F811 redefinition of unused '__init__' from line 33 HOT 2
- Thread safety fix HOT 1
- Doesn't work properly with pydantic types HOT 2
- Using >1 different Union raises TypeError HOT 1
- Make __call__ faster to step through in the debugger HOT 2
- multimethod does not recognize correctly 'collections.abc.Container' types HOT 7
- TypeError: type 'dict_keys' is not an acceptable base type
- Strange behaviour with pandas function application methods HOT 1
- multimethod "forgets" keyword only arguments HOT 2
- Documentation isn't updated? HOT 5
- Overload fails on Optional arguments HOT 1
- flake8 reports redefinition when using multimethod HOT 2
- Classmethod fails on the uninitialized class object HOT 2
- Different names for the multimethod in a class does not work with staticmethod and classmethod decorators HOT 1
- PEP about multiple dispatch and @typing.overload
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multimethod.