I have some code that blows up at runtime but not in the debugger:
if type(json_thing) is dict:
Exception has occurred: UnboundLocalError
cannot access local variable ‘dict’ where it is not associated with a value
In the debugger it resolves:
type(json_thing)
<class 'list'>
type(json_thing[0])
<class 'dict'>
type(json_thing[0]) is dict
True
type(json_thing) is dict
False
so why can I not reference type(x) is dict
in my code?
the context is that I need a bullet proof json printer and been having problems with (langchain) classes missing serialization methods.
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
elif type(json_thing) is dict:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
elif type(json_thing) is list and type(json_thing[0] is Document):
# List(Document):
dict = [doc.to_json() for doc in json_thing]
print(json.dumps(dict, sort_keys=sort, indent=indents))
elif type(json_thing) is Document:
dict = json_thing.to_json()
print(json.dumps(dict, sort_keys=sort, indent=indents))