1

I wonder why the jsonpickle-module when consecutively applying or calling encode & decode does not pass the isinstance(...) check in Python 3.8.

Let say i have a simple class Person.

Here some code to illustrate what i mean:

import jsonpickle


class Person:
    id: int = -1
    name: str = "John Doe"

    def __init__(self, pId: int = None, name: str = None) -> None:
        self.id = (pId, self.id)[pId is None]
        self.name = (name, self.name)[name is None]


testInstance = Person()
testInstanceJSON = jsonpickle.encode(testInstance, unpicklable=True, make_refs=True)
print(testInstanceJSON)
testInstanceObject = jsonpickle.decode(testInstanceJSON)
print(testInstanceObject)
print(isinstance(testInstanceObject, Person.__class__))

It returns False on the last print-command!

Manifest Man
  • 873
  • 10
  • 16

1 Answers1

1

The attribute __class__ of an object provides the class the object is an instance of.

Classes like Person are also objects and instances of type.

This means that

isinstance(testInstanceObject, Person.__class__)

is the same as

isinstance(testInstanceObject, type)

but of course testInstanceObject is not an instance of type.

Change it to

isinstance(testInstanceObject, Person)

to check if testInstanceObject is an instance of Person.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25