Search code examples
pythonoverloading

NotImplementedError: You should not call an overloaded function


@overload
def setSize(self,size:tuple[int|str])->None:
    '''
    Set image size (width,height)
    '''
    try:self.options.append(f"width=\"{str(size[0])}\" height=\"{str(size[1])}\"")
    except IndexError:print("Error reading the size, aborting")
@overload
def setSize(self,width:int|str,height:int|str)->None:
    '''
    Set image Size
    '''
    self.setSize((width,height))

This is my code and I called this function as var.setSize((500,500)) which would normally call the top one but I got this error:

NotImplementedError: You should not call an overloaded function. A series of @overload-decorated functions outside a stub module should always be followed by an implementation that is not @overload-ed.

Solution

  • When writing code with function overloading in Python, it is important to remember that

    [y]ou should not call an overloaded function. A series of @overload-decorated functions outside a stub module should always be followed by an implementation that is not @overload-ed.

    This is because @overload-ed functions are intended to simply declare the types of the arguments without providing an actual implementation. Therefore, an implementation must be provided following the @overload-ed functions in order for the code to be valid.