question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No love for NotImplemented?

See original GitHub issue

When 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
positacommented, Jul 1, 2021

… You’re in luck… again.

For some definition of luck, perhaps. Or maybe the emphasis belongs on recognizing that it was exactly one of us who was lucky here.

Mypy does exactly what everyone wants.

Oh! Well I guess that's all fixed then.

It may be but a small consolation that this irony was not lost on me.

That means I now need to ~disembowel~ refactor beartype to do exactly the same thing. Global happiness ensues.

A part of me celebrates resolution. After reading 57e5e99, however, another part wonders with great trepidation, “What have I wrought?”

The horror!

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.

0reactions
leyceccommented, Jul 5, 2021

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:

class CodeGoneWild(class):
    def __add__(self, other):   # <-- this is fine, bro
        return (
            42 if isinstance(other, CodeGoneWild) else
            NotImplemented
        )

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 private beartype._decor._code.codemain._code_check_return() function:

    # If this return is unannotated, generate code calling this callable
    # unchecked and returning this value from this wrapper.
    if hint is _RETURN_HINT_EMPTY:
        # >>>> DO SOMETHING MAGICAL AND RISKY HERE <<<<
        func_wrapper_code = CODE_RETURN_UNCHECKED

Both of us deserve a well-deserved break and it’s not even Monday. 😭

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found