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.update_wrapper does not work with type #119011

Open
nikola-benes opened this issue May 13, 2024 · 5 comments
Open

functools.update_wrapper does not work with type #119011

nikola-benes opened this issue May 13, 2024 · 5 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@nikola-benes
Copy link

nikola-benes commented May 13, 2024

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

@nikola-benes nikola-benes added the type-bug An unexpected behavior, bug, or error label May 13, 2024
@AlexWaygood AlexWaygood added topic-typing stdlib Python modules in the Lib dir labels May 13, 2024
@JelleZijlstra JelleZijlstra self-assigned this May 13, 2024
@sobolevn
Copy link
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

@AlexWaygood AlexWaygood added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels May 13, 2024
@dwt
Copy link

dwt commented May 16, 2024

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?

@AlexWaygood
Copy link
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

@mdkcore0
Copy link

mdkcore0 commented May 20, 2024

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 :)

@JelleZijlstra
Copy link
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.

sobolevn added a commit to sobolevn/cpython that referenced this issue May 21, 2024
mdkcore0 added a commit to mdkcore0/cpython that referenced this issue May 21, 2024
mdkcore0 added a commit to mdkcore0/cpython that referenced this issue May 21, 2024
mdkcore0 added a commit to mdkcore0/cpython that referenced this issue May 21, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 28, 2024
JelleZijlstra pushed a commit to JelleZijlstra/cpython that referenced this issue May 28, 2024
…ple (pythonGH-119296)

(cherry picked from commit 6b240c2)

Co-authored-by: Nikita Sobolev <[email protected]>
JelleZijlstra pushed a commit that referenced this issue May 28, 2024
JelleZijlstra added a commit that referenced this issue May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants