You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
functools.update_wrapper does not work with type #119011
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.
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
>>> 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.
Activity
sobolevn commentedon May 13, 2024
3.11:
3.12+:
dwt commentedon 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 commentedon May 16, 2024
This is a CPython bug
mdkcore0 commentedon 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 commentedon May 20, 2024
Some notes from in-person conversation:
The problem is this:
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_Typein thetype_get_type_paramsgetter. There is a similar check in the__abstractmethods__getter.pythongh-119011: `type.__type_params__` now return an empty tuple
pythongh-119011: Return an empty tuple on type_get_type_params as spe…
pythongh-119011: Return an empty tuple on type_get_type_params as spe…
pythongh-119011: Return an empty tuple on type_get_type_params as spe…
gh-119011: `type.__type_params__` now return an empty tuple (#119296)
pythongh-119011: `type.__type_params__` now return an empty tuple (py…
[3.12] pythongh-119011: `type.__type_params__` now return an empty tu…
[3.13] gh-119011: `type.__type_params__` now return an empty tuple (G…
[3.12] gh-119011: `type.__type_params__` now return an empty tuple (G…
encukou commentedon May 30, 2024
Is there more to do here?
dwt commentedon Jun 5, 2024
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?
AlexWaygood commentedon Jun 5, 2024
The fix will be included in 3.13.0b2 and 3.12.4. I think both should be coming pretty soon.
pythongh-119011: `type.__type_params__` now return an empty tuple (py…
pythongh-119011: `type.__type_params__` now return an empty tuple (py…
[Dynamo] Require Python 3.12.4+ for the type.__type_params__ check