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
function.__type_params__ and type.__type_params__ should be writable #104600
The PEP-695 implementation added a new attribute __type_params__ to functions. I made this field read-only, but I realized there is a use case for writing to it: functools.wraps, when wrapping a generic function, should add the .__type_params__ to the wrapper. Making it writable is also more consistent with other fields on functions, as even the __name__ of functions is writable.
The PEP also adds a __type_params__ attribute to classes and type aliases. For classes it's already writable (it's just stored in the type's __dict__). For type aliases it's readonly, but as I don't see a use case for mutating a type alias's __type_params__, I'd like to keep it that way.
For classes it's already writable (it's just stored in the type's __dict__).
Small correction: as noted in #104616, it's not actually writeable on class objects either at the moment (and probably should be, for similar reasons):
Running Debug|x64 interpreter...
Python 3.12.0a7+ (heads/main:cfa517d5a6, May 18 2023, 17:12:51) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo[T]: ...
...
>>> Foo.__type_params__ = ("bar",)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute '__type_params__' of 'type' objects is not writable
>>> classBar: ...
...
>>> Bar.__type_params__ = ("bar",)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute '__type_params__' of 'type' objects is not writable
changed the title [-]function.__type_params__ should be writable[/-][+]function.__type_params__ and type.__type_params__ should be writable[/+]on May 18, 2023
Good catch, I missed that the PEP explicitly said this. I think we can just update the PEP, cc @erictraut@gvanrossum. See my message above for the argument why these should be writable.
I expected some transmutation here but I guess in order to actually make the transmutation happen I need to update both wtfbbq.__code__.co_freevars and wtfbbq.__closure__?
Activity
pythongh-104600: Make function.__type_params__ writable
AlexWaygood commentedon May 18, 2023
Small correction: as noted in #104616, it's not actually writeable on class objects either at the moment (and probably should be, for similar reasons):
[-]function.__type_params__ should be writable[/-][+]function.__type_params__ and type.__type_params__ should be writable[/+]pythongh-104600: Make type.__type_params__ writable
gh-104600: Make function.__type_params__ writable (#104601)
gh-104600: Make type.__type_params__ writable (#104634)
Merge branch 'main' into gsallam/main
sobolevn commentedon Aug 16, 2023
Sorry, but now PEP is misleading:
But, since it is now writtable (on classes as well):
Shouldn't the PEP be updated? Or do you want to make this "implementation detail" private?
JelleZijlstra commentedon Aug 16, 2023
Good catch, I missed that the PEP explicitly said this. I think we can just update the PEP, cc @erictraut @gvanrossum. See my message above for the argument why these should be writable.
erictraut commentedon Aug 16, 2023
Sounds reasonable to me.
JelleZijlstra commentedon Aug 17, 2023
Reopening to remind myself to do that
PEP 695: __type_params__ attribute is writable
PEP 695: __type_params__ attribute is writable (#3457)
makslevental commentedon Apr 11, 2024
Sorry for commenting on a closed issue but can someone give me a quick word on why this is like this:
I expected some transmutation here but I guess in order to actually make the transmutation happen I need to update both
wtfbbq.__code__.co_freevarsandwtfbbq.__closure__?