astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.67k stars 1.09k forks source link

Failed to format notebook: source contains syntax errors: ParseError #8204

Closed Jonas1312 closed 1 year ago

Jonas1312 commented 1 year ago

Hi,

I tried the new ruff formatter on my notebooks through a pre-commit config:

  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.1.2
    hooks:
      - id: ruff
        types_or: [python, pyi, jupyter]
        args:
            - --fix-only 
            - --exit-non-zero-on-fix
      - id: ruff-format
        types_or: [python, pyi, jupyter]

But I get these errors:

error: Failed to format recipes/basic_project_setup.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/counterfactual_data_augmentation.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 18, source_path: "<filename>" }
error: Failed to format recipes/export_a_kili_project.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_dinov2.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_openai.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_openai_summaries.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Exclamation, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/frame_dicom_data.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/geojson.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/import_text_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_assets_and_metadata.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_coco.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_labels.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_pdf_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_video_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/inference_labels.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/label_parsing.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/medical_imaging.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/ner_pre_annotations_openai.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/ocr_pre_annotations.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/pixel_level_masks.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_development.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_example.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_example_document.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/set_up_workflows.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/tagtog_to_kili.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/webhooks_example.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }

Here is the pyproject.toml:


[tool.ruff]
select = ["ALL"]
src = ["src"]
line-length = 100
target-version = "py38"

[tool.ruff.pydocstyle]
convention = "google"

[tool.ruff.format]
line-ending = "lf"

[tool.ruff.isort]
known-first-party = ["src", "tests"]

And the notebooks: https://github.com/kili-technology/kili-python-sdk/tree/main/recipes

Thanks a lot!

MichaReiser commented 1 year ago

Can you try adding the notebook extensions to extend-include (doc)

[tool.ruff]
extend-include = ["*.ipynb"]
Jonas1312 commented 1 year ago

Can you try adding the notebook extensions to extend-include (doc)

[tool.ruff]
extend-include = ["*.ipynb"]
ruff.....................................................................Passed
ruff-format..............................................................Failed
- hook id: ruff-format
- exit code: 2

warning: The following rules may cause conflicts when used with the formatter: 'COM819', 'D206', 'ISC001', 'Q000', 'Q001', 'Q002', 'Q003', 'W191'. To avoid unexpected behavior, we recommend disabling these rules, either by removing them from the `select` or `extend-select` configuration, or adding then to the `ignore` configuration.
warning: The following rules may cause conflicts when used with the formatter: 'COM819', 'D206', 'ISC001', 'Q000', 'Q001', 'Q002', 'Q003', 'W191'. To avoid unexpected behavior, we recommend disabling these rules, either by removing them from the `select` or `extend-select` configuration, or adding then to the `ignore` configuration.
error: Failed to format recipes/basic_project_setup.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/counterfactual_data_augmentation.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 18, source_path: "<filename>" }
error: Failed to format recipes/export_a_kili_project.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_dinov2.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_openai.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/finetuning_openai_summaries.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Exclamation, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/frame_dicom_data.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/geojson.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/import_text_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_assets_and_metadata.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_coco.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_labels.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_pdf_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/importing_video_assets.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/inference_labels.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/label_parsing.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/medical_imaging.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/ner_pre_annotations_openai.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/ocr_pre_annotations.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/pixel_level_masks.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_development.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_example.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/plugins_example_document.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/set_up_workflows.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/tagtog_to_kili.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
error: Failed to format recipes/webhooks_example.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Percent, None), offset: 0, source_path: "<filename>" }
401 files left unchanged
MichaReiser commented 1 year ago

Interesting. @dhruvmanila any ideas? Could it be that we don't correctly set the parsing mode?

dhruvmanila commented 1 year ago

Yeah, that's correct. Let me send in a patch quickly.

dhruvmanila commented 1 year ago

Thanks for opening this issue! It's fixed and will be available in the next release.

harupy commented 1 year ago

@dhruvmanila Thanks for fixing this! I'm curious when the the next release will be available.

charliermarsh commented 1 year ago

I can probably cut a release tomorrow.