Skip to content

3.12 regression: cannot create weak reference to 'typing.TypeVar' object #106403

Closed

Description

@encukou

This works in Python 3.11, but fails in 3.12b3:

import weakref
import typing
weakref.ref(typing.TypeVar('T'))

I'm assuming it's an unintentional side effect of the PEP-695 implementation? Unfortunately I don't have time to investigate right now, let me know if I should.

Reported in cloudpipe/cloudpickle#507

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jul 4, 2023
  2. JelleZijlstra commented on Jul 4, 2023

    @JelleZijlstra
    Member

    Fixing.

    The new typing.TypeAliasType is also not weakrefable:

    >>> type T = int
    >>> weakref.ref(T)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot create weak reference to 'typing.TypeAliasType' object
    

    But neither is the existing UnionType:

    >>> weakref.ref(int | str)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot create weak reference to 'types.UnionType' object
    

    So maybe that's fine?

  3. added a commit that references this issue on Jul 4, 2023
  4. ogrisel commented on Jul 6, 2023

    @ogrisel
    Contributor

    Thanks @JelleZijlstra!

    So maybe that's fine?

    I suspect that this might cause problems with cloudpickle when executing interactively defined functions with type annotations in their signatures on remote Python workers (e.g. in Dask, Ray or PySpark / databricks).

    I did not have time to investigate the details myself yet but @musicinmybrain tried to test your fix in cloudpickle in cloudpipe/cloudpickle#507 (comment) and there are still segfaults to debug.

  5. added a commit that references this issue on Jul 11, 2023
  6. added a commit that references this issue on Jul 11, 2023
  7. JelleZijlstra commented on Jul 11, 2023

    @JelleZijlstra
    Member

    I merged my PR that re-adds weakref support for TypeVars. I haven't tried to debug the segfaults in the cloudpickle tests, but let me know if those can be minified to something that shows the issue is in CPython.

  8. added a commit that references this issue on Jul 11, 2023
  9. Yhg1s commented on Jul 31, 2023

    @Yhg1s
    Member

    Closing this as the original report is (believed to be) fixed. If the other cloudpickle issues are caused by bugs in 3.12 it probably deserves its own issue.

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

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions