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

Question:

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)

`

Asked By: zakharchik

||

Answers:

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']
Answered By: kconsiglio
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.