Message 401052 - Python tracker (original) (raw)

Python's help() function does not display overloaded function signatures.

For example, this code:

from typing import Union

class Smudge(str):

    @overload
    def __getitem__(self, index: int) -> str:
        ...

    @overload
    def __getitem__(self, index: slice) -> 'Smudge':
        ...

    def __getitem__(self, index: Union[int, slice]) -> Union[str, 'Smudge']:
        'Return a smudged character or characters.' 
        if isinstance(index, slice):
            start, stop, step = index.indices(len(self))
            values = [self[i] for i in range(start, stop, step)]
            return Smudge(''.join(values))
        c = super().__getitem__(index)
        return chr(ord(c) ^ 1)

Currently gives this help:

__getitem__(self, index: Union[int, slice]) -> Union[str, ForwardRef('Smudge')]
    Return a smudged character or characters.

What is desired is:

__getitem__(self, index: int) -> str
__getitem__(self, index: slice) -> ForwardRef('Smudge')
    Return a smudged character or characters.

The overload() decorator is sufficient for informing a static type checker but insufficient for informing a user or editing tool.