'Is there a way to type hint a decorator in Python so that functions decorated with it can be redefined without causing a MyPy error?

I'm creating a library that allows a function to be defined multiple times with different parameters, with only the final definition being considered as the actual definition (much like the behaviour of functools.overload).

Basically I want to achieve something like this:

def forget(func):
    # In reality I'm doing a little more than just returning nothing
    return None

# This version effectively doesn't exist after it is decorated
@forget
def hello():
    pass
# Therefore defining this function shouldn't cause an error
def hello():
    pass

How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source