Skip to content

Since 3.6: TypeError: cannot create weak reference to '_Any' object #345

@Tinche

Description

@Tinche

Hi,

I'm the author of cattrs, which is an experimental library for converting Python data (to keep it simple, it's a little more complex than that, and docs are at rtd).

Anyway, cattrs uses typing.py for runtime conversions, along with singledispatch. Looks like the version of typing.py in 3.6 broke it in several ways. :) Here's the first:

>>> import typing, weakref
>>> weakref.ref(typing.Any)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create weak reference to '_Any' object

This used to work. It's a problem when used together with singledispatch, since singledispatch has a WeakKeyDictionary cache in it.

>>> import typing
>>> from functools import singledispatch
>>> s = singledispatch(lambda x: x)
>>> s.register(typing.Any, lambda x: x+1)
<function <lambda> at 0x10d89a268>
>>> s.dispatch(typing.Any)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/functools.py", line 776, in dispatch
    impl = dispatch_cache[cls]
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/weakref.py", line 365, in __getitem__
    return self.data[ref(key)]
TypeError: cannot create weak reference to '_Any' object

This is kind of unfortunate for the library. Any chance of this being fixed on this end?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions