# Buggy code
def is_disconnect(self, e, connection, cursor):
    if isinstance(e, self.dbapi.Error):
        for code in (
            "08S01",
            "01002",
            "08003",
            "08007",
            "08S02",
            "08001",
            "HYT00",
            "HY010",
            "10054",
        ):
            if code in str(e):
                return True
    return super(MSDialect_pyodbc, self).is_disconnect(e, connection, cursor)


# Traceback (sqlalchemy.exc.InvalidRequestError)
Traceback (most recent call last):
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1209, in _execute_context
conn = self._revalidate_connection()
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 473, in _revalidate_connection
"Can't reconnect until invalid "
sqlalchemy.exc.InvalidRequestError: Can't reconnect until invalid transaction is rolled back

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/knutj/.config/JetBrains/PyCharmCE2020.1/scratches/scratch_3.py", line 45, in <module>
r = conn.execute(sqlalchemy.select([test_table.c.id]).where(test_table.c.id == test_id))
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1020, in execute
return meth(self, multiparams, params)
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_clauseelement
distilled_params,
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1214, in _execute_context
e, util.text_type(statement), parameters, None, None
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1518, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1209, in _execute_context
conn = self._revalidate_connection()
File "/home/knutj/python_venvs/notifier/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 473, in _revalidate_connection
"Can't reconnect until invalid "
sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back
[SQL: SELECT test_table.id
FROM test_table
WHERE test_table.id = ?]

# Fixed code
def is_disconnect(self, e, connection, cursor):
    if isinstance(e, self.dbapi.Error):
        code = e.args[0]
        if code in (
            "08S01",
            "01002",
            "08003",
            "08007",
            "08S02",
            "08001",
            "HYT00",
            "HY010",
            "10054",
        ):
            return True
    return super(MSDialect_pyodbc, self).is_disconnect(e, connection, cursor)