Fixed infinite recursion when pytypes tries to access .__orig_class__ on an instance of a @typechecked class with __getattr__/__getattribute__ #53
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! I hit this problem yesterday and dived in to fix it because I was excited to use
pytypes
in my library :)My environment is Python 3.5 running inside
pipenv
on 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.one
would 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
– ourbad
object. 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 thatpytypes
internals 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
pytypes
could 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!