Raymond Hettinger <[email protected]> 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 <[email protected]>
<https://bugs.python.org/issue45100>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com