Message408136
> I tend to agree with Steven and David here. You define __getattribute__ and so that's the behaviour you get when an attribute of the class is requested (whether by the system or by your code).
I would agree if it was obvious or explicitly stated that isinstance looks up __class__ using the object's __getattribute__, and thus prone to cause side-effects. It wasn't obvious to me that isinstance would access any attributes from the object, but that it would rather get the object's type in a more direct way (at the C level for CPython).
> Do you have a real-world example of code that is broken by this behaviour, or is this just a theoretical problem?
I was looking at some profiling data for a real-life project (observability) that I'm working on. I was using a simple Django application as a target and noticed calls to a __getattribute__ (implemented by a Django class) that I wasn't expecting. All my code was doing was to check that a certain object was not of "callable" type using isinstance. Whilst I believe no side effects were caused by this particular instance of __getattribute__, it should be noted that "Django" is a variable here: any other target framework or library might have caused side effects in theory. The code that I want to execute must have guarantees of being side-effects-free, so using isinstance does not give me that. Currently, I have worked around this by using a custom, pure Python implementation of isinstance that gets __mro__ (via object.__getattribute__) and checks that type(obj) is an element of it (plus caching, for performance reasons). |
|
Date |
User |
Action |
Args |
2021-12-09 16:18:41 | Gabriele Tornetta | set | recipients:
+ Gabriele Tornetta, paul.moore, steven.daprano, r.david.murray, bup |
2021-12-09 16:18:41 | Gabriele Tornetta | set | messageid: <1639066721.22.0.721643276169.issue32683@roundup.psfhosted.org> |
2021-12-09 16:18:41 | Gabriele Tornetta | link | issue32683 messages |
2021-12-09 16:18:41 | Gabriele Tornetta | create | |
|