Message 401094 - Python tracker (original) (raw)
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=}')