[issue45100] Teach help about typing.overload()

2021-09-05 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> The two @overload definitions will be overwritten by 
> the non-overload one at runtime, and hence will ever 
> been seen by help().

We can fix this by adding an __overloads__ attribute.  The overload decorator 
can accumulate the chain in an external namespace and function creation can 
move that accumulation into the new attribute.

- Proof of concept -

from typing import Union, _overload_dummy

def create_function(func):
namespace = func.__globals__
key = f'__overload__{func.__qualname__}__'
func.__overloads__ = namespace.pop(key, [])
return func

def overload(func):
namespace = func.__globals__
key = f'__overload__{func.__qualname__}__'
namespace[key] = func.__overloads__ + [func.__annotations__]
return _overload_dummy

class Smudge(str):

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

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

@create_function
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)

@create_function
def other_method(self, x:str) -> tuple:
pass

print(f'{Smudge.__getitem__.__annotations__=}')
print(f'{Smudge.__getitem__.__overloads__=}')
print(f'{Smudge.other_method.__annotations__=}') 
print(f'{Smudge.other_method.__overloads__=}')

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45100] Teach help about typing.overload()

2021-09-05 Thread Diego Ramirez


Change by Diego Ramirez :


--
nosy: +DiddiLeija

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45100] Teach help about typing.overload()

2021-09-05 Thread Alex Waygood


Alex Waygood  added the comment:

There is a similar issue with `functools.singledispatch`

```
>>> from functools import singledispatch
>>> @singledispatch
... def flip(x: str) -> int:
... """Signature when given a string"""
... return int(x)
... 
>>> @flip.register
... def _(x: int) -> str:
... """Signature when given an int"""
... return str(x)
... 
>>> flip(5)
'5'
>>> flip('5')
5
>>> help(flip)
Help on function flip in module __main__:
flip(x: str) -> int
Signature when given a string
```

--
nosy: +AlexWaygood

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45100] Teach help about typing.overload()

2021-09-05 Thread Ken Jin


Change by Ken Jin :


--
nosy: +gvanrossum, kj

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45100] Teach help about typing.overload()

2021-09-05 Thread Ronald Oussoren


Ronald Oussoren  added the comment:

I agree that this would be nice to have, but wonder how help() could access 
that information.  The two @overload definitions will be overwritten by the 
non-overload one at runtime, and hence will ever been seen by help().

--
nosy: +ronaldoussoren

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45100] Teach help about typing.overload()

2021-09-04 Thread Raymond Hettinger


New submission from Raymond Hettinger :

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.

--
messages: 401052
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Teach help about typing.overload()

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com