quarto-dev / quarto-dev/quarto

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

Open
#731 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
645
Forks
62
Avg merge
17h 42m
Merged PRs (30d)
13

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue in the reported notebook with Positron's "run above" command, comparing it with "run cell" on the supplied Python loop. Check whether queued cells and blank lines change how the console receives the loop. Done means both commands execute the complete loop with identical behavior on the reported Positron, Python, Windows, and Quarto versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.