Skip to content

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

Closed
python/peps
#3457

Description

@JelleZijlstra

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

Activity

  1. added
    type-feature
    on May 18, 2023
  2. added a commit that references this issue on May 18, 2023
  3. added
    3.12
    on May 18, 2023
  4. AlexWaygood commented on May 18, 2023

    @AlexWaygood
    Member

    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
  5. changed the title [-]function.__type_params__ should be writable[/-] [+]function.__type_params__ and type.__type_params__ should be writable[/+] on May 18, 2023
  6. added 3 commits that reference this issue on May 18, 2023
  7. added a commit that references this issue on May 20, 2023
  8. sobolevn commented on Aug 16, 2023

    @sobolevn
    Member

    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?

  9. JelleZijlstra commented on Aug 16, 2023

    @JelleZijlstra
    MemberAuthor

    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.

  10. erictraut commented on Aug 16, 2023

    @erictraut
    Contributor

    Sounds reasonable to me.

  11. JelleZijlstra commented on Aug 17, 2023

    @JelleZijlstra
    MemberAuthor

    Reopening to remind myself to do that

  12. added a commit that references this issue on Oct 1, 2023
    4e4357f
  13. added a commit that references this issue on Oct 3, 2023
    e173bd0
  14. makslevental commented on Apr 11, 2024

    @makslevental

    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__?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12topic-typingtype-feature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions