Skip to content

ruff_python_formatter: fix 'dynamic' mode with doctests#9129

Merged
BurntSushi merged 1 commit intomainfrom
ag/fmt/fix-i9126
Dec 14, 2023
Merged

ruff_python_formatter: fix 'dynamic' mode with doctests#9129
BurntSushi merged 1 commit intomainfrom
ag/fmt/fix-i9126

Conversation

@BurntSushi
Copy link
Member

This fixes a bug where the current indent level was not calculated correctly for doctests. Namely, it didn't account for the extra indent level (in terms of ASCII spaces) used by by the PS1 (>>> ) and PS2 (... ) prompts. As a result, lines could extend up to 4 spaces beyond the configured line length limit.

We fix that by passing the CodeExampleKind to the format routine instead of just the code itself. In this way, format can query whether there will be any extra indent added after formatting the code and take that into account for its line length setting.

We add a few regression tests, taken directly from @stinodego's examples.

Fixes #9126

This fixes a bug where the current indent level was not calculated
correctly for doctests. Namely, it didn't account for the extra indent
level (in terms of ASCII spaces) used by by the PS1 (`>>> `) and PS2
(`... `) prompts. As a result, lines could extend up to 4 spaces beyond
the configured line length limit.

We fix that by passing the `CodeExampleKind` to the `format` routine
instead of just the code itself. In this way, `format` can query whether
there will be any extra indent added _after_ formatting the code and
take that into account for its line length setting.

Fixes #9126
@github-actions
Copy link
Contributor

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@BurntSushi BurntSushi added docstring Related to docstring linting or formatting formatter Related to the formatter labels Dec 14, 2023
@BurntSushi BurntSushi added this to the Formatter: Stable milestone Dec 14, 2023
@BurntSushi BurntSushi merged commit 28b1aa2 into main Dec 14, 2023
@BurntSushi BurntSushi deleted the ag/fmt/fix-i9126 branch December 14, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docstring Related to docstring linting or formatting formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docstring formatting does not always respect line length in dynamic mode

2 participants