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

functools.WRAPPER_ASSIGNMENTS should include __annotations__ #53060

Closed
terrence mannequin opened this issue May 24, 2010 · 13 comments
Closed

functools.WRAPPER_ASSIGNMENTS should include __annotations__ #53060

terrence mannequin opened this issue May 24, 2010 · 13 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@terrence
Copy link
Mannequin

terrence mannequin commented May 24, 2010

BPO 8814
Nosy @rhettinger, @pitrou, @merwok, @david-caro
PRs
  • gh-85403: Make wraps retain type annotations #21392
  • Files
  • functools-wrapper-assign-annotations.diff: Patch to add annotations to WRAPPER_ASSIGNMENTS
  • functools-wrapper-assign-annotations.diff: Improved patch with documentation fixed to match.
  • functools-wrapper-assign-annotations.diff: Improved patch with documentation and tests
  • 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/rhettinger'
    closed_at = <Date 2010-08-08.00:57:27.438>
    created_at = <Date 2010-05-24.21:20:57.698>
    labels = ['type-bug', 'library']
    title = 'functools.WRAPPER_ASSIGNMENTS should include __annotations__'
    updated_at = <Date 2020-07-08.09:16:01.461>
    user = 'https://bugs.python.org/terrence'

    bugs.python.org fields:

    activity = <Date 2020-07-08.09:16:01.461>
    actor = 'David Caro'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2010-08-08.00:57:27.438>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2010-05-24.21:20:57.698>
    creator = 'terrence'
    dependencies = []
    files = ['17453', '17454', '17694']
    hgrepos = []
    issue_num = 8814
    keywords = ['patch']
    message_count = 13.0
    messages = ['106394', '106396', '106402', '107759', '107974', '112851', '112852', '112856', '112858', '112860', '112874', '112903', '113220']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'pitrou', 'eric.araujo', 'terrence', 'David Caro']
    pr_nums = ['21392']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue8814'
    versions = ['Python 3.1', 'Python 3.2']

    @terrence
    Copy link
    Mannequin Author

    terrence mannequin commented May 24, 2010

    __annotations__ should be included in the set of attributes copied by default to a wrapped method.

    An example of the problem:
    >>> from functools import wraps
    >>> def mydecorator(fn):
    ...     @wraps(fn)
    ...     def inner(*args, **kwargs):
    ...         return fn(*args, **kwargs)
    ...     return inner
    ...
    >>> @mydecorator
    ... def foo(a:int, b:str):
    ...     pass
    ... 
    >>> foo.__annotations__
    {}
    
    With the included fix:
    >>> foo.__annotations__
    {'a': <class 'int'>, 'b': <class 'str'>}

    @terrence terrence mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 24, 2010
    @merwok
    Copy link
    Member

    merwok commented May 24, 2010

    The patch is fine. Can you check if some doc has to be updated to mention that? (files under Doc and docstrings in functools.py).

    Removing 3.1 since this is a new feature, and 3.3 since 3.2 is not frozen.

    @terrence
    Copy link
    Mannequin Author

    terrence mannequin commented May 24, 2010

    Thank you for looking it over! The updated patch adds __annotations__ to the documentation where the value of WRAPPER_ASSIGNMENTS is given. I think it would be nice if the documentation showed WRAPPER_ASSIGNMENTS and WRAPPER_UPDATES in their own section, but that would probably merit a separate issue to fix, if it is even worth bothering with.

    @merwok
    Copy link
    Member

    merwok commented Jun 13, 2010

    Patch looks good to me. Could you add some tests to Lib/test/test_functools.py to check that annotations are really copied?

    (3.2 is not frozen, adjusting versions)

    @terrence
    Copy link
    Mannequin Author

    terrence mannequin commented Jun 17, 2010

    Alright, I've added several tests. I also modified update_wrapper to not copy missing attributes (like __annotations__ on builtin methods) -- see bpo-1576241.

    (I also finally see what you mean with removing 3.3 from the versions list. Sorry I didn't grok that before.)

    @terrence
    Copy link
    Mannequin Author

    terrence mannequin commented Aug 4, 2010

    Is there still a chance to get this fix in 3.2?

    @pitrou
    Copy link
    Member

    pitrou commented Aug 4, 2010

    The patch is now committed in py3k (r83731). Thanks for your contribution!

    @pitrou pitrou closed this as completed Aug 4, 2010
    @rhettinger
    Copy link
    Contributor

    This should probably be backported to 3.1

    @pitrou
    Copy link
    Member

    pitrou commented Aug 4, 2010

    This should probably be backported to 3.1

    Well, I think this is technically a new feature. If someone wants to
    backport it, then fine, but I don't think there's much point in doing
    so.

    @rhettinger
    Copy link
    Contributor

    Okay, I'll do it.

    The intention of wrapper assignments was to wrap all of the standard attributes, so the omission of __annotations__ is a bug.

    @rhettinger rhettinger reopened this Aug 4, 2010
    @rhettinger rhettinger self-assigned this Aug 4, 2010
    @terrence
    Copy link
    Mannequin Author

    terrence mannequin commented Aug 4, 2010

    Awesome! I greatly appreciate the 3.1 backport, as that's what I'm going to be using for the foreseeable future.

    @merwok
    Copy link
    Member

    merwok commented Aug 4, 2010

    Thanks for sticking with this Terrence, it fell off my radar. Thanks Antoine for the commit.

    @rhettinger
    Copy link
    Contributor

    Backported to Py3.1 in r83807

    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants