Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff_python_formatter: fix 'dynamic' mode with doctests #9129

Merged
merged 1 commit into from
Dec 14, 2023

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
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
17 checks passed
@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