This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Introspection support for typing.overload
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Improve help() by making typing.overload() information accessible at runtime
View: 45100
Assigned To: JelleZijlstra Nosy List: AlexWaygood, JelleZijlstra, gvanrossum, kj, sobolevn
Priority: normal Keywords:

Created on 2022-02-21 19:58 by JelleZijlstra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg413671 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-02-21 19:58
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.
msg413675 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2022-02-21 20:20
Discussion of similar ideas in Issue45100
msg413676 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-02-21 20:23
Thanks! Closing this as a duplicate.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90977
2022-02-21 20:23:36JelleZijlstrasetstatus: open -> closed
superseder: Improve help() by making typing.overload() information accessible at runtime
messages: + msg413676

resolution: duplicate
stage: resolved
2022-02-21 20:20:02AlexWaygoodsetmessages: + msg413675
2022-02-21 19:58:17JelleZijlstracreate