mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.6k stars 386 forks source link

active-py mode strips leading # from comments #1131

Closed kerrickstaley closed 10 months ago

kerrickstaley commented 1 year ago

Summary

When I load a file that has a cell tagged with active-py and then save it, Jupytext strips the leading # on comments in the cell, making the cell no longer run correctly in the .py file.

To reproduce

  1. Save this as foo.py:
    
    # ---
    # jupyter:
    #   jupytext:
    #     text_representation:
    #       extension: .py
    #       format_name: percent
    #       format_version: '1.3'
    #       jupytext_version: 1.15.2
    #   kernelspec:
    #     display_name: Python 3 (ipykernel)
    #     language: python
    #     name: python3
    # ---

%%

print('hello')

%% tags=["active-py"]

Print goodbye

print('goodbye')

2. Run `jupyter notebook` in the directory where you saved `foo.py` and open `foo.py`.
3. Hit Ctrl+S or Cmd+S to save the file.

# Expected behavior
The file foo.py is unchanged.

# Actual behavior
The line

Print goodbye

in foo.py changes to

Print goodbye


which makes the file no longer runnable (not valid Python code).

# System info
macOS 13.3.1
Python 3.11.5
jupytext 1.15.2

Also repros on:
Rocky Linux 8.8
Python 3.8.17
jupytext 1.5.2
mwouts commented 10 months ago

I see! Thank you @kerrickstaley for reporting this. I will have a look.