Home > database >  TypeError: 'bool' object is not callable in python module tinydb
TypeError: 'bool' object is not callable in python module tinydb

Time:11-29

I have error TypeError: 'bool' object is not callable, when try to use function search in tinydb

My code:

from tinydb import TinyDB, Query
db = TinyDB('db.json')
User = Query()
db.insert({'test': 'signs', 'age': 34})
res = db.search(User.test == 'signs')
print(res)

`

CodePudding user response:

It appears that "test" is a built-in function for a Query object. Changing test to name or anything else will fix issue.

from tinydb import TinyDB, Query
db = TinyDB('db.json')
User = Query()
db.insert({'name': 'signs', 'age': 34})
res = db.search(User.name == "signs")
print(f"the search: {res}")
print(f"User.test: {User.test}")

Output

the search: [{'name': 'signs', 'age': 34}]
User.test: <bound method Query.test of Query()>

You can also see 'test' listed as an attribute of a Query object buy running dir() on the object you create

>>> from tinydb import TinyDB, Query
>>> User = Query()
>>> dir(User)
['__and__', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__invert__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_generate_test', '_hash', '_path', '_test', 'all', 'any', 'exists', 'fragment', 'is_cacheable', 'map', 'matches', 'noop', 'one_of', 'search', 'test']
  • Related