Fixed infinite recursion when pytypes tries to access .__orig_class__ on an instance of a @typechecked class with __getattr__/__getattribute__#53
Conversation
…ed class with `__getattr[ibute]__`
__orig_class__ on an object of a @typecked class with __getattr[ibute]__|
Hey, thanks for working on this. I came to the conclusion that Python allows so much overriding, monkeypatching, wrapping of all sorts of stuff that we probably never get rid of such issues completely. |
(at least on my machine - Windows 10, Python 3.5)
|
Done! As you asked, the tests break before and work after (at least on my machine - is it possible to make Travis run the new tests on an older version?) |
|
I had to modify this slightly to fit into some changes I had made towards Python 3.7 support. Also moved your new function to a position where I find it belongs best. |
Hi! I hit this problem yesterday and dived in to fix it because I was excited to use
pytypesin my library :)My environment is Python 3.5 running inside
pipenvon Windows, and a fresh install ofpytypes==1.0b5.post2+dirty.Here's a simple case that should demonstrate the problematic behavior:
On an unwrapped class,
bad.onewould result inThing.__getattr__(bad, 'one')and work fine.When wrapped with
@typechecked, it becomes beThing.checker_tp<__getattr__>(bad, 'one'), wherechecker_tp<__getattr__>is thechecker_tp(frompytypes.typechecker._typinspect_func) that wraps__getattr__.Unfortunately, in its code,
checker_tp<__getattr__>does this:We're in a method call, so
args_kw[0]isself– ourbadobject. Butbad.__orig_class__– a dot-access on a nonexistent attribute – ends up callingchecker_tp<__getattr__>(bad, '__orig_class__')again, which tries to get the__orig_class__again, and so on until it hits aRecursionError.The way I solve this in this pull request is simple –
Use
object.__getattribute__(x, '__orig_class__')instead, thus bypassing a classes'__getattr__and__getattribute__. I'm not familiar withpytypes' internals, So I don't know what consequences this could have; I imagine it might lead to unexpected behaviour on proxy-like classes.Ideas for alternative solutions (FWIW from a person unfamiliar with the code):
Wrap
__getattr__/__getattribute__, but special-case them somehow so thatpytypesinternals know they should use the unwrapped version instead. I'm pretty fuzzy on the details, but it seems plausible.Don't wrap
__getattr__/__getattribute__at all.Unfortunately if the original
__getattr[ibute]__returns a method, an un-wrapped method would 'leak' out. But I guess this could happen with any method, so maybe it's not a solvable problem anyway.(I guess in that case
pytypescould do identity checks with known methods from the class dict, and returning the appropriate wrapped method instead if possible. But that'd require wrapping__getattr[ibute]__, which we're trying to avoid.)I hope this wasn't too long winded. Please tell me what you think about this solution!