asyncstdlib icon indicating copy to clipboard operation
asyncstdlib copied to clipboard

slot_get helper works incorrectly with metaclasses

Open maxfischer2781 opened this issue 4 years ago • 0 comments

As pointed out by @Aran-Fey, the slot_get helper does not work correctly:

The problem isn't inheritance, getting the dunder from a parent class works just fine with your getattr. The problem is that you could accidentally get it from the metaclass instead of the class

class Meta(type):
    def __int__(self): return 5

class Class(metaclass=Meta): pass

obj = Class()

print(slot_get(obj, '__int__')())  # 5
print(int(obj))  # TypeError

The helper is only used in contextlib to look up context manager special methods; all other special method accesses just use the . operator directly.
It should either be fixed and used more broadly (if performance permits) or abandoned altogether.

maxfischer2781 avatar Jan 11 '22 08:01 maxfischer2781