# Buggy code
def closeness(expression):
    # type: (Tuple[List[Any], Any]) -> int
    # Prioritise expressions with a node closer to the statement executed
    # without being after that statement
    # A higher return value is better - the expression will appear
    # earlier in the list of values and is less likely to be trimmed
    nodes, _value = expression
    nodes_before_stmt = [
        node for node in nodes if node.first_token.startpos < stmt.last_token.endpos
    ]
    if nodes_before_stmt:
        # The position of the last node before or in the statement
        return max(node.first_token.startpos for node in nodes_before_stmt)
    else:
        # The position of the first node after the statement
        # Negative means it's always lower priority than nodes that come before
        # Less negative means closer to the statement and higher priority
        return -min(node.first_token.startpos for node in nodes)


# Traceback (asyncpg.exceptions.ConnectionDoesNotExistError)
File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 443, in fetch
return await self._execute(query, args, 0, timeout)
File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1445, in _execute
result, _ = await self.__execute(
File "/server/athenian/api/db.py", line 191, in _asyncpg_execute
result = await self._execute_original(query, args, limit, timeout, return_status)
File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1454, in __execute
return await self._do_execute(query, executor, timeout)
File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1476, in _do_execute
result = await executor(stmt, None)
File "asyncpg/protocol/protocol.pyx", line 196, in bind_execute
return await waiter
asyncpg.exceptions.ConnectionDoesNotExistError: connection was closed in the middle of operation

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/sentry_sdk/scope.py", line 353, in apply_to_event
new_event = event_processor(event, hint)
File "/usr/local/lib/python3.8/dist-packages/sentry_sdk/integrations/pure_eval.py", line 79, in add_executing_info
pure_eval_frame(tb.tb_frame) or sentry_frame["vars"]
File "/usr/local/lib/python3.8/dist-packages/sentry_sdk/integrations/pure_eval.py", line 128, in pure_eval_frame
expressions.sort(key=closeness, reverse=True)
File "/usr/local/lib/python3.8/dist-packages/sentry_sdk/integrations/pure_eval.py", line 113, in closeness
nodes_before_stmt = [
File "/usr/local/lib/python3.8/dist-packages/sentry_sdk/integrations/pure_eval.py", line 114, in <listcomp>
node for node in nodes if node.first_token.startpos < stmt.last_token.endpos
AttributeError: 'Name' object has no attribute 'first_token'

# Fixed code
def closeness(expression):
    # type: (Tuple[List[Any], Any]) -> Tuple[int, int]
    # Prioritise expressions with a node closer to the statement executed
    # without being after that statement
    # A higher return value is better - the expression will appear
    # earlier in the list of values and is less likely to be trimmed
    nodes, _value = expression

    def start(n):
        # type: (ast.expr) -> Tuple[int, int]
        return (n.lineno, n.col_offset)

    nodes_before_stmt = [node for node in nodes if start(node) < stmt.last_token.end]
    if nodes_before_stmt:
        # The position of the last node before or in the statement
        return max(start(node) for node in nodes_before_stmt)
    else:
        # The position of the first node after the statement
        # Negative means it's always lower priority than nodes that come before
        # Less negative means closer to the statement and higher priority
        lineno, col_offset = min(start(node) for node in nodes)
        return (-lineno, -col_offset)