Skip to content

functools.update_wrapper does not work with type #119011

Closed

Description

@nikola-benes

Bug report

Bug description:

from functools import update_wrapper

def my_type(*args): pass

t = update_wrapper(my_type, type)

This works on Python ≤ 3.11, and I think this is expected because the docs for update_wrapper claim that it may be used with callables other than functions.

However, with 3.12, I get

TypeError: __type_params__ must be set to a tuple

(Similarly for functools.wraps.)

The problem is that update_wrapper includes __type_params__ by default (which is undocumented, see related issue) and type.__type_params__ is a descriptor.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. sobolevn commented on May 13, 2024

    @sobolevn
    Member

    3.11:

    Python 3.11.5 (main, Sep  5 2023, 10:34:31) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from functools import update_wrapper
    >>> 
    >>> def my_type(*args): pass
    ... 
    >>> t = update_wrapper(my_type, type)
    >>> 
    >>> t
    <function type at 0x104963060>
    

    3.12+:

    >>> from functools import update_wrapper
    ... 
    ... def my_type(*args): pass
    ... 
    ... t = update_wrapper(my_type, type)
    Traceback (most recent call last):
      File "<python-input-8>", line 5, in <module>
        t = update_wrapper(my_type, type)
      File "/Users/sobolev/Desktop/cpython2/Lib/functools.py", line 57, in update_wrapper
        setattr(wrapper, attr, value)
        ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
    TypeError: __type_params__ must be set to a tuple
    
  2. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on May 13, 2024
  3. dwt commented on May 16, 2024

    @dwt

    This was reported to me at dwt/fluent#14

    I'm seeing this when wrapping type() to make it useable in a fluent way.

    Is there something I need to do differently for python 3.12, or is this a bug in the python stdlib?

  4. AlexWaygood commented on May 16, 2024

    @AlexWaygood
    Member

    Is there something I need to do differently for python 3.12, or is this a bug in the python stdlib?

    This is a CPython bug

  5. mdkcore0 commented on May 20, 2024

    @mdkcore0
    Contributor

    hi @AlexWaygood, I'm trying to figure out what is happening here; what CPython should do with __type_params__? Should it be skipped?

    @encukou is also helping me to understand it :)

  6. JelleZijlstra commented on May 20, 2024

    @JelleZijlstra
    Member

    Some notes from in-person conversation:

    The problem is this:

    >>> type.__type_params__
    <attribute '__type_params__' of 'type' objects>
    

    We would instead want this to be an empty tuple, because that's the normal behavior for classes that are not PEP 695 generics.

    The way to achieve this would be to use a special case for &PyType_Type in the type_get_type_params getter. There is a similar check in the __abstractmethods__ getter.

  7. added a commit that references this issue on May 21, 2024
  8. added 3 commits that reference this issue on May 21, 2024
  9. added a commit that references this issue on May 28, 2024
  10. added a commit that references this issue on May 28, 2024
  11. added 3 commits that reference this issue on May 28, 2024
  12. encukou commented on May 30, 2024

    @encukou
    Member

    Is there more to do here?

  13. dwt commented on Jun 5, 2024

    @dwt

    Sorry if this is daft, but could someone help me understand how I can find out what python release this fix will be part of?

  14. AlexWaygood commented on Jun 5, 2024

    @AlexWaygood
    Member

    The fix will be included in 3.13.0b2 and 3.12.4. I think both should be coming pretty soon.

  15. added a commit that references this issue on Jul 11, 2024
  16. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions