Skip to content

quarto "run above" command breaks python for loops in positron #731

Open
@scal-o

Description

@scal-o

Bug description

When running quarto cells via the "run above" command, the indentation of for loops is not respected. The first lines of the loop run inside the loop, and others are run individually in the console.
This only happens when running cells via the "run above" comand, running them via the "run cell" command does not cause any issue.

Example cell:

# set up the coordinates dataset
nn_coord = tsp_coord.copy() 
nn_coord["order"] = np.nan
nn_coord.loc[0, "order"] = 0  # start from the first point

# set up the distances matrix
nn_distances = distances.copy()
nn_distances[:, 0] = np.inf  # set the distances to the starting point to infinity

# set up the variables to keep track of the current point and the total distance
cur_index = 0  
nn_tot_distance = 0  

# iterate through all poitns in the dataset
for i in range(1, nn_coord.shape[0]):
    
    # find the point with the minimum distance from the current point
    min_index = nn_distances[cur_index, :].argmin()
    nn_coord.loc[min_index, "order"] = i  # set the order of the point

    # update total distance and current point index
    nn_tot_distance += nn_distances[cur_index, min_index]  
    cur_index = min_index  

    # set the distances to the current point to infinity
    nn_distances[:, cur_index] = np.inf  

# sort the points by order
nn_coord = nn_coord.sort_values(by='order')
nn_coord.loc[nn_coord.shape[0]] = [0, 0, 51]  # add the starting point at the end to complete the loop

Result when running via "run above" command:

Image

You can see that some lines are executed as if they were outside of the for loop.

If the cell is run via the "run cell" command, everything works as expected:

Image

Steps to reproduce

I can successfully reproduce the issue in this notebook. However, I tried to create a reprex and I was unable to reproduce the issue consistently.

I think the problem might be due to the length of the document: when I run the "run above" command in my notebook, which has ~5 cells before the one where the problem appears, all the lines of python code are "queued" in the console before being executed. My naive thought is that the console than tries to execute the minimum number of consecutive lines that form valid python code, and stops when it finds a newline.

To test this, I tried to remove the newlines from inside the for loop:

# iterate through all poitns in the dataset
for i in range(1, nn_coord.shape[0]):
    # find the point with the minimum distance from the current point
    min_index = nn_distances[cur_index, :].argmin()
    nn_coord.loc[min_index, "order"] = i  # set the order of the point
    # update total distance and current point index
    nn_tot_distance += nn_distances[cur_index, min_index]  
    cur_index = min_index  
    # set the distances to the current point to infinity
    nn_distances[:, cur_index] = np.inf  

This does indeed work as expected:

Image

If needed, I can share the whole document as well.

Actual behavior

Sometimes, python for loops don't work when run via the "run above" command. The first lines of the loop are executed inside the loop context, the others are simply sent to the console and are executed as single-line statements.

Expected behavior

Running a cell via "run cell" or as part of a "run above" command should yield the same output (and so run the correct version of the for loop).

Your environment

  • IDE: Positron Version: 2025.06.0 (user setup) build 167
  • Interpreter: python 3.12.3
  • OS: Windows 11
  • Quarto: 1.7.31

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions