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

singledispatch support for type annotations #76408

Closed
ambv opened this issue Dec 5, 2017 · 8 comments
Closed

singledispatch support for type annotations #76408

ambv opened this issue Dec 5, 2017 · 8 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ambv
Copy link
Contributor

ambv commented Dec 5, 2017

BPO 32227
Nosy @gvanrossum, @rhettinger, @ambv, @1st1, @ilevkivskyi, @csabella
PRs
  • bpo-32227: functools.singledispatch supports registering via type annotations #4733
  • bpo-27984: explictly check type of singledispatch.dispatch() argument #6114
  • 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/ambv'
    closed_at = <Date 2018-03-14.23:34:12.078>
    created_at = <Date 2017-12-05.21:24:12.510>
    labels = ['3.7', 'type-feature', 'library']
    title = 'singledispatch support for type annotations'
    updated_at = <Date 2018-03-14.23:34:12.077>
    user = 'https://github.com/ambv'

    bugs.python.org fields:

    activity = <Date 2018-03-14.23:34:12.077>
    actor = 'lukasz.langa'
    assignee = 'lukasz.langa'
    closed = True
    closed_date = <Date 2018-03-14.23:34:12.078>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2017-12-05.21:24:12.510>
    creator = 'lukasz.langa'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32227
    keywords = ['patch']
    message_count = 8.0
    messages = ['307686', '307690', '307691', '307692', '307693', '308075', '311565', '311599']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'rhettinger', 'lukasz.langa', 'yselivanov', 'levkivskyi', 'cheryl.sabella']
    pr_nums = ['4733', '6114']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32227'
    versions = ['Python 3.7']

    @ambv
    Copy link
    Contributor Author

    ambv commented Dec 5, 2017

    With the patch attached to this issue, @singledispatch gains support for passing the type in @register via annotations.

    This looks more natural and enables more thorough type checking without repeating yourself:

    @singledispatch
    def generic(arg): ...
    
    @generic.register
    def _(arg: str): ...
    
    @generic.register
    def _(arg: int): ...

    The previous API is still available for backwards compatibility, as well as stacking, and use with classes (sic, I was surprised to learn it's used that way, too).

    The patch should be uncontroversial, maybe except for the fact that it's importing the typing module if annotations are used. This is necessary because of forward references, usage of None as a type, and so on. More importantly, with PEP-563 it's mandatory.

    @ambv ambv added the 3.7 (EOL) end of life label Dec 5, 2017
    @ambv ambv self-assigned this Dec 5, 2017
    @ambv ambv added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Dec 5, 2017
    @ambv
    Copy link
    Contributor Author

    ambv commented Dec 5, 2017

    Guido, I know that you have a lot on your plate right now. Adding you because of PEP-443 and PEP-484.

    @ilevkivskyi
    Copy link
    Member

    Idea looks interesting (like a basic runtime @overload). My expectation is that some changes are necessary in mypy for this to work properly.

    Another (minor) problem is that this creates a circular dependency functools <-> typing.

    @ilevkivskyi
    Copy link
    Member

    Oops something wrong happened.

    @ambv
    Copy link
    Contributor Author

    ambv commented Dec 5, 2017

    this creates a circular dependency functools <-> typing

    Well, it doesn't since I explicitly import typing inside singledispatch. By the time this import happens, functools is fully imported. This would only be a problem if functools itself tried to use singledispatch with annotations. Not impossible but super unlikely.

    @ambv
    Copy link
    Contributor Author

    ambv commented Dec 11, 2017

    New changeset e569753 by Łukasz Langa in branch 'master':
    bpo-32227: functools.singledispatch supports registering via type annotations (bpo-4733)
    e569753

    @csabella
    Copy link
    Contributor

    csabella commented Feb 3, 2018

    Hello,

    I believe this could be closed as resolved? Do you think it should be added to the What's New? page for 3.7?

    @ilevkivskyi
    Copy link
    Member

    Do you think it should be added to the What's New? page for 3.7?

    I leave this up to Łukasz.

    @ambv ambv closed this as completed Mar 14, 2018
    @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.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants