Typing: create weak reference when doing issubclass check

Created on 15 Sep 2017  路  5Comments  路  Source: python/typing

The following code:

from typing import *

A = TypeVar('A')
class Foo(Generic[A]):
    pass

issubclass((Foo,), Foo)

fails with:

TypeError: cannot create weak reference to 'tuple' object

I think this is a bug.

Most helpful comment

To be clear, issubclass(x, Y) where x is not a class is always an error in the source code. The bug here is that you get a weird error rather than the regular TypeError: issubclass() arg 1 must be a class. But your code is wrong in either case (and it's still a TypeError).

All 5 comments

The only bug here is the strange error message. Both issubclass(1, int) and issubclass([], collections.abc.Iterable) fail with TypeError (the error message in the second case is also weird). The reason I think is that ABCMeta tries to add (Foo,) to the negative cache, which is implemented as a WeakSet.

I mean, the bug is the error, no? It is coming from caching, not from type check itself. Maybe negative cache should be implemented so that if weak ref cannot be set, then cache is not used, but not that it explodes.

To be clear, issubclass(x, Y) where x is not a class is always an error in the source code. The bug here is that you get a weird error rather than the regular TypeError: issubclass() arg 1 must be a class. But your code is wrong in either case (and it's still a TypeError).

Oh, of course. I somehow missed that here (code was just automatically checking things).

I just checked on Python 3.8 the error is much better: TypeError: issubclass() arg 1 must be a class. So I think this can be closed now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gvanrossum picture gvanrossum  路  8Comments

gvanrossum picture gvanrossum  路  5Comments

scherrey picture scherrey  路  3Comments

saulshanabrook picture saulshanabrook  路  6Comments

mkurnikov picture mkurnikov  路  6Comments