# Buggy code
def __init__(
    self, servers=None, keyspace=None, table=None, entry_ttl=None, port=9042, **kwargs
):
    super(CassandraBackend, self).__init__(**kwargs)

    if not cassandra:
        raise ImproperlyConfigured(E_NO_CASSANDRA)

    conf = self.app.conf
    self.servers = servers or conf.get("cassandra_servers", None)
    self.port = port or conf.get("cassandra_port", None)
    self.keyspace = keyspace or conf.get("cassandra_keyspace", None)
    self.table = table or conf.get("cassandra_table", None)
    self.cassandra_options = conf.get("cassandra_options", {})

    if not self.servers or not self.keyspace or not self.table:
        raise ImproperlyConfigured("Cassandra backend not configured.")

    expires = entry_ttl or conf.get("cassandra_entry_ttl", None)

    self.cqlexpires = Q_EXPIRES.format(expires) if expires is not None else ""

    read_cons = conf.get("cassandra_read_consistency") or "LOCAL_QUORUM"
    write_cons = conf.get("cassandra_write_consistency") or "LOCAL_QUORUM"

    self.read_consistency = getattr(
        cassandra.ConsistencyLevel, read_cons, cassandra.ConsistencyLevel.LOCAL_QUORUM
    )
    self.write_consistency = getattr(
        cassandra.ConsistencyLevel, write_cons, cassandra.ConsistencyLevel.LOCAL_QUORUM
    )

    self.auth_provider = None
    auth_provider = conf.get("cassandra_auth_provider", None)
    auth_kwargs = conf.get("cassandra_auth_kwargs", None)
    if auth_provider and auth_kwargs:
        auth_provider_class = getattr(cassandra.auth, auth_provider, None)
        if not auth_provider_class:
            raise ImproperlyConfigured(E_NO_SUCH_CASSANDRA_AUTH_PROVIDER)
        self.auth_provider = auth_provider_class(**auth_kwargs)

    self._connection = None
    self._session = None
    self._write_stmt = None
    self._read_stmt = None
    self._make_stmt = None


# Traceback (AttributeError)
[2020-06-02 21:52:19,592: ERROR/MainProcess] Task add[4bd528a0-c1ba-456c-b74e-7dee054616bb] raised unexpected: AttributeError("'NoneType' object has no attribute 'execute'")
Traceback (most recent call last):
File "/home/bx/project/celery/celery/app/trace.py", line 480, in trace_task
uuid, retval, task_request, publish_result,
File "/home/bx/project/celery/celery/backends/base.py", line 158, in mark_as_done
self.store_result(task_id, result, state, request=request)
File "/home/bx/project/celery/celery/backends/base.py", line 443, in store_result
request=request, **kwargs)
File "/home/bx/project/celery/celery/backends/cassandra.py", line 201, in _store_result
self._session.execute(self._write_stmt, (
AttributeError: 'NoneType' object has no attribute 'execute'

# Fixed code
def __init__(
    self, servers=None, keyspace=None, table=None, entry_ttl=None, port=9042, **kwargs
):
    super(CassandraBackend, self).__init__(**kwargs)

    if not cassandra:
        raise ImproperlyConfigured(E_NO_CASSANDRA)

    conf = self.app.conf
    self.servers = servers or conf.get("cassandra_servers", None)
    self.port = port or conf.get("cassandra_port", None)
    self.keyspace = keyspace or conf.get("cassandra_keyspace", None)
    self.table = table or conf.get("cassandra_table", None)
    self.cassandra_options = conf.get("cassandra_options", {})

    if not self.servers or not self.keyspace or not self.table:
        raise ImproperlyConfigured("Cassandra backend not configured.")

    expires = entry_ttl or conf.get("cassandra_entry_ttl", None)

    self.cqlexpires = Q_EXPIRES.format(expires) if expires is not None else ""

    read_cons = conf.get("cassandra_read_consistency") or "LOCAL_QUORUM"
    write_cons = conf.get("cassandra_write_consistency") or "LOCAL_QUORUM"

    self.read_consistency = getattr(
        cassandra.ConsistencyLevel, read_cons, cassandra.ConsistencyLevel.LOCAL_QUORUM
    )
    self.write_consistency = getattr(
        cassandra.ConsistencyLevel, write_cons, cassandra.ConsistencyLevel.LOCAL_QUORUM
    )

    self.auth_provider = None
    auth_provider = conf.get("cassandra_auth_provider", None)
    auth_kwargs = conf.get("cassandra_auth_kwargs", None)
    if auth_provider and auth_kwargs:
        auth_provider_class = getattr(cassandra.auth, auth_provider, None)
        if not auth_provider_class:
            raise ImproperlyConfigured(E_NO_SUCH_CASSANDRA_AUTH_PROVIDER)
        self.auth_provider = auth_provider_class(**auth_kwargs)

    self._cluster = None
    self._session = None
    self._write_stmt = None
    self._read_stmt = None
    self._lock = threading.RLock()