No love for NotImplemented?
See original GitHub issueWhen overriding binary special methods (e.g., __eq__
, etc.), returning NotImplemented
is a common convention, and generally omitted from returned types. But omitting it doesn’t agree with poor old bear:
from beartype import beartype
class Sneetch:
@beartype
def __init__(self, star: bool):
self._upon_thars = star
@beartype
def __eq__(self, other) -> bool:
if isinstance(other, Sneetch):
return True # Sylvester McMonkey McBean can pound sand
else:
return super().__eq__(other)
assert Sneetch(True) == Sneetch(False) # all good
assert (Sneetch(True) == 42) is False # boom
This will result in the following:
beartype.roar.BeartypeCallHintPepReturnException: @beartyped Sneetch.__eq__() return "NotImplemented" violates type hint <class 'bool'>, as "NotImplemented" not bool.
Making NotImplemented
an explicit part of the return type is apparently hard (see python/mypy#4791).
I’m not sure what the right play is here. One could probably make a pretty good case that one should omit the @beartype
decorator and lean on the internals (in which case, this should be closed as not-a-bug). But I thought I’d raise it anyway to get another opinion.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Binary special methods cannot return NotImplemented #178
return NotImplemented instead of the declared return type. To Reproduce from typeguard ... No love for NotImplemented? beartype/beartype#38.
Read more >Python's `NotImplemented` Type - Shahriar Tajbakhsh - Medium
This post discusses Python's NotImplemented built-in constant/type; ... to indicate that the operation is not implemented with respect to the other type; ...
Read more >Why does int.__eq__ seem to be not implemented in python2
I've got a head scratchier and it seems I'm not the only one, but is there really no solution? I find that hard...
Read more >How to Fix the HTTP 501 Not Implemented Error on Your Site
The HTTP 501 Not Implemented status code indicates that the server does not support the functionality required to fulfill the request.
Read more >Fix “Not Implemented” Error in Microsoft Outlook on Windows
One of the common error faced by the Outlook users is “Not Implemented” error. It occurs when a user tries to check for...
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
For some definition of luck, perhaps. Or maybe the emphasis belongs on recognizing that it was exactly one of us who was lucky here.
It may be but a small consolation that this irony was not lost on me.
A part of me celebrates resolution. After reading 57e5e99, however, another part wonders with great trepidation, “What have I wrought?”
May we all draw the courage to persevere from some sliver of hope that somehow the cure turns out not to be worse than the disease.
Brilliant! I hadn’t realized Mypy also accepted non-boolean binary dunder methods, although that’s painfully obvious in hindsight.
I also realized there’s a related issue with our current approach: it doesn’t permit unannotated returns. Interestingly, mypy does permit that and is thus totally down with this wacky tabacky:
So, we also need to explicitly handle the case of binary dunder methods lacking return annotations. The best place for that “fun” kludge is probably in the following
if
branch in the body of the privatebeartype._decor._code.codemain._code_check_return()
function:Both of us deserve a well-deserved break and it’s not even Monday. 😭