Message413677
I made a similar suggestion in issue46821 (thanks Alex for pointing me to this older issue):
Currently, the implementation of @overload (https://github.com/python/cpython/blob/59585d6b2ea50d7bc3a9b336da5bde61367f527c/Lib/typing.py#L2211) simply returns a dummy function and throws away the decorated function. This makes it virtually impossible for type checkers using the runtime function object to find overloads specified at runtime.
In pyanalyze, I worked around this by providing a custom @overload decorator, working something like this:
_overloads: dict[str, list[Callable]] = {}
def _get_key(func: Callable) -> str:
return f"{func.__module__}.{func.__qualname__}"
def overload(func):
key = _get_key(func)
_overloads.setdefault(key, []).append(func)
return _overload_dummy
def get_overloads_for(func):
key = _get_key(func)
return _overloads.get(key, [])
A full implementation will need more error handling.
I'd like to add something like this to typing.py so that other tools can also use this information.
---
With my suggested solution, help() would need to call typing.get_overloads_for() to get any overloads for the function. Unlike Raymond's suggestion, we would not need to change the function creation machinery. |
|
Date |
User |
Action |
Args |
2022-02-21 20:27:20 | JelleZijlstra | set | recipients:
+ JelleZijlstra, gvanrossum, rhettinger, ronaldoussoren, kj, AlexWaygood, DiddiLeija |
2022-02-21 20:27:20 | JelleZijlstra | set | messageid: <1645475240.41.0.518216890478.issue45100@roundup.psfhosted.org> |
2022-02-21 20:27:20 | JelleZijlstra | link | issue45100 messages |
2022-02-21 20:27:20 | JelleZijlstra | create | |
|