Skip to content

Fix docstring indentation with leading tabs#5148

Merged
cobaltt7 merged 2 commits into
psf:mainfrom
puneetdixit200:fix/3667-docstring-tabs
May 22, 2026
Merged

Fix docstring indentation with leading tabs#5148
cobaltt7 merged 2 commits into
psf:mainfrom
puneetdixit200:fix/3667-docstring-tabs

Conversation

@puneetdixit200
Copy link
Copy Markdown
Contributor

Description

Closes #3667.

Black expands leading tabs in multiline docstrings before it computes the docstring indentation to strip. That expansion used Python's default 8-column tab stops, while Black normalizes surrounding code indentation to 4 spaces. As a result, nested tabs inside indented docstrings were doubled in the formatted output.

This changes the docstring-only leading-tab expansion to use 4-space columns, updates the existing tabbed docstring fixture, and adds a focused regression case from the issue shape.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Tests

  • .venv/bin/python -m pytest tests/test_format.py -k 'docstring or multiline_strings or string_prefixes' -q
  • .venv/bin/python -m pytest tests/test_black.py -k 'docstring or lines_with_leading_tabs_expanded' -q
  • .venv/bin/python -m pytest tests --run-optional no_jupyter -n auto -q -k 'not expression_diff_with_color and not piping_diff_with_color'
  • env -u NO_COLOR TERM=xterm-256color .venv/bin/python -m pytest tests/test_black.py -k 'expression_diff_with_color or piping_diff_with_color' -q
  • .venv/bin/python -m black --check --verbose src/black/strings.py tests/test_black.py
  • SKIP=prettier PRE_COMMIT_HOME="$PWD/.pre-commit-cache" PIP_CACHE_DIR="$PWD/.pip-cache" .venv/bin/pre-commit run --files CHANGES.md src/black/strings.py tests/test_black.py tests/data/cases/docstring.py tests/data/cases/docstring_tabs.py

The prettier hook was skipped locally because nodeenv failed SSL certificate verification while downloading Node; the other pre-commit hooks passed.

@github-actions
Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (988ffff) to main (13e97b4):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@cobaltt7
Copy link
Copy Markdown
Collaborator

Thanks!

@cobaltt7 cobaltt7 merged commit 650983f into psf:main May 22, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Excessive additional whitespace when using """ """ to document functions and tab indentation

2 participants