GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #104600

New issue
Copy link
New issue
Copy link
Closed
python/peps
#3457
python/peps#3457
Closed
function.__type_params__ and type.__type_params__ should be writable#104600
python/peps
#3457
Copy link
Assignees
JelleZijlstra
Labels
3.12only security fixesonly security fixestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@JelleZijlstra
JelleZijlstra
opened on May 18, 2023
Issue body actions

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

  • gh-104600: Make function.__type_params__ writable #104601
  • gh-104600: Make type.__type_params__ writable #104634
Reacted by Alex Waygood

Activity

  1. JelleZijlstra
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-typing
    on May 18, 2023
  2. JelleZijlstra
    self-assigned this
    on May 18, 2023
  3. JelleZijlstra
    added a commit that references this issue on May 18, 2023

    pythongh-104600: Make function.__type_params__ writable

    7c04199
  4. AlexWaygood
    added
    3.12only security fixesonly security fixes
    on May 18, 2023
  5. AlexWaygood commented on May 18, 2023

    @AlexWaygood
    AlexWaygood
    on May 18, 2023
    Member
    More actions

    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
    Reacted by Jelle Zijlstra
  6. JelleZijlstra
    changed the title [-]function.__type_params__ should be writable[/-] [+]function.__type_params__ and type.__type_params__ should be writable[/+] on May 18, 2023
  7. JelleZijlstra
    added 3 commits that reference this issue on May 18, 2023

    pythongh-104600: Make type.__type_params__ writable

    41b9279

    gh-104600: Make function.__type_params__ writable (#104601)

    3fadd7d

    gh-104600: Make type.__type_params__ writable (#104634)

    8f1f3b9
  8. JelleZijlstra
    closed this as completedon May 20, 2023
  9. carljm
    added a commit that references this issue on May 20, 2023

    Merge branch 'main' into gsallam/main

    ea5965a
  10. sobolevn commented on Aug 16, 2023

    @sobolevn
    sobolevn
    on Aug 16, 2023
    Member
    More actions

    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?

  11. JelleZijlstra commented on Aug 16, 2023

    @JelleZijlstra
    JelleZijlstra
    on Aug 16, 2023
    MemberAuthor
    More actions

    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.

    Reacted by Alex Waygood
  12. erictraut commented on Aug 16, 2023

    @erictraut
    erictraut
    on Aug 16, 2023
    Contributor
    More actions

    Sounds reasonable to me.

  13. JelleZijlstra commented on Aug 17, 2023

    @JelleZijlstra
    JelleZijlstra
    on Aug 17, 2023
    MemberAuthor
    More actions

    Reopening to remind myself to do that

  14. JelleZijlstra
    reopened this on Aug 17, 2023
  15. JelleZijlstra
    added a commit that references this issue on Oct 1, 2023

    PEP 695: __type_params__ attribute is writable

    This issue will close once commit 4e4357f is merged into the 'master' branch.
    4e4357f
  16. JelleZijlstra
    closed this as completedin peps#3457on Oct 3, 2023
  17. JelleZijlstra
    added a commit that references this issue on Oct 3, 2023

    PEP 695: __type_params__ attribute is writable (#3457)

    This issue will close once commit e173bd0 is merged into the 'main' branch.
    e173bd0
  18. makslevental commented on Apr 11, 2024

    @makslevental
    makslevental
    on Apr 11, 2024
    More actions

    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

  • @JelleZijlstra
    JelleZijlstra

Labels

3.12only security fixesonly security fixestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.