Skip to content

Interpret \t in the indent setting as a tab - #2708

Open
shan3520 wants to merge 1 commit into
PyCQA:mainfrom
shan3520:issue/2693
Open

shan3520 wants to merge 1 commit into
PyCQA:mainfrom
shan3520:issue/2693

Conversation

@shan3520

@shan3520 shan3520 commented Oct 3, 2026

Copy link
Copy Markdown

Refs #2693

indent=\t in a config file was used literally, so wrapped imports were written as \tname, (a backslash followed by t), which is a SyntaxError in the output:

>>> print(isort.code(src, indent=r"\t", multi_line_output=3))
from a import (
\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
...

This makes \t in the indent value mean a tab, alongside the existing tab / number / literal-string handling. It also updates the --indent help text and docs/configuration/options.md to list the accepted values and to note that indent only applies to the hanging-indent multi_line_output modes, because GRID, VERTICAL and BACKSLASH_GRID align with the opening bracket. That explains the original report in #2693.

This is separate from #2704: on main, the quoted and capitalised tab spellings already work (details in my comment there). This PR only covers the \t case. I've left out the "reject an empty indent" idea from my issue comment, since it changes behaviour and is a maintainer call. I can add it here if you'd like.

Tests: parametrized Config(indent=...) cases plus one reading indent=\t from a real .isort.cfg. The unit suite passes locally (678 passed), and ruff and mypy are clean.

I used Claude to help investigate and write this change; I've reviewed and verified it.

A config file value of `indent=\t` was used literally, so wrapped imports
were written with a backslash followed by `t`, producing a SyntaxError.
Also document the accepted values for `indent` and that it only applies
to the hanging indent wrap modes.

Refs PyCQA#2693

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the difference between multi line modes is not a bug? Can we use indent there? Or would that not make a lot of sense?

@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.41%. Comparing base (d93953f) to head (e8389c1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2708   +/-   ##
=======================================
  Coverage   99.41%   99.41%           
=======================================
  Files          41       41           
  Lines        3229     3230    +1     
  Branches      689      689           
=======================================
+ Hits         3210     3211    +1     
  Misses         12       12           
  Partials        7        7           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shan3520

shan3520 commented Oct 4, 2026

Copy link
Copy Markdown
Author

That's a good question. I would say it was done intentionally and not because of a bug. Grid, Vertical and Backslash Grid align the continuation lines with the column after from x import (: the white_space padding is calculated from the statement width in wrap.py, and that alignment is the main feature of those modes, as illustrated in multi_line_output_modes.md. If they used indent, the modes would turn into the existing hanging variants:

  • Grid (0) → Vertical Grid (4)
  • Vertical (1) → Vertical Hanging Indent (3)
  • Backslash Grid (11) → Hanging Indent (2)

And tabs could not produce column alignment, since tab width depends on the editor.

So I would keep the current behaviour and use the note in the docs to let people discover it. If you want it to be more obvious, one idea is to show a warning when indent contains a tab and an aligned mode is selected. I'd be happy to implement it either here or in a follow-up.

This branch has not been deployed

No deployments
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.

2 participants