Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask view raises TypeError: 'bool' object is not callable

I am trying to debug a view in my Flask app that is return a 500 status with the error TypeError: 'bool' object is not callable in the traceback. The view calls login_user from Flask-Login then returns True to indicate that the login was successful.

I have debugged until app_iter = app(environ, start_response) and the app is now a boolean with the value True rather than the Flask app object.

Traceback (most recent call last):
  File "D:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "D:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\Python27\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "D:\Python27\lib\site-packages\flask\app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "D:\Python27\lib\site-packages\werkzeug\wrappers.py", line 824, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "D:\Python27\lib\site-packages\werkzeug\wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "D:\Python27\lib\site-packages\werkzeug\test.py", line 854, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'bool' object is not callable
@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    user = User.query.filter_by(username=username).first()

    if user:
        login_user(user)
        return True

    return False
like image 311
StefanE Avatar asked Jan 28 '14 12:01

StefanE


People also ask

How do I fix bool not callable in Python?

The Python "TypeError: 'bool' object is not callable" occurs when we try to call a boolean value ( True or False ) as a function. To solve the error, correct the assignment and resolve any clashes between function and variable names.

What does bool object is not iterable mean?

The Python "TypeError: 'bool' object is not iterable" occurs when we try to iterate over a boolean value ( True or False ) instead of an iterable (e.g. a list). To solve the error, track down where the variable got assigned a boolean and correct the assignment.

What does bool object is not Subscriptable mean?

The Python "TypeError: 'bool' object is not subscriptable" occurs when we use square brackets to try to access a bool object at a specific index or specific key. To solve the error, track down where the value got assigned a boolean and correct the assignment.


1 Answers

In Flask, a view must return one of the following:

  • a string
  • a Response object (or subclass)
  • a tuple of (string, status, headers) or (string, status)
  • a valid WSGI application

Flask tests for the first 3 options, and if they don't fit, assumes it is the fourth. You returned True somewhere, and it is being treated as a WSGI application instead.

See About Responses in the documentation.

like image 121
Martijn Pieters Avatar answered Oct 13 '22 20:10

Martijn Pieters