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

function.__type_params__ and type.__type_params__ should be writable #104600

Open
JelleZijlstra opened this issue May 18, 2023 · 1 comment
Open
Assignees
Labels
3.12 new features, bugs and security fixes topic-typing type-feature A feature request or enhancement

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented May 18, 2023

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.

Linked PRs

@AlexWaygood
Copy link
Member

AlexWaygood commented 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 writable

@JelleZijlstra JelleZijlstra changed the title function.__type_params__ should be writable function.__type_params__ and type.__type_params__ should be writable May 18, 2023
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 new features, bugs and security fixes topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants