The name Foo
isn't bound yet, because the class itself has not yet been defined at the time that you try to use the name (remember: function arguments are evaluated at function definition time, not at function call time).
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'))
EDIT: For Python 3.7+ use:
from __future__ import annotations
NOTE: It must be placed before all other imports.