Repository navigation
Highlights
Please test the --preview style!
Every year in December, the Black team reviews the current code style and considers
many suggested style changes for potential impementation in the next year. We want your
feedback! Please try running black --preview on your codebases, and compare the
changes with stable Black. Let us know your thoughts -- the good, the bad, and the
ugly -- on Issue #5658.
Later in December, we plan to release a 27.1a1 beta release that stabilize the subset of
preview features we expect to stabilize in the January 27.1.0 release (hopefully most of
them!). Once that comes out, we would appreciate any further feedback! Once a feature is
stabilized, it's often very difficult to undo it, so we try very hard to get it right
the first time.
Also, if this is your first time hearing of the --preview option, we welcome feedback
on the issue tracker or
in the Discord year-round, not just in December!
Thank you so much for your input!
Other highlights
- Drop runtime support for Python 3.10 (#5616)
- Build Python 3.15 wheels (#5616)
- Add
blackd-format, a command-line client for blackd that only uses the standard
library and formats several files in parallel (#5690, #5778, #5771)
Stable style
- Fix crash when
# fmt: skipis on a one-line semicolon-separated block body (#5774,
#5674, #5588, #5675) - Fix crash when a comprehension, generator expression or lambda has
# type: ignore
comments on more than one of its lines (#5761) - Fix
--line-rangesinserting extra blank lines into top-levelasync def,
async with, andasync forstatements whose headers use# fmt: skip(#5703) - Fix
# fmt: skipinside f-string and t-string replacement fields dropping the
following newline and producing invalid code (#5728) - Fix crash for a
...body under adef,async deforclassheader that is left
unformatted by# fmt: skipor--line-ranges(#5671) - Remove the extra blank lines at the end of a file when the selected
--line-ranges
reaches its last line, matching a full-file run (#5654) - Fix
# fmt: skipbeing ignored when the same comment also contains# fmt: offor
# yapf: disable, like# yapf: disable # fmt: skip(#5665) - Preserve trailing whitespace and tab expansion in multiline docstrings containing
doctests so formatting no longer breaks their source or expected output (#5687) - Fix
# fmt: skipon the last line of a multi-line comprehension producing invalid
code (#5647) - Preserve trailing blank lines at EOF in unclosed
# fmt: offblocks (#5634) - Fix crash when removing parentheses would combine two
# type: ignorecomments onto
one line (#5686) - Fix crash on a walrus wrapped in redundant parentheses as a
withitem, such as
with ((a := 1)):(#5725) - Fix crash when a tuple used as a
withitem is wrapped in another pair of
parentheses, such aswith ((a, b)):orwith (c, (a, b)):(#5788) - Fix crash when
# fmt: skipfollows the closing bracket of a multi-line call or
subscript that isn't the last element of a collection or argument list (#5522) - Normalize hex codes in unicode escape sequences in f-strings (#5526, #5756)
- Fix
--stdinoutput ending in\r\r\ninstead of\r\nwhen the input uses CRLF
line endings and does not end with a newline, e.g. a notebook piped toblack -
(#5559) - Fix block headers being dropped or merged into a comment when a comment-only
# fmt: off/# fmt: onblock comes right before them (#5678, #5549) - Fix crash when
# fmt: skipon an opening bracket is followed by more code after the
closing bracket (#5538, #5586, #5620) - Fix a parenthesized lambda being turned into a tuple when a standalone comment sits in
its parameters (#5596, #5533) - Fix crash when
# fmt: skipfollows the closing parenthesis of a multi-linefrom
import and another statement on the same line, such as); import c # fmt: skip
(#5584) - Fix
--skip-magic-trailing-commadropping the trailing comma from a split one-element
subscript (a[x,]) used as a lambda parameter default (#5533) - Fix crash on bytes literals containing
\u,\Uor\N{...}, which are not escape
sequences in bytes and were changed by the escape normalization (#5583) - Keep comments at the end of a
# fmt: offregion inside brackets unchanged, instead
of moving a trailing comment to its own line or re-indenting them (#5585) - Fix unstable formatting when a comment-only
# fmt: off/# fmt: onblock comes
right before an assignment to a tuple, likea, b = c(#5679) - Fix crash when a compound statement header with
# fmt: skipcontains a bracket that
has its own# fmt: skipon the opening line (#5597, #5612, #5610, #5633) - Fix unstable formatting of a call with an inline comment after an argument when its
last argument is an implicitly concatenated string (#5587) - Keep the empty lines before a comment outside the requested
--line-rangesinstead of
capping them (#5613) - Fix a
# fmt: skipcomment on a blank line skipping the statement above it (#5615) - Fix crash on an assignment whose target is split over lines inside redundant
parentheses and doesn't fit on one line with the value (#5606, #5621, #5662) - Fix crash when a comment and a
# type: ignorefrom the next line are joined onto one
line and ensure the# type: ignorecomes first (#5625, #5618) - Fix crash on consecutive statements that each have
# fmt: skipon an opening bracket
(#5626) - Fix a Jupyter notebook magic in an annotated assignment (for example
files: list[str] = !ls) being replaced with theget_ipython()call that IPython
runs it as (#5628) - Fix
--line-rangesfailing when anif,for,whileortrystatement has a
clause with its body on the same line, such aselse: return(#5635) - Fix IPython help syntax after an object (
obj?), wildcard searches (np.*load*?) and
pasted>>>prompts being rewritten in Jupyter notebooks. Cells with IPython syntax
that can't be put back as written are now left unchanged (#5630) - Fix crash when
# fmt: skipis on the closing bracket of a# fmt: offregion that
ends in a comment (#5643) - Fix crash on an indented
# fmt: offin the expression of an f-string field with=
or of a t-string field (#5733, #5653) - Keep an ignored line holding only a backslash as written instead of crashing or
dropping it in certain cases (#5681, #5760) - Fix crash when a
# fmt: offinside brackets comes right before the trailing comma or
the rest of the last item (#5651) - Fix a Jupyter notebook failing to format when one of its cells can't be tokenized, and
instead just skip the cell (#5652) - Fix
--line-rangesproducing invalid code when stacked decorators outside the
selected lines need reindentation (#5672) - Fix crash on a
# fmt: off/# fmt: onblock with only comments in an f-string or
t-string field (#5701) - Fix a comment on its own line after a
# fmt: skipline being moved onto that line
when it comes right before a closing bracket, which crashed for a# type: ignore
(#5792) - Fix Jupyter notebook cells crashing or being skipped when combining magics with
ordinaryget_ipython()method calls (#5749) - Don't normalize parens around splittable long string return types, due to a style
regression in 26.10.0 (#5805) - Preserve blank lines between imports and unselected statements when using
--line-ranges, and ensure a blank line is added between an unselected import and a
newly formatted statement (#5493, #5649) - Keep the parentheses around a walrus that is the only item in a set (for example
{(x := f())}) when targeting Python 3.9 or older (#5502)
Preview style
New preview styles
- Move more compact formatting of nested brackets
(hug_parens_with_braces_and_square_brackets) from--unstableto--preview
(#5744) - Split long standalone boolean expressions containing
andororacross multiple
lines (#5735) - Split long async function argument lists before parentheses in type parameter bounds
(#5779) - Split long method chains consistently when a standalone comment separates two calls
(#5518) - Keep long single-name
fromimports on one line unless they contain comments or a
magic trailing comma (#5669) - Keep the optional parentheses around an assignment's right-hand side when removing
them would produce a first line longer than the line length and the expression
contains standalone comments (#5638, #5657) - Remove redundant parentheses around simple top-level expressions (#5516)
- Format long binary operations between collection displays symmetrically when both
operands fit on their own delimiter-split line (#5259) - Remove redundant parentheses in subscripts and indexing operators (#5496)
- Remove redundant parentheses around lists and list comprehensions (#5431)
- Remove redundant parentheses around operands in comparison expressions (#5539, #5631)
- Put each string literal of a wrapped implicit string concatenation on its own line
when the literal ends with a newline (#5554) - Correctly indent
# fmt: offcomments inside brackets, such as function calls, and
fix a crash when one comes right before a lone name, number or string (#5643) - Add parentheses around one-item sequence patterns in
casestatements, such as
case x,:(#5673) - Keep parentheses around a walrus in a subscript (for example
x[(y := f())]) when
targeting Python 3.9 or older (#5619) - Re-normalize parens around splittable long string return types, restoring regressed
behavior from 26.10.0 (#5805)
Updates to preview styles
- Keep an expression together wherever a standalone comment sits inside the surrounding
brackets (#5660) - Retain user-written parens around
inclauses of comprehensions if they have
delimiters (#4881, #5726) - Fix crash when
# fmt: skipis on the opening bracket of a collection used as a
binary operand and the expression is split at a short line length (#5609) - Fix unstable formatting when an unpacking target's redundant parentheses are split
across lines (#5563) - Fix unstable formatting of a call whose sole argument is a generator in redundant
parentheses, e.g. with--skip-magic-trailing-comma(#5564) - Don't add or remove parens around long dict values unless they have delimiters (#5627,
#5710) - Remove redundant parentheses around a
yieldof a tuple, such as(yield a, b)
(#5646) - Fix crash when a dict value is a walrus wrapped in redundant parentheses, such as
{1: ((a := 1))}(#5727)
Updates to unstable styles (string_processing)
- Keep parentheses around long concatenated strings before comparison operators (#5787)
- Merge adjacent short raw string literals with matching prefixes and quotation marks
(#5515) - Avoid adding redundant parentheses around unsplittable strings(#5544)
- Don't remove the parentheses around an implicitly concatenated string in a function
argument default value when they are needed to keep the line within the line length
limit (#5558) - Don't remove the parentheses around an implicitly concatenated string with a trailer
such as.format(...)used as a comparison operand when they are needed to keep the
line within the line length limit (#5666) - Split long strings after overloaded binary and augmented assignment operators,
including middle operands of operator chains (#5562) - Fix crash on strings in lists, tuples, and sets by not wrapping them in parentheses
when the wrapped line would still be too long (#5521) - Don't split a long t-string inside an interpolation, or merge t-strings when that
would mean escaping a quote in an interpolation (#5764)
Configuration
- Report a clean configuration error instead of crashing with a traceback when
auto-discovering a malformedpyproject.toml(#5785) - Reject the
codekey inpyproject.tomlwith a clear error instead of letting it
hijack the run, and drop it from the JSON schema (#5748) - Report invalid non-table
toolvalues in a selectedpyproject.toml, and skip them
when searching for a[tool.black]configuration (#5757) - Stop disregarding leading blank lines, which shifted the lines selected by
--line-ranges, reported the wrong line number in parse errors, and removed form
feeds on the first line (#5650) - Reject
--line-rangesfor Jupyter notebooks (#5711) - Enable the cache when using
--diff(#5499) - Respect
--skip-source-first-linewhen formatting code passed through stdin (#5580) - Emit a clear error when running
--diffin environments where the temporary directory
path is too long (#5513) - Treat an unreadable cache file as a cache miss instead of failing the formatting run
with a "cannot format" error (#5641) - Reject non-positive
--line-lengthvalues on the command line and inpyproject.toml
instead of silently formatting with them (#5664)
Parser
- Fix parsing of unpacking in a generator expression passed as the only argument to a
call, such assum(*x for x in y)(PEP 798) (#5568) - Fix parsing of starred expressions in subscripts that use
or,and,not,if,
comparisons orlambda, such asa[*b or c](#5768) - Fix parsing PEP 810
lazyimports after colons, semicolons, and backslash line
continuations (#5750)
Performance
- Skip the safety checks for Jupyter notebook cells that are already formatted, making
--checkon formatted notebooks about 3x faster (#5683) - Skip the regular expressions in string quote normalization when a string has no
backslash and no quote character to escape (#5680) - Check whether each path is a directory once during file discovery, instead of once for
every directory level above it (#5724)
Output
- Emit a warning when an explicitly supplied source directory is the filesystem root
(#5637) - Write the content of a file passed through stdin that is excluded with
--force-excludeback unchanged, retaining line endings and non-UTF-8 files (#5530) - Fix
--checkpassing when formatting multiple files is interrupted with Ctrl+C or
SIGTERM. The run is now reported as aborted and exits with code 1 (#5542)
Blackd
- Preserve non-ASCII source in the Python client when a custom
Content-Typedeclares a
non-UTF-8 charset (#5645) - Return HTTP 400 for an unknown request charset or input that cannot be decoded using
the requested charset, instead of HTTP 500 (#5519) - Keep the first line skipped with
X-Skip-Source-First-Lineinside the patch returned
for aX-Diffrequest, instead of prepending it as raw text in front of the diff
(#5573, #5732) - Fix blackd startup on Windows with more than 61 logical CPUs (#5667)
- Return an empty diff from
BlackDClient.format_codewithdiff=Truewhen the code is
already formatted, instead of the unchanged source code (#5730) - Reject zero and negative
X-Line-Lengthvalues in blackd with HTTP 400 (#5777)
Integrations
- Fix the GitHub Action picking up
venv.pyfrom the local repository (#5611) - Stop including pip & build dependencies in Docker images, nearly halving image size
(#5707)