Message401094
> 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__=}') |
|
Date |
User |
Action |
Args |
2021-09-05 16:14:44 | rhettinger | set | recipients:
+ rhettinger, gvanrossum, ronaldoussoren, kj, AlexWaygood, DiddiLeija |
2021-09-05 16:14:44 | rhettinger | set | messageid: <1630858484.4.0.260515886029.issue45100@roundup.psfhosted.org> |
2021-09-05 16:14:44 | rhettinger | link | issue45100 messages |
2021-09-05 16:14:44 | rhettinger | create | |
|