When a 3rd party library method uses isinstance() to compare an object with a class, it returns False because it compares the fully qualified class name of the object with a qualified class name that starts "higher" up.
E.g.: isinstance() finds that the object class and classname differ:
Expected:
'network.mhistory.service.mhistory_messages.MHistoryActivityViewMessage'
Found:
'backend.network.mhistory.service.mhistory_messages.MHistoryActivityViewMessage'
and returns False given the code snippet:
if not isinstance(value, self.type):
raise ValidationError('Expected type %s for field %s, '
'found %s (type %s)' %
(self.type, name, value, type(value)))
Is there a way to change the fully qualified name of a class (at least temporarily)?
As far as Python is concerned, the classes network.mhistory.service.mhistory_messages.MHistoryActivityViewMessage and backend.network.mhistory.service.mhistory_messages.MHistoryActivityViewMessage are not the same. That's true even if they have exactly the same definition because they were read from the same file!
Your bug isn't that isinstance is returning the "wrong" answer, it's that you're able to access those two classes (and possibly others as well) by two different names.
There are likely two different problems leading to the bug. First, you've probably got some code somewhere that is messing around with sys.path. That isn't inherently bad, but it's causing you problems by making the contents of your backend package available two differnet ways, first directly (e.g. import network) and via backend (from backend import network). You don't want this.
The second part of your bug (which may have been the motivating factor leading to the first part), is that you're actually using both ways of accessing those objects. You only need one, and should thus fix the parts that import the package the wrong way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With