GeezerVison is showing github.com — function.__type_params__ and type.__type_params__ should be writable · Issue #104600 · python/cpython
function.__type_params__ and type.__type_params__ should be writable #104600DescriptionThe 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.
Linked PRs
ActivityAlexWaygood commented on May 18, 2023
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
>>> class Bar: ...
...
>>> Bar.__type_params__ = ("bar",)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute '__type_params__' of 'type' objects is not writableReacted by Jelle Zijlstra changed the title [-]function.__type_params__ should be writable[/-] [+]function.__type_params__ and type.__type_params__ should be writable[/+] on May 18, 2023sobolevn commented on Aug 16, 2023 Sorry, but now PEP is misleading:
A new read-only attribute called __type_params__ is available on generic classes, functions, and type aliases.
But, since it is now writtable (on classes as well):
>>> class A[X]: ...
...
>>> A.__type_params__
(X,)
>>> A.__type_params__ = ()
>>> A.__type_params__
()
Shouldn't the PEP be updated? Or do you want to make this "implementation detail" private? JelleZijlstra commented on 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 commented on Aug 16, 2023 JelleZijlstra commented on Aug 17, 2023 Reopening to remind myself to do that added a commit that references this issue on Oct 1, 2023This issue will close once commit 4e4357f is merged into the 'master' branch.4e4357f added a commit that references this issue on Oct 3, 2023This issue will close once commit e173bd0 is merged into the 'main' branch.e173bd0 makslevental commented on Apr 11, 2024 Sorry for commenting on a closed issue but can someone give me a quick word on why this is like this:
def wtfbbq[M, K, N]():
print(type(M), type(K), type(N))
wtfbbq.__type_params__ = (32, 32, 32)
wtfbbq()
>>> <class 'typing.TypeVar'> <class 'typing.TypeVar'> <class 'typing.TypeVar'>
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__?
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|