executablebooks / mdformat

CommonMark compliant Markdown formatter
https://mdformat.rtfd.io
MIT License
437 stars 46 forks source link

Incorrecly reformats python code #419

Closed fyliu closed 10 months ago

fyliu commented 10 months ago

Describe the bug

context When I pass in a code block such as below. (notice the indented inner class)

click to expand ``` 1. Add a serializer class ```python class RecurringEventSerializer(serializers.ModelSerializer): # (1)! """Used to retrieve recurring_event info""" class Meta: model = RecurringEvent # (2)! fields = ( "uuid", "name", "start_time", "duration_in_min", "video_conference_url", "additional_info", "project", ) read_only_fields = ( "uuid", # (3)! "created_at", "updated_at", ) ``` ```

expectation I expected mdformat to either:

bug But instead it re-indents the inner class to the same level as the outer class. Here's the output... (where the error is the un-indented inner class code)

click to expand ``` 1. Add a serializer class ```python class RecurringEventSerializer(serializers.ModelSerializer): # (1)! """Used to retrieve recurring_event info""" class Meta: model = RecurringEvent # (2)! fields = ( "uuid", "name", "start_time", "duration_in_min", "video_conference_url", "additional_info", "project", ) read_only_fields = ( "uuid", # (3)! "created_at", "updated_at", ) ``` ```

problem This is a problem for people creating programming guides because readers can't run the code they copy/pasted from the documentation.

Reproduce the bug

  1. I uninstalled mdformat from my system to to eliminate possiblities.

  2. I have the original text in a file called test.md

    click to expand ``` 1. Add a serializer class ```python class RecurringEventSerializer(serializers.ModelSerializer): # (1)! """Used to retrieve recurring_event info""" class Meta: model = RecurringEvent # (2)! fields = ( "uuid", "name", "start_time", "duration_in_min", "video_conference_url", "additional_info", "project", ) read_only_fields = ( "uuid", # (3)! "created_at", "updated_at", ) ``` ```
  3. My pre-commit config for mdformat:

    click to expand ``` - repo: https://github.com/executablebooks/mdformat rev: 0.7.17 hooks: - id: mdformat ```

    There's no plugin being used.

  4. I call the mdformat pre-commit hook on this file

    pre-commit run mdformat --files docs/how-to/test.md

List your environment

mdformat revision (from pre-commit config) 0.7.17

❯ python --version Python 3.11.4

❯ pre-commit --version pre-commit 3.0.4

OS: Linux Mint 21.1 Vera. Based on Ubuntu Xenial I think.

fyliu commented 10 months ago

I'm closing the issue. I ran mdformat without pre-commit and it's doing the correct thing.