GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.04k stars 230 forks source link

doesn't support isinstance check #201

Closed hongweipeng closed 2 years ago

hongweipeng commented 2 years ago
import wrapt

@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):
    return wrapped(*args, **kwargs)

@wrapper
class C1():
    def method(self):
        pass

print(isinstance(C1(), C1))  # output: False

Is it expected?

GrahamDumpleton commented 2 years ago

What version of Python are you using?

I can't remember off hand if there is a limitation around this with certain Python versions. I would need to go through and see if it is covered by tests.

Decorators on classes do present some problems which necessitates accessing the wrapped class.

As a workaround, this should work:

isinstance(C1(), C1.__wrapped__)

It doesn't seem to cover this specific issue, but some other problems with decorated classes can be found mentioned in:

hongweipeng commented 2 years ago

I use python 3.8 and I think the check can be done by setting __instancecheck__.

GrahamDumpleton commented 2 years ago

Added in 1.14.0.