Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introspection support for typing.overload #90977

Closed
JelleZijlstra opened this issue Feb 21, 2022 · 3 comments
Closed

Introspection support for typing.overload #90977

JelleZijlstra opened this issue Feb 21, 2022 · 3 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@JelleZijlstra
Copy link
Member

BPO 46821
Nosy @gvanrossum, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood
Superseder
  • bpo-45100: Improve help() by making typing.overload() information accessible at runtime
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/JelleZijlstra'
    closed_at = <Date 2022-02-21.20:23:36.411>
    created_at = <Date 2022-02-21.19:58:17.232>
    labels = ['type-feature', 'library', '3.11']
    title = 'Introspection support for typing.overload'
    updated_at = <Date 2022-02-21.20:23:36.410>
    user = 'https://github.com/JelleZijlstra'

    bugs.python.org fields:

    activity = <Date 2022-02-21.20:23:36.410>
    actor = 'JelleZijlstra'
    assignee = 'JelleZijlstra'
    closed = True
    closed_date = <Date 2022-02-21.20:23:36.411>
    closer = 'JelleZijlstra'
    components = ['Library (Lib)']
    creation = <Date 2022-02-21.19:58:17.232>
    creator = 'JelleZijlstra'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46821
    keywords = []
    message_count = 3.0
    messages = ['413671', '413675', '413676']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '45100'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46821'
    versions = ['Python 3.11']

    @JelleZijlstra
    Copy link
    Member Author

    Currently, the implementation of @overload (

    return _overload_dummy
    ) 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.

    @JelleZijlstra JelleZijlstra added the 3.11 only security fixes label Feb 21, 2022
    @JelleZijlstra JelleZijlstra self-assigned this Feb 21, 2022
    @JelleZijlstra JelleZijlstra added stdlib Python modules in the Lib dir type-feature A feature request or enhancement 3.11 only security fixes labels Feb 21, 2022
    @JelleZijlstra JelleZijlstra self-assigned this Feb 21, 2022
    @JelleZijlstra JelleZijlstra added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 21, 2022
    @AlexWaygood
    Copy link
    Member

    Discussion of similar ideas in bpo-45100

    @JelleZijlstra
    Copy link
    Member Author

    Thanks! Closing this as a duplicate.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants