42

The following code does not work as expected. Apparently, I cannot use the class's own type inside class definition:

class Foo:
    def __init__(self, key :str) -> None:
        self.key = key

    def __eq__(self, other :Foo) -> bool:
        return self.key == other.key

print('should be true: ', Foo('abc') == Foo('abc'))
print('should be false: ', Foo('abc') == Foo('def'))

The result of running it is:

Traceback (most recent call last):
  File "class_own_type.py", line 1, in <module>
    class Foo:
  File "class_own_type.py", line 5, in Foo
    def __eq__(self, other :Foo) -> bool:
NameError: name 'Foo' is not defined

Also, checking the code with mypy returns:

class_own_type.py:5: error: Argument 1 of "__eq__" incompatible with supertype "object"

How can I correct this code to be valid, both for Python and for mypy?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
macjan
  • 531
  • 1
  • 4
  • 6

1 Answers1

65

When Foo.__eq__ is being defined the name Foo is still unbound, because the class itself hasn't yet been created. Remember: function arguments are evaluated at function definition time, not at function call time.

From Python 3.7+ you can postpone evaluation of annotations by adding this import at the top of the module:

from __future__ import annotations

For Python < 3.7, you can use string literals to delay evaluation of the type:

class Foo:
    def __init__(self, key: str) -> None:
        self.key = key

    def __eq__(self, other: 'Foo') -> bool:
        return self.key == other.key

print('should be true: ', Foo('abc') == Foo('abc'))
print('should be false: ', Foo('abc') == Foo('def'))
wim
  • 338,267
  • 99
  • 616
  • 750