Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of NotImplemented?

I have the following function:

   def __eq__(self, other: object) -> Union[bool, NotImplemented]:
        try:
            for attr in ["name", "variable_type"]:
                if getattr(self, attr) != getattr(other, attr):
                    return False
            return True
        except AttributeError:
            return NotImplemented  # Expression has type "Any"

I'm running mypy, and it's telling me that NotImplemented is of type "Any", which it obviously doesn't like.

Is there any better type I can use to remove this error? Or should I just put a type: ignore in and move on? (In addition, is my use of NotImplemented in the return type correct?)

like image 216
Pro Q Avatar asked Jun 16 '19 03:06

Pro Q


People also ask

What is NotImplemented?

NotImplemented is one of Python's six constants living in the built-in namespace. The others are False , True , None , Ellipsis and __debug__ . Similar to Ellipsis , NotImplemented can be reassigned (shadowed). Assignments to it, even as an attribute name, do not raise a SyntaxError .

How do you use NotImplemented in Python?

If you run into the NotImplementedError, the recommended way to handle it is to implement the abstract method for which the error is being raised. Because the NotImplementedError is user-defined, Python can't raise this error on its own. So, you'll need to raise it by a package you're using or code your team wrote.

What does not implemented mean in Python?

The 501 (Not Implemented) status code indicates that the server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.


Video Answer


1 Answers

It is NotImplementedType.

type(NotImplemented)                                                                                                                
# NotImplementedType

Meaning, you can define your function like this:

def foo(self, other: object) -> Union[bool, type(NotImplemented)]: 
     pass 

Now, help(foo) gives,

help(foo)                                                                                                                           
# Help on function foo in module __main__:
#  
# foo(self, other:object) -> Union[bool, NotImplementedType]

As an aside, it is worth mentioning that NotImplemented is a singleton object used with a very specific purpose (it is used to indicate that a particular operation is not defined on an object). You almost never really need to access its type, and to my knowledge there's nowhere you can import NotImplementedType from.

I would just define __eq__ to be

def foo(self, other: object) -> bool: 
     pass 

When NotImplemented is returned, it should be understood that it is a not a valid result, rather an indication that the equality comparison is invalid.

like image 166
cs95 Avatar answered Sep 21 '22 12:09

cs95