-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Create a plugin to handle singledispatch #2904
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
Comments
I cannot reproduce your error. I installed mypy 0.471, copied your example code and ran it against mypy, no errors. Can you provide an exact minimal example where running against mypy 0.471 produces the output you're seeing? I tried to no avail with various combinations of options:
|
I attached a file with the exact code also, here's the console log from the mypy run: D:\Python>"C:\Program Files (x86)\Python36-32\Scripts\mypy.bat" d:\python\testmypyerror.py D:\Python> |
FWIW: |
Mypy will produce this error if you have more than one named function |
In the code sample, there are actually two functions named _. But those names are not ever used. Since the functions are decorated using the singledispatch decorators, those names are merely placeholders. mypy is not fine with this! That's why I filed this PR. I get the errors I posted above. Note, if I rename the second function to e.g, "_42" the error goes away. Perhaps this is outside the scope of what mypy can do? I mean, how is mypy to know that the decorator actually results in no function named "_" to be created? OTOH the error messages are misleading, at least to me. FWIW I simply followed the docs at https://docs.python.org/3/library/functools.html which give the example:
which also yields the same mypy error messages |
This is related to #1332. Until this gets "fixed", you can define functions with names like |
ah I see, now, you are quite correct. FWIW singledispatch could throw away the "_" function, I think that is, if I add
then
it all works just fine. Perhaps i should file a bug report against singledispatch. My gut tells me it should delete "_" In fact, following your suggestion (_int, _list, etc), the namespace gets polluted with these functions |
You shouldn't add del statements and certainly |
I added del to prove the concept. but what is your reasoning for not insisting singledispatch clean up after itself? IOW what is the value of the decorated functions with their original names? Especially when you follow the docs and use the same name for each one, so that at the end only the last one is actually defined? I realize this is now OT. Apologies! Just trying to learn. |
Because one may want to use both And yes, mypy issue tracker isn't the best place to discuss stuff like that, so let's end this. :) |
OK, so the full example (copied from the zip) is: from functools import singledispatch
from typing import Sequence
@singledispatch
def cmp(other, match):
return other in (..., match)
@cmp.register(type)
def _(other, match):
return isinstance(match, other)
@cmp.register(Sequence)
def _(other, match):
return all(cmp(m, o) for m, o in zip(match, other)) The reason mypy complains about this is the duplicate use of def deco(f): return f
@deco
def foo(a): return 0
@deco
def foo(a): return 1 TBH I think the error message could be clearer. The error is much clearer when the redefined function is undecorated, e.g. this gives def foo(a): return 0
def foo(a): return 1 But either way mypy is just complaining about a redefinition, I'm not sure how we should address this. Should we special-case single-dispatch? Or should we special-case |
I think |
Special casing |
Hm, the current rules for special casing |
Another use case for functions with is name A possible temporary workaround could be using |
I agree. A plugin seems the best way forward. |
While the original issue was a bug report, I am marking this a feature issue for adding a plugin, since that seems to be the current best method of dealing with |
I'm curious what the update on this is. Was a plugin ever made? |
There has no progress with a plugin, as far as I know. |
I currently use
|
Workaround for python/mypy#2904
Wow! So we can specify multiple decorators when we need multiple types. Your code example must go to the official documentation. |
It would be useful to handle singledispatchmethod also from __future__ import annotations
import functools
class Foo:
@functools.singledispatchmethod
def __getitem__(self, item: int) -> int:
return 10
@__getitem__.register
def _(self, item: slice) -> list[int]:
return [10, 20, 30] See https://twitter.com/raymondh/status/1433555551133343746?s=19 |
I think that a decorated I have once again needed to add hundreds of |
@oscarbenjamin That should be fixed on master by #10811 (and the mypy_primer run on that PR does show a lot of |
This issue has mostly talked about the redefinition error. But, we should also have singledispatch typing working. e.g. With this example:
You also get errors due to not recognising the overloads:
It would be good for mypy to treat the I was also getting unreachable code errors after every call to the defined function. However, I was unable to reproduce it with a minimal example, so not sure exactly what is causing that. |
Since we now have basic support for |
@JukkaL Would you happen to know which issue interested observers can follow for this problem? #2904 (comment) |
@NeilGirdhar I created #11727 to track it. |
For me, this code:
results in the mypy error:
on the line @cmp.register(type)
mypy version:
What should I do to resolve the error?
The text was updated successfully, but these errors were encountered: