1

is there anyone knows how to overload hint like the default overload.decorator do?

I have a custom working overload decorator, but cannot hint correctly.

* the way to custom overload decorator for example

The reason behind doing this is because of :

  • 1: Default overload.decorator is only for decoration
  • 2: Alternative ways like multipledispatch.dispatch are not from standard library

For example, in vscode, the default @overload will hint like below

from typing import overload


class Foo :

    @overload
    def bar( self , value : int ) -> int : ...

    @overload
    def bar( self , value : str ) -> str : ...

enter image description here


The custom overload decorator, however, will only display the last loaded method.

def custom_overload( * types ) : 

    # logic to resolve callable
    ...

class Foo :

    @custom_overload( int )
    def bar( self , value : int ) -> int : ...
    
    @custom_overload( str )
    def bar( self , value : str ) -> str : ...

enter image description here


How can I hint the decorated callable like the default overload.decorator do.

Is it related to this?

How to document Python functions with overload/dispatch decorators?

Thank you for your advises.

Micah
  • 4,254
  • 8
  • 30
  • 38

1 Answers1

0

If there is not alternative way, this will be the only one solution for python at this moment. Its ugly but, below is what I will do.

# default overload 
from typing import overload as hint

# custom overload
def overload( * types ) : 

    # logic to resolve callable
    ...

class Foo :

    @hint
    def bar( self , value : int ) -> int : ...

    @hint
     def bar( self , value : str ) -> str : ...

    @overload( int )
    def bar( self , value : int ) -> int : ...
    
    @overload( str )
    def bar( self , value : str ) -> str : ...
Micah
  • 4,254
  • 8
  • 30
  • 38