marimo icon indicating copy to clipboard operation
marimo copied to clipboard

feat: persist pragma comments in `marimo check --fix`

Open martimlobao opened this issue 11 months ago • 5 comments

Describe the bug

I'm running marimo check and marimo check --fix to lint and fix issues in my notebooks, and noticed that marimo is removing commented out lines that it does not complain about when running check and that are not problematic to running the notebook.

❯ uv run marimo check notebooks/example.py

❯ uv run marimo check --fix notebooks/example.py
Updated: notebooks/example.py
Updated 1 file.

This is particularly annoying since the comments I'm adding are necessary to disable test coverage calculation (using # pragma: no cover), since I don't want untestable cell definitions to decrease my test coverage. (As an aside, am I right in that there's no way to create unit tests for cell definitions?)

Will you submit a PR?

  • [ ] Yes

Environment

❯ uv run marimo env                                       
{
  "marimo": "0.18.4",
  "editable": false,
  "location": "/Users/martim/Documents/example/.venv/lib/python3.13/site-packages/marimo",
  "OS": "Darwin",
  "OS Version": "25.1.0",
  "Processor": "arm",
  "Python Version": "3.13.0",
  "Locale": "en_US",
  "Binaries": {
    "Browser": "--",
    "Node": "v25.2.1"
  },
  "Dependencies": {
    "click": "8.3.1",
    "docutils": "0.22.3",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.10",
    "narwhals": "2.14.0",
    "packaging": "25.0",
    "psutil": "7.1.3",
    "pygments": "2.19.2",
    "pymdown-extensions": "10.19.1",
    "pyyaml": "6.0.3",
    "starlette": "0.50.0",
    "tomlkit": "0.13.3",
    "typing-extensions": "4.15.0",
    "uvicorn": "0.38.0",
    "websockets": "15.0.1"
  },
  "Optional Dependencies": {
    "altair": "6.0.0",
    "duckdb": "1.4.3",
    "loro": "1.10.3",
    "mcp": "1.24.0",
    "nbformat": "5.10.4",
    "openai": "2.13.0",
    "pandas": "2.3.3",
    "polars": "1.36.1",
    "pyarrow": "22.0.0",
    "pytest": "9.0.2",
    "ruff": "0.14.9",
    "sqlglot": "28.4.1",
    "ty": "0.0.2"
  },
  "Experimental Flags": {}
}

Code to reproduce

import marimo

__generated_with = "0.18.4"
app = marimo.App(width="medium")


@app.cell  # pragma: no cover
def _():
    print("hello world")
    return


if __name__ == "__main__":
    app.run()

Then run uv run marimo check notebooks/example.py and uv run marimo check --fix notebooks/example.py

martimlobao avatar Dec 17 '25 11:12 martimlobao

Thanks, that would be annoying. Let me think through how best to handle this since comments outside of cell bodies (except in the file header) are totally untracked. It might be worth just having in general, or we might make this an edge case.


As an aside, am I right in that there's no way to create unit tests for cell definitions?

To unblock you, checkout https://docs.marimo.io/guides/testing/pytest/

cells named test_ are picked up by pytest (and run in the context of the notebook) Cells containing only functions called test_

or are you saying something like a markdown cell which would reduce test coverage but not be run?

dmadisetti avatar Dec 17 '25 16:12 dmadisetti

cells named test_ are picked up by pytest (and run in the context of the notebook) Cells containing only functions called test_

Oh, interesting I didn't realize you could rename cells to something other than the default _. I guess that would allow me to import any cell definition into a test file and run a (admittedly fairly useless) test against it so I don't reduce test coverage.

or are you saying something like a markdown cell which would reduce test coverage but not be run?

This was more what I was getting at, yes. For functions that I actually want to write tests for, I add them to the app.setup context which allows me to write unit tests for them normally by importing them into a test file.

martimlobao avatar Dec 18 '25 10:12 martimlobao

We might be able to support this- # pragma: nocover looks general enough, but seems a little niche. Quick clarification, wouldn't it be

@app.cell
def _():  # pragma: no cover
    print("hello world")
    return

Or does the given example work?

You could also just rely on our format as per the coverage rules: https://coverage.readthedocs.io/en/latest/excluding.html#advanced-exclusion

[tool.coverage.report]
exclude_also = [
    "def _", # and name relevant cells
    "@app.cell(hide=True)", # or hide unrelevant cells
]

Oh, interesting I didn't realize you could rename cells to something other than the default _. I guess that would allow me to import any cell definition into a test file and run a (admittedly fairly useless) test against it so I don't reduce test coverage.

You can expose functions too (this is automatic, if the function can act standalone), so maybe a little less useless:

@app.function
def something_that_can_be_tested_against(var: Type) -> Something:
    return ....

https://docs.marimo.io/guides/reusing_functions/

dmadisetti avatar Dec 18 '25 14:12 dmadisetti

We might be able to support this- # pragma: nocover looks general enough, but seems a little niche. Quick clarification, wouldn't it be

@app.cell def _(): # pragma: no cover print("hello world") return Or does the given example work?

Either way works in terms of disabling the statements for test coverage, but both comments still get removed automatically when either saving a notebook or running marimo check --fix.

You could also just rely on our format as per the coverage rules: coverage.readthedocs.io/en/latest/excluding.html#advanced-exclusion

[tool.coverage.report] exclude_also = [ "def _", # and name relevant cells "@app.cell(hide=True)", # or hide unrelevant cells ]

Oh nice, this actually works perfectly and solves my immediate issue of not being able to ignore statements for test coverage, thanks!

I guess it would still be nice if marimo didn't remove comments when formatting notebooks, but at least I have a workaround that works for my specific use case.

martimlobao avatar Dec 18 '25 14:12 martimlobao

I reproduced both placements with check --fix: a pragma after @app.cell and a pragma after def _(): are removed.

I prepared a fix that carries decorator and function-header comments through the parsed notebook representation and restores them during Python regeneration. It also handles multiline headers and LF/CRLF input.

The actual CLI preserves both comments. Existing parser, codegen, loader and lint suites report 432 passed and 2 skipped; 24 focused roundtrips pass.

Proposed patch. Please confirm this scope for a draft PR.

Assisted by Codex.

umtong avatar Oct 25 '26 17:10 umtong