Overloading bear confuses Mypy (and/or perhaps issue author)
See original GitHub issueConsider:
# test_case.py
from typing import Union, overload
from beartype import beartype
class MypySoConfuse:
@overload
def __getitem__(self, key: int) -> int:
...
@overload
def __getitem__(self, key: slice) -> tuple:
...
@beartype # <- comment this out and the warning goes away
def __getitem__(self, key: Union[int, slice]) -> Union[int, tuple]:
if isinstance(key, int):
return 0
else:
return ()
This yields:
$ mypy --version
mypy 0.902
$ mypy test_case.py
test_case.py:14: error: Overloaded function implementation does not accept all possible arguments of signature 1
test_case.py:14: error: Overloaded function implementation does not accept all possible arguments of signature 2
Found 2 errors in 1 file (checked 1 source file)
If you comment out the @beartype
decorator above, Mypy stops complaining. beartype is utter magic to me, and I—sadly—have not spent much (okay, any) time investigating a root cause, so I don’t know if this is a flaw in Mypy, or beartype, or just a fundamental incompatibility (no judgment, some people are just not meant to be together, and that’s okay).
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
False positive: in overloads, mypy is confused by torch.Tensor ...
I'm running into a similar problem but instead of torch.Tensor and numpy.ndarray it is two different types from an untyped library. from typing ......
Read more >Simple Index
andor · andrewlist · androguard · android · android-asset-resizer · android-autotools · android-benchmark-views · android-debian-builder · android-dumpkey
Read more >Rebuild Podcast Republic
Podcast Republic is one of the most popular podcast platforms on the world serving 1M+ podcasts and 500M+ episodes worldwide.
Read more >ISSEP 2019 - Easy Conferences
Computer Science Problem Solving in the Escape Game "Room-X". Alexander Hacke ... The authors of article describe the experience of working with students....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To my utter embarrassment (but not shock), this is indeed not only a Mypy issue, but a known one. Specifically: python/mypy#8393. No need to continue chewing valuable cognitive cycles keeping this issue open where it be known where said dragons and dark magic lie.
In summary, I’m closing this as fixed and suggesting that https://github.com/beartype/beartype/issues/39#issuecomment-871865394 et seq. should be treated as a non sequitur.
Thanks for all the attention!
Bwaha! I cackle madly like a scantily clad Heffalump huffing honey, pictured here.
I suspect we’ve now flung ourselves headlong from what was once an actual beartype issue into what is now a probable mypy issue. Yes, I am now bravely shifting the blame! Behold, evidence supporting my suspicious claims:
So, we’ve lazily replaced
@beartype
in the sad Eeyore example you graciously gave above with the most trivial possible decorator. Yup, it’s none other than the@identity
decorator silently reducing to a noop by returning the passed callable unmodified as is. Excitement! Thrills! And… spills:Verily, mypy spaketh. It spaketh the exact same thing as when decorated by the
@beartype
decorator, actually. We infer that the actual decorator being applied doesn’t particularly matter. It’s something else that matters, which means either:@overload
intersects with dark magic and flamboyant dragons – so I can’t say for certain that you’re doing it right.Would you mind reposting the
@identity
decorator example I just gave to mypy’s issue tracker? They should be able to clarify the situation. Until then, I’ll graciously 🤮 leave this open. It’s possible, however unlikely, that we’ve still mistyped the@beartype
decorator in some way.please let this be mypy’s fault please please please