#black-formatter
1 messages · Page 5 of 1
nice! looks like i might have close to doubled my score after you posted this
I opened a PR with the basics here: https://github.com/psf/black/pull/3765
In the same vein as https://github.com/psf/black/issues/3169. Looking at that issue, I think we can remove the patch click stuff and run pyupgrade, but those two seem to be better off as separate PRs.
Describe the bug
When Black is used as a pre-commit hook, the --extend-exclude option has no effect.
To Reproduce
- Take a piece of Python code that you know will be formatted by
Black - Add the path of this file to
--extend-exclude - Run
Blackdirectly in the command line (likeblack /folder/that/contains/this/file) - Run
Blackin pre-commit
Expected behavior
When Black is run directly in the command line, that file is not formatted.
When `B...
Awesome. So much cleanup going to >= 3.8.
I also agree the other patches etc. That we can remove are better for separate PRs so we can ensure more closely they don't change any behavior.
@dense jungle what are you thoughts on https://github.com/psf/black/pull/3661 ? I'm about to patch our internal black version with this patch because we really need it, but ideally I'd align it with upstream.
I don't feel great about it because I don't feel like special-casing pyright but maybe practicality should beat purity
Apparently this was only needed on Python 3.6. We've now dropped support for 3.6 and 3.7. It's also not needed on new enough click.
most tools seem to follow # word colon comment directive syntax, could maybe special case that
# TODO: fix that - is this a directive?
Wow. This brings back memories from where the terrorized us at work everywhere too. What a time to be alive deleting legacy on black 🥰
good example, hadn't thought of that. no, but might be okay to treat it like one
I'd vote for simplicity+practicality over purity
In the output of black --check --diff the diff is shown like this:
@@ -7,11 +7,11 @@
print("test")
print("test")
print("test")
print("test")
print("test")
-
+
print("test")
print("test")
print("test")
print("test")
print("test")
There is one space too much in line 12.
Because the line itself is not mentioned in the output, it makes it impossible to write a regex to extract the line number. This would be handy f...
Also clean up unused imports
0b4d7d5 Run pyupgrade on blib2to3 and src (#3771) - hauntsaninja
2593af2 Improve performance by skipping unnecessary nor... - hauntsaninja
257d392 Fix removed comments in stub files (#3745) - JelleZijlstra
It's been a while since the last release and we have some good bugfixes and enhancements. Let's do a release in the next week or so.
b8e2ec7 Fix crash on type comment with trailing space (... - hauntsaninja
Newer mypy is typically much faster, a lot has happened since 0.991
Thanks.
Let's activate the auto updating of the .pre-commit-config.yaml - I thought we had.
has anyone looked into why the pypy tests are so slow? wondering if maybe we can just turn coverage for them off or something and everything becomes fast
probably coverage yeah
Looks good (but didn't check if there were any other changes to be made outside of diff)
The pypy tests are reeeeaaally slow. Maybe this will help.
Is there a way to disable the if-check consolidation via configuration file ?
Example:
- else "Always Convert"
- if mode == CoinsAutoConvert.ALWAYS
- else "Never Convert"
+ else "Always Convert" if mode == CoinsAutoConvert.ALWAYS else "Never Convert"
no
Aight, Thanks!
@dense jungle sorry for direct ping, would you know why https://github.com/psf/black/pull/3661 is not working for me on 3.9? I replaced all the places where type: ignore is mentioned by pyright: ignore but still no luck -- comments are reformatted....
are you using mypyc-compiled black?
Describe the bug
I have a repo in which I use the following rule to ignore all files without extensions (e.g all compiled executables)
# To ignore compiled binaries that have no extensions we need to first ignore everything,
# then unignore files with extensions and directories
*
!*.*
!*/
When I run black in the root of this repo, none of the python files in any of the subdirectories are checked because black thinks those subdirectories are ignored:
...
When trying to build with mypyc on 23.3.0
CIBW_BUILD='cp39-*' python3 -m cibuildwheel --platform linux
I'm getting error
Successfully installed click-8.1.4 editables-0.4 hatch-mypyc-0.16.0 hatchling-1.18.0 mypy-0.991 mypy-extensions-1.0.0 packaging-23.1 pathspec-0.11.1 platformdirs-3.8.1 pluggy-1.2.0 setuptools-68.0.0 tomli-2.0.1 trove-classifiers-2023.7.6 types-typed-ast-1.5.8.7 typing-extensions-4.7.1
Installing backend dependencies: finished with status 'done'
Preparing metadata (pyproject.toml): started
Running command Preparing metadata (pyproject.toml)
Traceback (most recent call last):
File "/opt/python/cp39-cp39/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/opt/python/cp39-cp39/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/opt/python/cp39-cp39/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 152, in prepare_metadata_for_build_wheel
whl_basename = backend.build_wheel(metadata_directory, config_settings)
File "/tmp/pip-build-env-d3py5x5p/overlay/lib/python3.9/site-packages/hatchling/build.py", line 56, in build_wheel
return os.path.basename(next(builder.build(wheel_directory, ['standard'])))
File "/tmp/pip-build-env-d3py5x5p/overlay/lib/python3.9/site-packages/hatchling/builders/plugin/interface.py", line 150, in build
build_hook.initialize(version, build_data)
File "/tmp/pip-build-env-d3py5x5p/normal/lib/python3.9/site-packages/hatch_mypyc/plugin.py", line 338, in initialize
raise Exception(f'Error while invoking Mypyc:\n{process.stdout.decode("utf-8")}')
Exception: Error while invoking Mypyc:
src/black/__init__.py:200:2: error: Argument 1 has incompatible type "Callable[[Optional[str], int, List[TargetVersion], bool, bool, bool, bool, bool, bool, Sequence[str], bool, bool, bool, bool, bool, bool, bool, Optional[str], Pattern[str], Optional[Pattern[str]], Optional[Pattern[str]], Optional[Pattern[str]], Optional[str], Optional[int], Tuple[str, ...], Optional[str]], None]"; expected <nothing> [arg-type]
src/black/__init__.py:461:13: error: <nothing> has no attribute "get_usage" [attr-defined]
src/black/__init__.py:466:13: error: <nothing> has no attribute "get_usage" [attr-defined]
src/black/__init__.py:1447:5: error: <nothing> not callable [misc]
error: subprocess-exited-with-error
× Preparing metadata (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> See above for output.
Seems to work fine on main .
Yeah, IIRC a recent click release broke typing. It's fixed on main, but we don't backport fixes.
@bright glacier thank you for the replay. Would it be possible to make a release? 🙂 I'd prefer to avoid backporting myself, but at the same time I don't want to use non-release commit for build the internal wheel.
I don't get to make that decision anymore as I've effectively retired from the project, but you're in luck! 23.7.0 is scheduled for ~next week: psf/black#3774
GitHub
Actually, if you just want to build your own compiled Black, you can simply patch the mypyc dependencies in pyproject.toml to exclude click 8.1.4, like we did
https://github.com/psf/black/pull/3770

scheduled for when I'm done with work in like half an hour 😄
Haha, nice :)
I do plan on getting involved here and there, but I got work too and it's a bigger priority at the moment 🙂
@elder tusk thank you so much for all of your work, it really does have an impact!
Clearing out all of the cruft! 🧹
amazing!
will take a bit for the mypyc wheels to build though
but the pure-Python wheel is on PyPI already
all the wheels are built successfully 🙂
@dense jungle 
Hm, got a ton of INTERNAL ERROR: Black produced code that is not equivalent to the source. on the new release. Here is an example diff
--- src
+++ dst
@@ -2112,11 +2112,11 @@
annotation=
None, # NoneType
arg=
'memory', # str
type_comment=
- 'Optional[joblib.Memory]', # str
+ None, # NoneType
) # /arg
defaults=
Constant(
kind=
None, # NoneType
@@ -2290,11 +2290,11 @@
name=
'__init__', # str
returns=
None, # NoneType
type_comment=
- None, # NoneType
+ 'Optional[joblib.Memory]', # str
) # /FunctionDef
FunctionDef(
args=
arguments(
args=
Description
Add a space between "of" and "hand-formatting."
Thank you very much!
looks like we're stripping type comments. Do you have a minimal example?
probably related to the type comment ast validation change I made?
let me make one
actually. I have this custom pyright patch now lol. Let me try it on the vanialla release
yeah sorry for the false alarm -- on vanilla release I don't see these crashes
Description
follow up to #3748: completely avoid import overhead in jupyter_dependencies_are_installed. This function now takes 200ms on first run when the result is True.
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Hmm, I thought about this, but when this returns true we'll end up importing these anyway right?
Black no longer uses typed-ast or the dataclasses backport, so these should both be unnecessary now!
Follow-up on https://github.com/psf/black/pull/3765
Description
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
027afda Remove Python 3.7 from classifiers (#3784) - nsoranzo
f4490ac Remove unneeded mypy dependencies (#3783) - AlexWaygood
Black https://github.com/psf/black/commit/193ee7
Options
--line-length=88
--safe
Input
Output
Expected
Hi,
I've used the formatte...
Describe the problem
The documentation of the current code-style fails to list hashbang comments (#!) among the types of comments that are excepted from the spacing rules, even though they are indeed excepted from the spacing rules (see src/black/comments.py).
From the [comments subsection of docs/the_black_code_style/current_style.md](https://github.com/psf/black/blob/main/docs/the_black_code_style/current_style...
Description
The documentation previously lacked a mention of how hashbangs are handled. Now, the documentation mentions that hashbangs are excepted from the comment spacing rules. This fixes issue #3786.
Checklist - did you ...
- [x] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Thanks! Suggesting using "shebangs" since that's the more common term (as per wikipedia)
congratulations on the release!
Here is my black configuration in pyproject.toml
[tool.black]
required-version = '23.3.0'
target-version = ['py38', 'py39', 'py310', 'py311']
skip_magic_trailing_comma = true
line-length = 88
verbose = true
I have a .pre-commit-config.yaml file that will install the black hook
(buildtest) ~/Documents/github/buildtest/ [pyproject.toml+*] cat .pre-commit-config.yaml
ci:
autoupdate_branch: ''
autoupdate_commit_msg: '[pre-commit.c...
Describe the style change
Hello,
We noticed that Black breaks long chains of assignments into multiple lines in a very unexpected way.
Example source code
AA = BB = CC = DD = EE = FF = GG = HH = JJ = KK = LL = MM = NN = OO = PP = QQ = RR = SS = TT = UU = WW = XX = YY = ZZ = 1
Examples in the current Black style
Black formats it in this way (last line is matched to max line length, but previous elements get 2 lines per one element):
AA = ...
for some reason the latest release of black is failing to parse what seems to be valid code:
error: cannot format /home/iwana/sw/d/github.com/Kron-AS/kron-backend.review/kron/prosolutions/adapters/cash_order.py: Cannot parse: 338:4: match (
This corresponds to the first line below
match (
await kron.db.execute(
db,
sa.select(CashAccount.type).where(
CashAccount.id == cash_order.dst_account_id
),
)
).scalar_one_or_none():
case None:
assert cash_order.dst_account_type != CashOrderAccountType.CASH_ACCOUNT
epk_segment = None
It is running in production at the moment, and all tests pass, which includes tests for this
So I think so
I will make a bug report, I just want to not make a dupe report
Just need to isolate and reproduce
it has to do with the .type in there
Okay, this file reproduces it: https://github.com/aucampia/issues/blob/57de52f52b947e9073c74c4263ccc29f437475cc/20230713-black-issue/badfile.py
It is .type inside ()
$ task
task: [default] python3.11 badfile.py PASSWORD
2023-07-13T12:50:47.673 952320 140388153448256 020:INFO root badfile:26:black_likes_this TRY AGAIN
2023-07-13T12:50:47.673 952320 140388153448256 020:INFO root badfile:35:black_does_not_like_this TRY AGAIN
task: [default] python3.11 badfile.py SECRET
2023-07-13T12:50:47.693 952321 139883387086656 020:INFO root badfile:24:black_likes_this YOU GOT IT
2023-07-13T12:50:47.693 952321 139883387086656 020:INFO root badfile:33:black_does_not_like_this YOU GOT IT
task: [default] pipx run --spec=black==23.3.0 black --diff badfile.py
⚠️ black is already on your PATH and installed at /home/iwana/.local/bin/black. Downloading and running anyway.
All done! ✨ 🍰 ✨
1 file would be left unchanged.
task: [default] pipx run --spec=black==23.7.0 black --diff badfile.py
⚠️ black is already on your PATH and installed at /home/iwana/.local/bin/black. Downloading and running anyway.
error: cannot format badfile.py: Cannot parse: 31:4: match (input + SomeClass.type):
Oh no! 💥 💔 💥
1 file would fail to reformat.
task: Failed to run task "default": exit status 123
Filing a bug now
Describe the bug
When black encounters access of a .type attribute inside brackets, inside a switch statement, it thinks that it is invalid syntax, even though python 3.11 executes it just fine.
To Reproduce
For example, take this code:
class SomeClass:
type = ""
match (SomeClass.type):
case _:
print("Hello there ...")
And run it with these arguments:
$ pipx run --python python3.11 --spec=black==23.7.0 black --...
I sometimes find that black inserts blank lines in spots that significantly harm readability.
This is the only time that I find myself wanting to fight the tool. I've had this particular gripe for a few years and I don't think it's going to go away, for me.
The cases I know of:
- inner classes / functions
- tightly related module-scope objects (constants/functions/classes)
importstatements tightly related to the next line
All of these are uncommon, but aren't bad style if they exist due to inherent complexity. Black makes them even harder to understand than necessary though, because I can't control those lines relations to their comments and surrounding lines of code.
Do you find this as well, or is it just me?
These are the relevant (open) issues that I find:
anyone aware of a repr function that returns black-safe text - default repr uses wrong quotes
Why would you need that?
@slow ibex to generate code thats not going to be reformatted by black
The idea sounds reasonable and the changes look good. Please address failing lint CI though.
0e26ada Continue to avoid Click typing issue (#3791) - hauntsaninja
92e0f5b Avoid importing IPython if notebook cells do ... - neutrinoceros
8d80aec Maintainers += Shantanu Jain (hauntsaninja) (#3... - ichard26
Describe the bug
if clauses in match-case blocks in are not reformatted when the line length is above the maximum length set.
To Reproduce
Take this code, where the second line is 99 characters (11 characters above the horizontal limit):
match "test":
case "test" if "any long condition" != "another long condition" and "this is a long condition":
print("Test")
And try formatting it:
$ black file.py
Black...
Will take a look tomorrow. I need sleep.
Description
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
Hello
ouch, the newlines are being escaped it seems like :/
I wonder if we remove these lines, newlines will just ✨ work ✨ https://github.com/psf/black/blob/068f6fb8fa2b52f647aec8696033e43f6b0db70b/scripts/diff_shades_gha_helper.py#L227-L228
scripts/diff_shades_gha_helper.py lines 227 to 228
# https://github.community/t/set-output-truncates-multiline-strings/16852/3
escaped = body.replace("%", "%25").replace("\n", "%0A").replace("\r", "%0D")```
Yeah, I'm not sure how this would cause issues, but this is a good idea either way. Thanks for filing the PR!
Please add a changelog entry though.
https://github.com/psf/black/actions/runs/5442939061/jobs/10199886637?pr=3759#step:7:97 annnnnd the latest PyYAML has broken the PyPy jobs :(
And to mark the 3rd thing I've noticed since coming back to Black development today: diff-shades is really slow now
Seems like I should bite the bullet and add sharding support
Hey I am a beginner and I want to contribute to black
Pyright is giving me some errros
Should I just ignore the errors?
We don't use pyright as our type checker so unless the errors seem bad, yes you can ignore them.
Are they a lot of them?
Just 2. What do you use as your type checker btw?
mypy
Thanks
if there are only two errors, what are the errors? Maybe they are worth addressing
I am going Afk
[Pyright] Cannot access member "detach" for type "AnsiToWin32"
Member "detach" is unknown (858:14)
[Pyright] Cannot access member "detach" for type "AnsiToWin32"
Member "detach" is unknown (911:10)
I am on linux btw
if that is relevant
in init.py
yeah, we can probably ignore that. I'll take a look later
okay
Description
Preserving newlines is done differently when writing to $GITHUB_OUTPUT over the deprecated :set-output: command.
https://github.com/ichard26/black/pull/70
https://github.com/ichard26/black/actions/runs/5582638757
Is your feature request related to a problem? Please describe.
It would be nice to be able to force black to never change the order of code and comments.
for example:
x = (
codeA # commentA
.codeB # commentB
.codeC # commentC
.codeD # commentD
.codeE # commentE
.codeF # commentF
)
[gets formatted to](https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AD9AG5dAD2IimZxl1N_WmyLkeQzBfBJMV7c0GsbQl...
Describe the bug
To Reproduce
For example, take this code:
def __str__(self):
return f"{self.get_name_display()}" # pyright: ignore[reportGeneralTypeIssues] -- This is some special Django magic that we can't easily tell Pyright about
And run it with these arguments:
$ black --preview
The resulting error is:
Unnecessary "# pyright: ignore" rule: "reportGeneralTypeIssues" (Pylance)
def __str__(self):...
Describe the bug
pre-commit is using pyyaml with broken version pyyaml==5.4.1, upping the version to pyyaml==6.0.1 (.pre-commit-config.yaml)
To Reproduce
Run:
pre-commit run -a
The resulting error is:
Stack trace
[INFO] Installing environment for local.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/Users/user/.cache/pre-co...
This is to resolve #3803
The same error as reported before in closed #3357 still happens when running unit tests locally now with
pytest-cov==4.1.0
pytest-xdist==3.3.1
To Reproduce
Run unit tests
(.venv) % tox -e py
...
...
Successfully built black
Installing collected packages: typing-extensions, pathspec, mypy-extensions, multidict, idna, frozenlist, click, charset-normalizer, attrs, async-timeout, yarl, black, aiosignal, aiohttp
Successfully installed aiohttp-3.8.5 aiosignal-1.3.1 async-timeou...
Description
I think the intent was to say "large changes in formatting", because it doesn't make sense to say "large formatting in changes".
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [X] Add new / update outdated documentation?
Black v23.7.0
Context
- EuroPython 2023 Mu Editor sprints.
- Mu uses black to Tidy up beginner's code.
- Several volunteers helping out translating Mu to other languages are poki...
Describe the bug
I modified a file to remove commas after ** unpacking operators to support compatibility with older versions of Python, but Black added them back. The docs say this will only happen if Black thinks my code requires Python 3.6 or above, and that it determines this by looking for other instances of commas following * or ** operators, or f-strings. I've checked carefully and none of those things are present in the file as far as I can tell.
To Reproduce
I was ...
Description
I think the intent was to say "large changes in formatting", because it doesn't make sense to say "large formatting in changes".
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Description
Hello, first-time submitting PR here.
This PR updates an assert statement that checks the bounds of a string-slicing operation. The updated assertion provides more accurate and informative error handling by specifically checking the relative values of the indices and the string length.
The original assertion was essentially checking if Python's string slicing was behaving as expected. However, it wasn't providing any guarantees or useful information about th...
good day,
i want to raise here the requirement for support formating with tab indentation on small devices with low memory / limited resources.
i am aware that there are already some issues regarding this (all closed for comments). e.g. https://github.com/psf/black/issues/47
i kindly ask you to have a closer look at the following scenario.
the following script has indented with tabs the size of 103 bytes, and formatted with black default 133 bytes (1 tab == 4 spaces).
a ...
Is this related to a problem? Please describe.
How to separate multiple values, command line or toml.
I can't work out how to do multiple excludes: the following doesn't work
black --check --line-length 120 --force-exclude PoB_rc.py src/ui/*.py src/*.py src/*/*.py
black --check --line-length 120 --force-exclude "PoB_rc.py src/ui/*.py" src/*.py src/*/*.py
black --check --line-length 120 --force-exclude PoB_rc.py src/ui/*.py --include src/*.py src/*/*.py
**Describe the...
Describe the bug
Black adds line breaks on tuple expansion even when the line length is under the allotted limit.
To Reproduce
Given the following snippet:
def do_something(values):
( a2, t0, t1, ) = values
# Dummy code follows:
if a2 < 0:
return 0
elif t1 == t0:
return 1
else:
return 2
Using
black test_black.py --target-version py39
Black formats it to:
def do_something(values):...
Hi there! I did black . on my project, but then I want black --line-length 120 .. And it did nothing after first call. How can I force black to format my code with 120 characters width?
it sounds like you need to need to run black -l 120 . with --skip-trailing-comma and then rerun it without that flag
the magic trailing comma is sticky
black by default normalizes strings to double quotes
there is option to disable string normalization
is there a way to force single quotes?
No
And don't bother with feature requests, black chose double quotes and won't change it
How can I use "--skip-string-normalization" or "-S" in my pyproject.toml or .black config files? Or I can only use it via CLI?
Black https://github.com/psf/black/commit/c3235e
Options
--line-length=88
--safe
Input
if ((
True
# sdf
)):
print("hw")
Output
Describe the style change
Currently, black will break a long line that uses a | character for type hints after the | character resulting is very unsightly code.
If it is possible to break the line after the type hints to stay within the line limit, black should leave the type hints on a single line.
Examples in the current Black style
def foo(
bar: Baz
| None = Baz(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod...
Description
In the code and tests the parameter is called target-version, not target-versionS. I think it's not good to have this typo here as it present a copy & paste risk of confusion.
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Description
We use (and have contributed) to black.
Checklist - did you ...
- [N/A] Add an entry in
CHANGES.mdif necessary? - [N/A] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
1a972e3 Add Lyft to organizations using black (#3818) - aneeshusa
Is this related to a problem? Please describe.
Describe the solution you'd like
Describe alternatives you've considered
Additional context
Describe the bug
Black trims whitespace around := operators within slices. I'm not sure if this is a feature or a bug (if it's intended, feel free to close of course).
The behavior is similar to Black's treatment of : operators in slices as described here, but I can't tell if the same treatment is intentional for := operators.
To Reproduce
For example, given this code:
Description
Rewrite and improve caching implementation to use file hashes as fallback for mtime and size comparisons. Especially for CI systems, comparing just based on mtime makes caching practically useless. With each new run and git checkout it changes and the cache will miss even if the file didn't change.
This PR adds a fallback to compare file hashes if the mtime changed to resolve that. It's not as fast, but still much faster than formatting the file outright. This approach is ...
Description
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
Description
At present, Black removes spaces around walrus operators within subscripts. For example, this is stable style:
x[y:=1]
While rare in practice, it appears that this behavior is unintentional, and falls out from Black's removal of spaces around colons in slices (e.g., x[y:z]).
This PR removes that behavior, instead requiring spaces around walrus operators in such cases.
Closes https://github.com/psf/black/issues/3820.
Checklist - d...
This looks good, thanks! However, the change should be made only in the preview style: we'll promote it to stable in January.
Describe the bug
I use the force-exclude feature to exclude files living in symlinked directories.
However, it looks like black uses the regexp from force-exclude after the symlink is resolved (not the symlinked path).
$ black -v /path_2_file/application/api/lib/dir_symlinked/__init__.py
Identified `/path_2_file` as project root containing a .git directory.
Using configuration from project root.
line_length: 135
target_version: ['py39']
force_exclude: (?:.*?applic...
Describe the bug
black seems to reformat and change the default values from None to (None,). Was this necessary? First time I have seen it being changed to (None,)
From:
@dataclass
class Foobar:
include_feedback: Optional[List[str]] = None,
exclude_feedback: Optional[List[str]] = None,
To:
@dataclass
class Foobar:
include_feedback: Optional[List[str]] = (None,)
exclude_feedback: Optional[List[str]] = (None,)
...
@dusky bluff
?
I'm using black as my formatter in VSCode, including for jupyter notebooks. Is there a way to disable it adding an empty line at the end of each cell?
EDIT: actually, that seems to be the extension's fault, not black's, #1877. But I don't see a way to fix it in notebook cells...
EDIT2: Fixed by switching to the Black Formatter extension (from just having the black formatter in the Python extension).
EDIT3: ahh, but that doesn't work when formatting the entire notebook...
isnt an empty line a requirement for normal python files? probably carry over from there
c160e4b More concise formatting for dummy implementatio... - hauntsaninja
Thanks, this looks good! I made some minor changes to preview handling, tests and reformatted the source
ugh fixing the tests on https://github.com/psf/black/pull/3823 is mildly annoying. i kinda just want to only test stable here...
i config the black in pycharm like this :-l 120 $FilePath$ -S -t py39
format the code normally in on project but occur the following issue in another project. can somebody helpt to resolve it,thanks
Usage: black [OPTIONS] SRC ...
Try 'black -h' for help.
Error: Config key target-version must be a list
python version 3.8
black version 23.7.0
black, version 20.8b1
Describe the bug
error: cannot format {...}/parser.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_wg0dl3cf.log
cat /tmp/blk_wg0dl3cf.log
--- src
+++ dst
@@ -399,11 +399,11 @@
body=
Expr(
value=
Constant(
value=
- 'According...
@warm stag :white_check_mark: Your 3.11 eval job has completed with return code 0.
{'d'}
Describe the bug
some output characters fail to render (or look like that)
(i mean, those three squares after 'all done' message)
same things in both alacritty and vscode terminals. i think this is probably due to lack of dome unicode fonts, or something
To Reproduce
checkout screenshot above
Expected behavior
symbols are visible, i suppose
Environment
- Bla...
I am trying to read black source code and confused about something,
this is their __main__.py file. How are they taking cli arguments?
from black import patched_main
patched_main()
hoping a black developer is here
check what patched_main looks like ;)
click
basically you dont need to take cli arguments in the main.py file as you can access them anyway from sys.argv
but yeah click parses the argv for you
what does freeze mean btw?
iirc its just to fix a multiprocessing issue, i wouldnt worry about it
black can be used just with a snippet so code is the snippet provided and src is the place to run black on
oh
its like python -c "import foo; foo.do_something()" vs python foo.py
except its gonna do formatting and not run the code/file
didn't know there was a thing called echo() in python
if not src and code is None:
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
ctx.exit(1)
its a click function
What is this doing?
is just checking is one of them or are present?
I thought you use `or' for that?
there are multiple ways of doing basically anything
not (src or code)
wouldn't this be better?
or am I missing something here
#+begin_src python :results output
src = None
code = None
print(not (src or code))
print(not src or code is None)
#+end_src
#+RESULTS:
: True
: True
They seem to do the same?
ah nevermind
#+begin_src python :results output
src = False
code = False
print(not (src or code))
print(not src and code is None)
#+end_src
#+RESULTS:
: True
: False
they do differ
Is your feature request related to a problem? Please describe.
I want to add a problem matcher for the Github Action.
Describe the solution you'd like
the official github action of "black" supports the problem matcher.
Describe alternatives you've considered
provide a matcher JSON for black.
Additional context
i did follow this, from a random python repo which implement the black prob...
Thanks, looks like another user confirmed the fix as well!
c36e468 Remove ENV_PATH on Black action completion (#3759) - cjproud
I have checked with the online formatter that it is not fixed in the most up to date development branch.
Describe the bug
The following code has a line that is longer than 88 characters and yet is not split by black:
class LongNameLongName:
pass
class LongNameLongName2:
pass
class LongNameLongName3:
pass
class LongNameLongName4:
pass
class LongNameLongName5:
pass
class LongNameLongName6:
pass
class ClassWithTypeDefs...
I've been using the black-jupyter pre-commit hook and was trying to update to the pre-commit mirror documented in #3828. However, the mirrored hook does not seem to reformat any files.
Describe the bug
To Reproduce
With this entry in my .pre-commit-config.yaml and Jupyter notebook test.ipynb, running pre-commit run --hook-stage manual black-jupyter --files=test.ipynb does not reformat the notebook.
- repo: https://github.com/psf/black-pre-co...
Thanks, this is a nice feature!
Thanks for the updates! Looks good, but I'd like for Jelle to take a look before merging
Hmm, I'm not sure I like the current version, it seems too easy to forget to call cache.read(). I think I'd prefer one of:
a) make file_data a property that reads the cache file if self._file_data is not set
b) initialise file_data to None, so we get errors if we try to use the Cache without read-ing first
c) revert to before the last commit
I'm a bit uncreative as to what users might want to configure there
Description
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Cool 👀
black uses CWD to search for config files when using stdin mode instead of --stdin-file as a starting point for locating config files.
This impacts tools like VS Code where we need to format unsaved file buffer content before saving it to disk. Like as implemented with this extension here https://github.com/psf/black/issues/2883
To Reproduce
Dir structure:
project
|- client
| |- pyproject.toml
| |- script.py
|- templates
- Set cwd as
prokect - Run `...
black, 22.8.0 (compiled: yes)
Python (CPython) 3.6.9 (I know, I know, sorry)
Ran black on a pytest file with some pytest.fixture definitions; a Test class, decorated with pytest.usefixtures(), with three staticmethod tests; and several test functions decorated with pytest.parametrize(). Got this:
error: cannot format test/test_apps.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues.
Am ...
Description
A more permanent fix for tox issue. This reverts #3843
Starting with 4.9, tox will throw an error for unspecified environment names. The run command was missing the python version. Doing the fix inline might not be as nice as a separate variable but works for all three OS without issues.
https://tox.wiki/en/latest/changelog.html#v4-9-0-2023-08-16
https://github.com/tox-dev/tox/issues/2858
Is your feature request related to a problem? Please describe.
I am planning to use black for my existing large codebase. till now it didnt have any formatters used.
black does change a lot of code along with replacing code with backslashes to paranthesis which i am fine with to maintain consistency. but my import statements are greater than 79 chars even after formatting because of nested module structure and long file names which doesn't look good in my code.
**Describe the solut...
c6a031e Improve caching by comparing file hashes as fal... - cdce8p
Rewrite "Next steps" paragraph.
Description
I just wanted to notify you of a paragraph that I thought needed to be re-phrased after reading this page. However, I'll leave the final decision up to you. As I'm not familiar with GitHub at all, I'm not even sure if I'm doing this "Pull Request" correctly and don't have the time or inclination to read up on everything as I don't plan on becoming a regular contributor. Nevertheless, I've put an 'x' in all 3 check boxes below but have no...
it's always amusing when I search for "black playground" and get an ad for https://www.playgroundboss.com/play-king/ (only $89k!)
Describe the bug
Match patterns with lots of |-delimited alternatives are not split into multiple lines.
To Reproduce
% cat longmatch.py
match x:
case "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd":
pass
% black -t py311 longmatch.py --diff
All done! ✨ 🍰 ✨
1 file would be left unchanged.
(Note the line is 142 characters long)
Expected behavior
...
Describe the style change
Unlike other expression level single element tuples, single element tuples in yield expressions (e.g. yield 1,) are currently not parenthesized. This is inconsistent, they should also be parenthesized.
Examples in the current Black style
Input:
for a, in b:
pass
c = d,
def e():
yield f,
return g,
Output:
for (a,) in b:
pass
c = (d,)
def e():
yield f,
return (g,)
...
Downloads Badge
The downloads badge on the main readme seems broken.
Could it be that there has been updates that has not been followed up on? The link works as intended but the badge is not.
Description
Fixed the broken badge link in main readme, picture of how it was can be seen in the issue
Checklist - did you ...
Changed from broken link to badge to the working one. Not sure if it earns a place in CHANGES.md
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ x] Add new / update outdated documentation?
Black currently formats
zzzzzzzzzzzzzzzzzzzzzzzzzzz = zzzzzzzzzzzzzzzzzzzz = zzzzzzzzzzzzzzz = (
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
)
to:
zzzzzzzzzzzzzzzzzzzzzzzzzzz = (
zzzzzzzzzzzzzzzzzzzz
) = zzzzzzzzzzzzzzz = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
Expected behavior
Black should favor using parenthesis and break on the right most side:
zzzzzzzzzzzzzzzzzzzzzzzzzzz = z...
Sorry if I caused spammed - Airplane internets ...
Got it anyways - yay - Helped black for the first time in a long time: https://github.com/psf/black/pull/3848
Hi everyone! I'm a bit surprised by two cases where I would have expected different results. Each is one line that is nicely wrapped and one slightly altered example that is not wrapped and thus exceeds the maximum line length (set to 40 for these examples):
a = (
a123456789.a123456789()
.a123456789()
.a123456789()
.a123456789()
)
a = (
a123456789.a123456789.a123456789.a123456789.a12345678()
)
Is there a workaround for the above example? I.e. what can I do so that black will wrap the second line?
(
a123456789.a123456789()
.a123456789()
.a123456789()
.a123456789()
)
a123456789.a123456789().a123456789().a123456789().a123456789()
Is this expected? Could black be extended to add parenthesis or backslashes automatically so the line can be wrapped (the original example where this came from in my codebase had backslashes).
I'm happy to open GitHub issues for either one, if requested.
Playground for Black, the uncompromising Python code formatter.
this is perhaps https://github.com/psf/black/issues/510
we should probably fix it by adding parentheses (Black doesn't do backslashes)
Yeah, that issue seems to kind of cover both of my cases.
Hot take here: IIUC backslashes would allow black to wrap some lines that it currently can't without adding parenthesis, which often change the AST (were not changing the AST if possible is a stated, understandable design decision).
Has there ben any talk on using backslashes vs. using parenthesis? I know this might never change but I always use backslashes when possible to wrap long statements and in my experience, that works well, so IMHO chossing backslashes instead of parenthesis would have also been a valid desing choice.
Changing parentheses doesn't change the AST (except in one arcane case, the del statement)
Ah, really? I would have assumed they're part of the AST in most chases.
Not using backslashes was an early decision by Lukasz, probably because he thought they looked ugly; I agree
(important edit there :D)
I added the two "bad" examples I got to the issue.
I'm looking through our (about 100k lines) project for instances of bad formatting (after running with --preview) and I'm quite happy so far. One thing that comes up a lot in our codebase is examples like this (we have a lot of API help messages and error messages):
example(
help_text=f'a123456789 "a123456789" {a123456789}')
Which gets formatted to this:
example(
help_text=(
"a123456789"
' "a123456789"'
f" {a123456789}"
)
)
After looking at this for a few hours I still find it quite annoying that different lines of the same implicitly concatenated string end up having different quotes and prefixes. I think the reasons I find this annoying are:
- Harder to edit because different lines need different quotes escaped (it's mostly english text with lots of
'plus lots of"around identifiers in the help text). - Harder to join lines after editing text (need to convert between quote types to be able to join string).
- The content of each line doesn't start on the same column making it harde to gauge which quotes are part of the string and which are the delimiting quotes.
I haven't found an open GitHub issue about this exact stylistic question. Should I open one or is it obvious that isn't going to change?
Playground for Black, the uncompromising Python code formatter.
My idea would be to change the style rules so that implicitly concatenated string literals and f-strings are always formatted as a single item. For that item, a single "best" quote (same rules as currently used for individual strings) is selected plus whether it needs to be an f-string or not, then the implicitly concatenated string is re-flowed and all lines are formatted with the same quote and prefixes.
(haven't thought about r-strings, because I have not much experience using those, but our code base certainly contains some of these)
Describe the bug
Trying to format the following input (playground) with black -l 1 --preview leads to an error:
dict(a=r' ' r'')
$ black -l 1 --preview foo.py
error: cannot format foo.py: Cannot parse: 5:4: r""
...
if your project is using single-quote strings, consider using --skip-string-normalization
I was just now investigating that idea as a possible workaround. But:
- It won't solve the issue of having inconsistent prefixes (string literal vs. f-string).
- I'd actually like to have quotes normalized (it's currently a mess), but I could use other tooling for that.
Feel free to open an issue. I don't remember seeing this before, but I think it's reasonable request. Maybe separate issues for quote style and prefixes, though
I think using the same quote style consistently for implicitly concatenated strings makes sense, but for prefixes, we would have to change the user's decision on whether to use a raw string, which feels a little icky
I see the issue here. A pragmatic approach would be to look at a corpus of projects and seeing how often r-strings and other strings are mixed in this way. I assume this doesn't happen often, and I would find it super confusing
But there is an edge case here: A string containing a single trailing backslash can't be representet as a r-string, i.e. r'C:\Windows\System32' '\\' would have to be converted to a normal string instead of an r-string, if it was to be noramlized to a single string type.
Error compiling contract, Error message:
tokenize.TokenError: ('EOF in multi-line statement', (230, 0))
The above exception was the direct cause of the following exception:
vyper.exceptions.SyntaxException: EOF in multi-line statement
line 230:0
229 227 ],
---> 230
---------^
_Originally posted by @Last-Winner in https://github.com/psf/black/issues/2317#issuecomment-169572856

granted, breaking up/reflowing f-strings makes me feel uneasy in general
feels like it can end up muddying the intent of the code
hmm, this reflowing has some interesting quirks in general
it will never make short things longer
so doing black -l 20 then black -l 80 in succession sticks with the narrow reflow
(except in the case when everything fits in one line)
Yeah, that's my general feeling too. :/ I'm currently thinking a lot about how a style rule could look that would lead to consistent, readable code that also doesn't make hand-formatted code worse in too many cases or at least allows the freedom to hand-format in a way that isn't messed up by black.
I noticed that too and I think that's quite a weird behavior. In almost all other cases black -l <a> followed by black -l <b> leads to the same result as black -l <b> for all a and b. IMHO it would be nice if that would also hold with string reflow. What do you think about that?
I don't think that's reasonable. Having used Elm's formatter for a few years it's clear for me that joining/splitting lines based on column width is bad. You want two modes: vertical and horizontal. The human chooses which to use by having a newline somewhere or not.
I've only been looing into black for less than a week, but my understanding was that it is moving into the direction of re-wrapping strings in the future. Is that a decision that's still being discussed?
I would hope they don't do that personally. Rewrapping at all based on line length seems like a mistake to me. Number of things yes, but that makes little sense for strings.
it's in the preview style
should be in the stable style next year
Black produced code that is not equivalent to the source. (with --preview)
Describe the bug
To Reproduce
For example, take this code:
log.info(f'Skipping: {desc["db_id"]=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]=} {desc["exposure_max"]=}')
And run it with these arguments:
$ black --preview file.py
The resulting error is:
cannot format file.py: INTERNAL ERROR: ...
--- src
+++ dst
@@ -8,11 ...
i went through all the black preview issues. of 21, 15 + maybe 1 are caused by string_processing, 5 are other things
Hello, I'm experiencing a bizarre issue with black, some files in certain folders error out when trying to format them with black
(both test.py are equivalent and it's just a hello world)
The only difference I can see between both is that it's reading from the pyproject.toml in the first example, but even so the only thing defined there is the line-length parameter, and the root of the project seems to be properly identified
Maybe these files are saved in different encodings
Both files have been created in the same way with the same content, and they have the same checksum
If they have the same checksum, then they should be the same... Do they have the same permissions set? Could it be that black can't open the file, and is erroring out saying that the file has no data?
Honestly just delete Black's cache, might be some weird corruption
It should be in ~/.cache/black
hello guys, how can i resolve this error?.....ImportError: DLL load failed while importing win32print: The specified module could not be found.
[8596] Failed to execute
but how? error in windows?
Why does black need cache? What is stored inside of it?
it stores the converted parser grammar and files previously formatted so you can run black against a project multiple times quickly
Yeah, that makes sense. Thank you
#2736 added as rule that preserves a blank line between attributes and methods. The implementation looks at the previous line instead of the previous statement, so it depends on the last statement in the (potentially arbitrarily nested) body if black inserts a blank line or not (code). #2783/#3564 fixes this for classes by always inserting a blank line, but this does not apply e.g. to fu...
Looks good (though I don't use PyCharm and haven't tried this).
@hauntsaninja are you OK with this PR now? (I can't merge since you requested changes.)
Looks reasonable to me (also do not use pycharm and haven't tried)
74d3009 Add Black PyCharm 2023.2 integration instructio... - jonashaag
a20338c Avoid removing whitespace for walrus operators ... - charliermarsh
The idea behind this change is that we stop looking into previous body to determine if there should be a blank before a function or class definition.
Input:
import sys
if sys.version_info > (3, 7):
class Nested1:
assignment = 1
def function_definition(self): ...
def f1(self) -> str: ...
class Nested2:
def function_definition(self): ...
assignment = 1
def f2(self) -> str: ...
if sys.version_info > (3, 7):
def n...
is there an official way to turn of caching in development? I've been using BLACK_CACHE_DIR=/dev/null so far
Looks good from an implementation perspective, will wait a little longer for feedback on whether the formatting change is good.
I think that's the best way
release time
Nice catch on the lost changelog entries!
welp seems like we failed to build 3.12 wheels https://github.com/psf/black/actions/runs/6128665138/job/16635954268
I don't think mypyc supports 3.12 fully yet as of the latest release
oh seems like latest mypy should support it?
23.9.0 is on pypi now but without mypyc wheels. I think I'll make 23.9.1 tomorrow, if possible with 3.12 mypyc wheels and otherwise with wheels only for 3.8-3.11
cc @bright glacier if you're around/available
I'm honestly not entirely sure if the latest mypyc fully supports 3.12.
Description
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
I was going off https://github.com/mypyc/mypyc/issues/995 being closed in July
Yea... is black using the latest mypyc?
probably worth doing that
do you know off hand the command to test building the wheels locally?
unfortunately no
so I can test whether it works without doing another release 🙂
ok I'll figure it out
thanks, that's great
if you'd like, I can just do an one-off GHA run for 23.9.0 with an updated mypyc (and avoid the bugfix) tomorrow
I still have upload privileges to PyPI.
it feels slightly sketchy to have the release tag pin 1.3.0 but have a wheel built with 1.5.1
fair enough
we should really set up the workflow to perform a test run weekly or biweekly
I'll take a look this weekend.
thanks!
The mypyc wheels for the latest release (23.9.0) failed to build (https://github.com/psf/black/actions/runs/6128665138/job/16635954268) because we were running a version of mypyc that doesn't yet support Python 3.12. Therefore, 23.9.0 now only provides a pure-Python wheel on pypi, which will be much slower.
My current plan is to upgrade to the latest mypyc (#3864), confirm that the 3.12 wheel gets built correctly with it, then release version 23.9.1 tomorrow.
^ pinning this preemptively in case people complain
Reasonably likely I'd say
It would be nice if CIBW could not bail on a single platform failing to build, but it does make sense (if one is broken, the others may be subtly broken)
or at least it could still save the wheels from the successful platforms for debugging (and returning an appropriate failure indicator)
getting errors like this running cibuildwheel, suggesting that it's installing an old version of black somehow ```/project/tests/util.py:31: in <module>
DEFAULT_MODE = black.Mode()
E AttributeError: module 'black' has no attribute 'Mode'. Did you mean: 'mode'?
(we've been building 3.12 mypyc wheels for mypy for a month, so it should work with sufficiently new mypy)
Hey!
How do people feel about supporting mojo in black? If you mojo like python black will often just work, but more mojo like features break compatibility.
The mojo peeps apparently have some internal fork
I have a suspicion however supporting two languages (even if they are related) might become a bit of a hassle, so a fork might be preferable.
got a 3.12 wheel to build locally, but import fails with ```>>> import black
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/black/init.py", line 64, in <module>
from black.linegen import LN, LineGenerator, transform_line
File "src/black/linegen.py", line 1000, in <module>
@dont_increase_indentation
File "src/black/linegen.py", line 971, in dont_increase_indentation
def split_wrapper(
File "/Users/jelle/.pyenv/versions/3.12.0b2/lib/python3.12/functools.py", line 58, in update_wrapper
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'split_wrapper_dont_increase_indentation_obj' object has no attribute 'dict'. Did you mean: 'dir'?
So we don't just test this workflow on releases. Maybe every PR is too much, but I want it at least on PRs that prepare releases.
Great idea. Maybe on merge to main only we run this ... but either way this is handy to know we work and I'll be stealing it for other projects.
Looks like mypyc might be producing function objects that don't have __dict__s, and that's breaking functools.update_wrapper from the stdlib?
yes, for nested functions only maybe?
That sounds pretty plausible
Can't reproduce this though with a simple example
Recent ReadTheDocs builds have been failing as our documentation dependencies (notably Sphinx) require Python 3.9+.
I'm also struggling to create a repro that uses functools.wraps, but here's a repro without functools.wraps. Save this to a file repro.py, run mypyc repro.py, then run python -c "import repro:
from typing import Callable
def dont_increase_indentation(split_func: Callable[[], str]) -> Callable[[], str]:
def split_wrapper() -> str:
return "foo"
split_wrapper.__dict__.update(split_func.__dict__)
return split_wrapper
@dont_increase_indentation
def delimiter_split() -> str:
return "bar"
Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "repro2.py", line 11, in <module>
@dont_increase_indentation
File "repro2.py", line 7, in dont_increase_indentation
split_wrapper.__dict__.update(split_func.__dict__)
AttributeError: 'split_wrapper_dont_increase_indentation_obj' object has no attribute '__dict__'. Did you mean: '__dir__'?
I can reproduce that too
Another oddity is that if you use @wraps the name of the inner function ends up in the module namespace
Which parts of black do you mypycify? Could it be that the issue only occurs if you @wraps a function in a non-mypycified file where the function is imported from a mypycified file?
I'm wondering if @wraps in a mypycified file maybe doesn't access __dict__ in the same way as it does in a non-mypycified file
this occurs in src/black/linegen.py, the decorator is in the same file as the decorated function
I don't recall which parts we compile, almost all I think
oh but the decorated function is a generator, I wonder if that matters
I tried with generator functions, they didn't seem to be any different from regular functions
I'm not sure mypyc properly supports function attributes ig
Nested functions are usually compiled into a dict less class internally
I thought so too but the code in linegen.py is pretty old, so it used to work
this is also not the main thing blocking us by the way, on https://github.com/psf/black/pull/3867 we're failing to pickle some NamedTuples
which I also can't reproduce locally 😦
Did functools change in 3.12 ?
yeah i know, but I'm already looking at this one, and that one looks even harder to debug, so Imma sticking with this one 😉
I remember somebody called "Jelle Zijlstra" making some changes to @wraps: https://github.com/python/cpython/pull/104601
Other than that change, though, it looks like the source code for functools.wraps and functools.update_wrapper has been unchanged for the last 10 years
The RTD build is still failing which is strange since docs CI on GH is fine...
It's seemingly some bad interaction involving Furo, but I have no idea..
CPython PR looks innocent on a brief read. Hmm.
huh, there's a comment in the mypyc source code that seems to imply the opposite, and specifically calls out functools.wraps as the reason why?
mypyc/irbuild/callable_class.py lines 60 to 65
# The functools @wraps decorator attempts to call setattr on
# nested functions, so we create a dict for these nested
# functions.
# https://github.com/python/cpython/blob/3.7/Lib/functools.py#L58
if builder.fn_info.is_nested:
callable_class_ir.has_dict = True```
Only on mypyc? That's... even more weird since it seems like the FileData class is from importlib?
I haven't seen that line of code before :P
So wait, this only fails on 3.12?
it's not from importlib, it's in our code. I think the problem is that mypyc miscompiles it so pickle thinks it's in importlib
oh i'm testing locally with Python 3.11. That repro I posted above that doesn't use functools.wraps repros with Python 3.11.
I don't even have Python 3.10 on my system right now 😅
PyMemberDef split_wrapper_dont_increase_indentation_obj_members[] = {
{"__dict__", T_OBJECT_EX, sizeof(black_shenanigans___split_wrapper_dont_increase_indentation_objObject), 0, NULL},
{"__weakref__", T_OBJECT_EX, sizeof(black_shenanigans___split_wrapper_dont_increase_indentation_objObject) + sizeof(PyObject *), 0, NULL},
{0}
};
__dict__ should exist on the split_wrapper callable class object ...
unless CPython expects it to be in the tp_dict slot instead??
I'm now trying with Python 3.12, but mypyc isn't working for at all on 3.12 locally on Windows...
LINK : fatal error LNK1104: cannot open file 'python312.lib'
error: command 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.32.31326\\bin\\HostX86\\x64\\link.exe' failed with exit code 1104
I love mypyc development, it is definitely not hairpulling
oh i think some weird things about tp_dict changed in py312, no idea if that's relevant
there are two __dict__ lookups in the C (line 412 and 425) not sure what's going wrong yet
It's failing on 3.8 so probably not relevant?
for posterity, this is what I was referring to: https://github.com/python/cpython/issues/105227
ah it looks like the decorated function doesn't have a __dict__
❯ ./shim.py temp/black_shenanigans.py -r
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "temp/black_shenanigans.py", line 12, in <module>
@dont_increase_indentation
File "temp/black_shenanigans.py", line 7, in dont_increase_indentation
src = split_func.__dict__
AttributeError: 'builtin_function_or_method' object has no attribute '__dict__'
[black_shenanigans.py in 3.16s: (646) return code 1]
nevermind, both attribute lookups are failing...
I don't understand why only Python 3.12 is failing with the older mypyc and why everything is failing with the newer mypyc. I'm still stuck at square one :/
for what it's worth, older versions are also failing with mypyc 1.3.0, except for macos arm64 where only 3.12 is failing
I don't know if that decreases or increases the mystery
btw the magical incantation to build a mypyc wheel locally is HATCH_BUILD_HOOKS_ENABLE="1" python -m pip wheel --no-deps -v .
Okay, I managed to get mypyc working on 3.12 for me locally (using pyenv rather than building from source).
It looks like a mypycified version of this file will break with AttributeError on both 3.11 and 3.12:
from typing import Callable
def dont_increase_indentation(split_func: Callable[[], str]) -> Callable[[], str]:
def split_wrapper() -> str:
return "foo"
split_wrapper.__dict__.update(split_func.__dict__)
return split_wrapper
@dont_increase_indentation
def delimiter_split() -> str:
return "bar"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "repro2.py", line 12, in <module>
@dont_increase_indentation
File "repro2.py", line 8, in dont_increase_indentation
split_wrapper.__dict__.update(split_func.__dict__)
AttributeError: 'split_wrapper_dont_increase_indentation_obj' object has no attribute '__dict__'. Did you mean: '__dir__'?
But a mypycified version of this file only breaks on Python 3.12:
from functools import update_wrapper
from typing import Callable
def dont_increase_indentation(split_func: Callable[[], str]) -> Callable[[], str]:
def split_wrapper() -> str:
return "foo"
update_wrapper(split_wrapper, split_func)
return split_wrapper
@dont_increase_indentation
def delimiter_split() -> str:
return "bar"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "repro3.py", line 12, in <module>
@dont_increase_indentation
File "repro3.py", line 8, in dont_increase_indentation
update_wrapper(split_wrapper, split_func)
File "C:\Users\alexw\.pyenv\pyenv-win\versions\3.12.0rc2\Lib\functools.py", line 58, in update_wrapper
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'split_wrapper_dont_increase_indentation_obj' object has no attribute '__dict__'. Did you mean: '__dir__'?
So it looks like something did change in Python 3.12?
I think this is a minimal enough repro to file a mypy issue, anyway (writing it up now)
have you considered using nuitka instead of mypyc?
from my experience, it makes faster code than mypyc and implements semantics closer to pure python behaviour
Describe the bug
Black fails to format code containing inline type comments within a multi-line attribute access consistently (the formatting moves the type comments to the end of line in trying to collapse the attribute access, changing meaning and triggering the INTERNAL ERROR check).
To Reproduce
For example, take this code:
❯ cat /tmp/foo.py
def _get_light_style(app: sphinx.application.Sphinx) -> Style:
return (
app # type: ...
I just filed my first issue in black!
My mess around PR. Skip compilation on 3.12, skip compilation of black.cache
iirc there were some similar changes in 3.11 that also broke mypyc
has anyone been able to reproduce the pickle thing locally?
i think https://github.com/psf/black/pull/3870 is likely good, should be enough to unblock 23.9.1
yes that's a good idea, thanks
looks green! (diff-shades aside)
This is enough for me to get a clean test run on Python 3.9 with mypyc, for the usual reason that mypyc doesn't like monkeypatching.
I have not been able to repro the pickle failures on either Linux or macOS.
Describe the bug
TLDR: https://github.com/psf/black/pull/3796 can make stuff longer.
To Reproduce
def long_function_with_sine_params_and_long_return(param, another_one) -> ReturnType:
...
which is fine in the current style, is now formatted, in the preview style, as
def long_function_with_sine_params_and_long_return(
param, another_one
) -> ReturnType: ...
which is longer.
Expected behavior
I am not sure:
- Acce...
Description
I spotted that https://black.readthedocs.io/en/stable/ is still pointing to 23.7.0 and https://black.readthedocs.io/en/latest/ to 23.7.1.dev33, instead of both at 2.3.9.0[.devXX].
The last Read the Docs build failed with:
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/black/envs/latest/lib/python3.11/site-packages/sphinx/cmd/build.py", line 293, in bui...
4eebfd1 Add mypyc test marks to new tests that patch (#... - hauntsaninja
Describe the style change
I don't like current behavior on documented FastAPI handlers.
@router.get("/path",)
async def some_handler(
can_restrict_members: bool | None = Query(None, description="Can restrict members?"), # the line is toooooo long
):
...
Examples in the current Black style
async def some_handler(
can_restrict_members: bool
| None = Query(
None,
description="Can restrict members?",
),...
And test that certain common external use cases (taken from https://grep.app/search?q=black.Mode&case=true) keep working.
0b62b9c Ignore aiohttp DeprecationWarning for 3.12 (#3876) - JelleZijlstra
(can also use pickle protocol 5 if we want to now)
751583a Pickle raw tuples in FileData cache (#3877) - JelleZijlstra
time to try another release
LGTM.
Let's see if the workflow passes.
I'm glad to no longer be playing Russian roulette with mypyc when I actually cut the release 🙂
As of #3870 we now run the full mypyc wheel build workflow on all PRs. This should prevent a repeat of the 23.9.0 release, which didn't come with mypyc wheels because after the release was cut we discovered that the mypyc build was broken (#3865).
It's very important to me that we can test the wheel build before the release, as is happening right now on #3878 for the upcoming release 23.9.1. That gives me confidence to cut a release knowing that the build will actually succeed.
Some o...
it's building the last wheel now
and all the wheels got uploaded 🎉
After upgrading to 23.9.1, due to #3846 I ran into the following situation:
a/file.pyis a symlink tob/file.pyb/file.pyis not formatted because it's inexclude
Previously, Black ignored the file. Now it wants to format it.
I would expect Black to not format these files. The actual files are excluded, the symlinks aren't themselves Python files. I think this can be implemented with a simple rule: never follow symlinks. If the file is in a place that Black should form...
hmm seeing
return black.format_str(
File "src/black/__init__.py", line 1085, in format_str
File "src/black/__init__.py", line 1117, in _format_str_once
File "src/black/nodes.py", line 156, in visit
File "src/blib2to3/pytree.py", line 47, in type_repr
AttributeError: attribute 'paramspec' of '_python_symbols' undefined
on some test at work
hm maybe you have some old file lying around?
that node should have been added in #3703 which was in 23.7.0 already
probably not from file system state, this is in CI on some test that looks like it's running black on some codegen. anyway, i'll look into it and see what's going on
interestingly line 47 appears to be where it does dir(), and the getattr is on line 48.
I get a different error locally when I run type_repr
>>> blib2to3.pytree.type_repr(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/blib2to3/pytree.py", line 43, in type_repr
from .pygram import python_symbols
ImportError: cannot import name 'python_symbols' from 'blib2to3.pygram' (/Users/jelle/py/venvs/py311/lib/python3.11/site-packages/blib2to3/pygram.cpython-311-darwin.so)
oh works if I import black first
Build in separate jobs. This makes it clearer if e.g. a single Python version is failing. It also potentially gets you more parallelism.
Build everything on push to master.
Only build Linux 3.8 and 3.11 wheels on PRs.
… docs
Adding configurations for sphinx-copybutton in conf.py https://sphinx-copybutton.readthedocs.io/en/latest/use.html#using-regexp-prompt-identifiers
Description
Checklist - did you ...
- [x] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Earlier this year, Arch Linux changed the structure of their repos as announced in this post. This PR updates the link in editors.md that points to an Arch Linux package to point to the package's new location.
I do not know if this warrants a changelog entry.
- [ ] Add an entry in
CHANGES.mdif necessary? - [ X ] Add / update tests if necessary?
- [ X ] Add new / update outdated documentation?
Hello there! I am looking at https://github.com/psf/black/pull/3796 that, I'd expect, would give this
from typing import overload
...
@overload
def tesy(children: None) -> None: ...
@overload
def tesy(children: str) -> list[dict[str, str]]: ...
def tesy(children: str | None) -> None | list[dict[str, str]]:
if children is not None:
return None
return [{"options": "str"}]
...
as output format. However, that's not the case.
What am I missing? 😕
i think this applies only to stub files (.pyi)
no, stubs are already formatted like this
from Preview.dummy_implementations in self.mode i can infer that you should enable dummy_implementations preview feature
i dont see a way to enable only specific features from cli
i guess --preview will do the thing
b2f03f9 Bump sphinx from 7.2.3 to 7.2.5 in /docs (#3882) - dependabot[bot]
14f60c8 Bump docutils from 0.19 to 0.20.1 in /docs (#3699) - dependabot[bot]
Eeeey, you are right!
My boss wants to implement Black but also believes that Black may introduce bugs. I shared Black's AST documentation but was told that “there's plenty of places in the existing codebase where a bad import of a typo essentially causes a broken AST”.
Perhaps my understanding of syntax trees is incorrect? I thought that with an identical AST, the introduction of bugs would be impossible.
no idea what your boss is saying there
your understanding is basically correct
Woah, you're Jelle.
Thank you for the reassurance. It especially means a lot coming from you.
do I know you?
Probably not, but I recognize your name as a Black dev... and maybe from IRC or the Python mailing list? Not sure.
The maintainers are around in this channel :)
probably the mailing list then 🙂
Quick search didn't show anything - Anyone had flake8 E704 + black fun - I just did on some bandersnatch code - Had to do this to ignore the formatting: https://github.com/pypa/bandersnatch/pull/1545/commits/ec8e1fa9b7e62249e0f69fc088445545d586ac00
Multiple statements of a function definition should be on their own separate lines.
GitHub
updates:
github.com/psf/black: 23.7.0 → 23.9.1
Can't see if E704 is new or not ... wonder why this has only just come up 😐
Or did we just add the merge it to the same line?
Ahh right we did ... Lets see if I can find the reasoning ...
yeah, you're looking at https://github.com/psf/black/pull/3796. Black's formatting here is still PEP-8 compliant, maybe we can get pycodestyle to change their logic. E704 isn't part of its default config...
Describe the style change
There exist a number of functions in numpy and other libraries that use numpy arrays that allow you to specify the array shape directly as function args rather than as a tuple. It is common style when calling such functions with the intent of specifying a single dimension to add a trailing comma. This is such that the argument list is read as a shape and looks like what would be returned when looking at .shape. Unfortunately this plays badly with Black's mag...
Afaik the default "non-fast" implementation is super paranoid and loads the resulting and original code into the standard python library AST and compares them to make sure they are exactly identical.
I would not worry about black introducing bugs, except when it has questionable taste in formatting and that can trip up humans :P
We recommend to enable it: https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#flake8
This preview style change https://github.com/psf/black/pull/3796 violates E704 as implemented by flake8 / pycodestyle.
Note that the change itself is PEP 8 compliant. E704 is disabled by default in pycodestyle for this reason. ruff intentionally does not implement E704, instead explicitly deferring to Black on this question.
At a minimum we should update https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#flake8 , but figured I'd open an issue to discuss.
good point! let's discuss on issue tracker https://github.com/psf/black/issues/3887
We'll should probably edit our own .flake8 config (or delete it + move to pyproject.yaml which I think is supported now)
I guess time to update my many projects with this too and live with this formatting change.
Describe the bug
Black miscomputes the horizontal space required for lines containing the power operator ** with simple operands, and rewraps them much earlier than the expected 88-character line length. Perhaps that computation wasn’t adjusted for #538/#2726.
To Reproduce
Run Black with no arguments on this code ([playground link](https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ADWAE5dAD2IimZxl1N_WmyLkeQ19WvnMnMLiOdunzNME7LTg9q9ETmvUb3zADAfw...
5a0615a Bump sphinx from 7.2.5 to 7.2.6 in /docs (#3895) - dependabot[bot]
34ed4cf Bump docker/build-push-action from 4 to 5 (#3894) - dependabot[bot]
ab92daf Bump docker/login-action from 2 to 3 (#3891) - dependabot[bot]
edf66ba Bump docker/setup-buildx-action from 2 to 3 (#3... - dependabot[bot]
f5990e8 Bump docker/setup-qemu-action from 2 to 3 (#3890) - dependabot[bot]
7316a79 Bump actions/checkout from 3 to 4 (#3893) - dependabot[bot]
Description
I noticed that the README mentioned Black requiring Python 3.7+ to run even though runtime support for Python 3.7 was dropped in #3765. Running git grep '3\.7' revealed a few more outdated mentions.
Checklist - did you ...
- [x] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Is this related to a problem? Please describe.
The documentation currently doesn't share that python 3.11 is supported as part of the -t, --target-version flag.
Describe the solution you'd like
Add the relevant changes to display py311 as the supported version.
Describe alternatives you've considered
Additional context
Description
The documentation currently doesn't share that python 3.11 is supported as part of the -t, --target-version flag. This PR updates the documentation to support that.
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Thanks! Let's remove py37 from the list while we're at it.
Also, let's add a sentence like "You can run black --help and look for the --target-version option to see the full list of supported versions." so it's less likely we have to update this again.
Description
The simple way of resolving #2316
I would personally prefer to resolve it by formatting it without added parentheses (i.e. option 3 in #2316) to save two unnecessary lines, but that will likely require a bunch of messing around in _maybe_split_omitting_optional_parens and could also impact some other cases - so I think I'll hold off on that one for the moment.
I had to extend maybe_make_parens_invisible_in_atom to also allow expressions, I was quite afraid this would ...
Describe the style change
Currently Black has no opinion on the number of empty lines that may exist between the module's docstring and the first import line other than collapsing down to at-most-2. I'm proposing that Black should have an opinion, specifically that there should be none (in line with the general philosophy on empty lines).
Examples in the current Black style
All three examples are left unchanged:
"""My super cool module"""
from __future__ ...
!help site
Query "site" not found.
Did you mean:
filter
stream
!help
Someone here might know - is there any nogil runtime build available anywhere for testing it out? I’d like to compare black’s multiprocess approach to real Gil-less threads and see on a decent size repo if there is any different - or using diff shades …
I have 4 hours in an airport tomorrow and this could kill some time
Ahh - docker nogil/python is prob the easiest way. cool
do checkout and give opinions ty 🙂
@viscid vale
all the best buddy
did it hurt when u fell from heaven
Description
As I've been working more on Black, I found the full printing of AST trees to be cumbersome and insanely long, but it regardless often containing necessary info, so I ended up modifying the value of that variable a lot and/or temporarily splitting tests into shorter files. But realized that diff-printing would solve that issue, and while not as simple as I'd initially hoped it wasn't too tricky to work out and is turning out very useful.
Checklist - did you ...
...
Thanks! Left some comments mostly on wording.
9b82120 add support for printing the diff of AST trees ... - jakkdl
e.g. https://github.com/psf/black/actions/runs/6339994960/job/17220443288#step:17:18
The underlying error is:
error: command 'clang-12' failed: No such file or directory
Possibly we need to use a newer version of clang somewhere.
Is your feature request related to a problem? Please describe.
Its often problematic, that you format dictionaries with new comma at the end of each iterator, exg.:
{
"any_list": [
"hans",
"franz", <--- comma to much for json format
]
}
The Problem is, that you finally cant copy your dicionary into a json-file, because the syntax doesnt fit anymore.
Describe the solution you'd like
format dictionaries in a style, that obyes json syntax.
dam you is that really an issue to be opened dumb
Describe the bug
As per this stackoverflow: https://stackoverflow.com/questions/70961301/black-not-formatting-python-code-from-vscode
And this one: https://stackoverflow.com/questions/66846608/attributeerror-module-secrets-has-no-attribute-token-bytes
Having a 'secrets.py' present in the repository causes black to fail on startup within VSCode
To Reproduce
- Setup black to run in VSCode (use the black formatter extension)
- Add a file names 'secrets.py' to the project
...
Updated dependencies
Description
Just updated the dependencies of the project
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
Description
This pull-request adds fuzzy testing with pysource_codegen and pysource_minimize
Bugs found so far:
- #3678
- #3677
- #3676
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
Description
Closes #3756
👋 First PR c:
StringParenStripper was getting an input that looks like this:
Line(leaves=[Leaf(LSQB, '['),
Leaf(LPAR, '('),
# ⬇️ this leaf's ID is the key of the comment
Leaf(STRING, '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'),
Leaf(RPAR, ')'),
Leaf(COMMA, ','),
Leaf(RSQB, ']')],
comments={4348688720: [Leaf(COMMENT, '# aaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
a91eb73 Fix comments getting removed from inside parent... - spagh-eddie
Description
Closes #3851
Please let me know if I should be going along a different path c^:
I had originally intended to make a visit_yield_expr = partial(v, keywords={"yield"}, parens={"yield"}) to be more similar to return alongside this, but that doesn't work since yield can be an expression but return can't. https://github.com/psf/black/blob/1b08cbc63400a8b8e0fbb620b6b2a4dab35e1e7b/src/black/linegen.py#L527
Checklist - did you ...
- [ ] Add an entry in `C...
Description
Closes https://github.com/psf/black/issues/2999 regarding our recommendation for SublimeText users.
Checklist - did you ...
- [x] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
Thanks. Sounds like the correct advice here.
Description
Black default job summary isn't really readable, since Python code will be converted 1:1 in Markdown, leading to something like this: https://github.com/csalerno-asml/test-black-repo/actions/runs/6380983558#summary-17316480770
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
i use sublack on ST4 and dont see any problems
there is another extension that uses LSP, right? what are advantages?
I ran this diff also on our internal codebase, which very heavily uses yield because of https://github.com/quora/asynq. Most of the changes are clear improvements, but I noticed a few cases like this:
- content_type = yield (
- a.model.tribe.item.TribeItem(tribe_item_id).get_content_type.asynq()
- )
+ content_type = yield a.model.tribe.item.TribeItem(
+ tribe_item_id
+ ).get_content_type.asynq()
Here the old formatting see...
I like this idea. Makes a lot of sense.
I do wonder tho, can we break people who might be already parsing the output? If so, should we consider making a config parameter (if possible) to disable this code block new default?
Description
Fixes #3018
I started out with modifying left_hand_split, doing a comprehensive rewrite of it to fully handle all manner of possible cases (see the length of the test file). But after reading more of the existing source code I started realizing that I was duplicating functionality, and in fact the only real thing that needed fixing was making sure that right_hand_split was applied to the function definition first when there's a trailing comma.
I also found a ...
Is the difference in spacing around = depending on whether there's a type hint intentional?
def foo(x=5): ...
def foo(x: int = 7): ...
I think PEP-8 specifies that
Yes, it's covered in this section: https://peps.python.org/pep-0008/#other-recommendations
Python Enhancement Proposals (PEPs)
Description
I run pylsp-mypy in my dev environment, which screams loudly about all the illegal stuff being done in tests/data/.*. So excluding those files in the mypy config would be a nice QoL for me, and probably other users as well, while making no difference to the CI.
Checklist - did you ...
None of the below
- [x] Add an entry in
CHANGES.mdif necessary? - [x] Add / update tests if necessary?
- [x] Add new / update outdated documentation?
thanks!
kinda weird, but gtk
Describe the style change
Black excludes one-tuples (1,) and single-item subscripts with trailing comma tuple[int,] from magic comma handling, because unlike in list literals etc. the comma here is required, so cannot be used to distinguish between the user's desire to single-line or multi-line.
The single-line format chosen by Black is the desired behavior for "actual" one-tuples, but is not the desired behavior for "currently 1 item but maybe more in the future" tuples.
**E...
947bd38 [pre-commit.ci] pre-commit autoupdate (#3915) - pre-commit-ci[bot]
Ok, I thought this was for human consumption only, was just checking we were not missing anything.
In that case lets add a change log entry so we show somewhere we've decided to make this change and rebase this and I'll merge.
Thankyou for your contribution!
Has anyone seen Docker build fails with aiohttp of late? Seems we're hitting them:
61.88 Failed to build aiohttp
61.88 ERROR: Could not build wheels for aiohttp, which is required to install pyproject.toml-based projects
https://github.com/psf/black/actions/runs/6396305404/job/17361811411
Compilation failing
#15 49.56 aiohttp/_websocket.c:3293:53: error: ‘PyLongObject’ {aka ‘struct _longobject’} has no member named ‘ob_digit’
possibly a python 3.12 thing?
O yeah, the container has probably moved to 3.12
Yeah that's a Python 3.12 thing
I guess we should open an issue and hard pin us to 3.11 until that's fixed for aiohttp ?
aiohttp/yarl need to cut a release and regenerate their Cython stuff I think
yeah possibly. Pretty unfortunate as Black itself should work fine with 3.12, it's just blackd that needs aiohttp
Ya, and unfortunately we need to test it 😐
Well, install [d] in the container unless we make separate containers
But running blackd in a container is a good use case and thus why I did it
True, let's just keep the Docker build on 3.11 for now then. https://github.com/aio-libs/aiohttp/issues/7639 seems like the relevant issue
and apparently their releases are blocked by some nodejs thing?
Describe the bug
Docker is failing to build aiohttp with python 3.12.
To Reproduce
Build docker with a 3.12 base image
Expected behavior
Docker to build
Environment
Black's version: [main]
OS and Python version: Debian/3.12
Additional context
This is due to aiohttp being used for blackd
Description
The documentation for formatting Python in Visual Studio Code was moved from the Editing page to the new dedicated Formatting page.
The change occured sometime between July and September. Link to the archived version of the Editing page.
...
Description
The documentation for formatting Python in Visual Studio Code was moved from the Editing page to the new dedicated Formatting page.
The change occured sometime between July and September. Link to the archived version of the Editing page.
...
36078bc respect magic trailing commas in return types (... - jakkdl
Currently, our test cases are executed with different flags based on some combination of their filename and what directory they are in. I would prefer a system where we define the flags in a directive inline, e.g. like this:
# flags: --target-version=py38 --preview
1+1
# output
1 + 1
Is this related to a problem? Please describe.
Setting exclude option will override the exclusions from .gitignore. Need to use the extend-exclude instead. This is not well documented in the current user guide, with the assumption that the user will know the caveats.
Describe the solution you'd like
Update the documentation to explicitly saying that the exlcude option will...
6c88e8e Update link to VS Code formatting instructions ... - jake-anto
3a2d76c Remove $, >>> and other prompt prefixes whe... - david-lev
The preview style moves a comment on one line of an implicitly concatenated string to a different line.
$ python -m black --version
python -m black, 23.9.1 (compiled: yes)
Python (CPython) 3.9.14
$ python -m black --diff --preview movecomment.py
--- movecomment.py 2023-10-06 01:40:10.153647+00:00
+++ movecomment.py 2023-10-06 01:40:41.077230+00:00
@@ -1,7 +1,7 @@
xxxxxxx.xxfx(
- f"xxxx_xxxxxxxx: " # xxxxxxx
+ "xxxx_xxxxxxxx: "
f"xxxxx={xxxx_xxxxxxxx.x...
I feel we're going to get a lot of complaints if we actually turn on expterimental string processing by default in January
yeah, i agree. impl isn't really there yet; i went through issues a while back and 3/4 of the preview style bugs are esp related
the trouble is we've been in this state for years 😦
i think is okay to push it out if controversial, i think less okay to push it out if buggy and controversial
especially since folks people who are okay with the bugs have preview. i'm also not seeing folks really clamouring for the feature, so seems fine to get there when we get there?
but i'll try to take a look at some of the bugs some time!
Describe the bug
Black 23.9.1 reformat already clean but complex code expression and creates a line too long.
The original expression does not have lines longer than 79 characters.
But black reformat it and creates a line of 94 characters.
To Reproduce
Create a file named bug.py with the following code :
if True:
if True:
if True:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr = (
abcdefghijklmnopqrstuvwxyzabcd
...
From the test cases in https://github.com/psf/black/blob/main/tests/data/preview/multiline_strings.py, I guess this is a deliberate choice, but I really don't like the way the preview style will do things like this:
diff --git a/foo.py b/foo.py
index 14dda3925..f2e71baec 100644
--- a/foo.py
+++ b/foo.py
@@ -2,10 +2,8 @@ from textwrap import dedent
def foo():
- return dedent(
- """
+ return dedent("""
foo
bar
baz
- """
- )
+ """)
I don't mind moving the opening """ so much, but having the closing ) be on the same line as the closing """ feels really inconsistent with black's style everywhere else, to me at least
Describe the bug
This was first reported in #2699, and thought resolved in #2990, except that only solved the specific case of the function having no parameters.
To Reproduce
Run the test return_annotation_brackets_string:
The second function should be formatted exactly the same as the first one.
I feel like it looks better if the closing triple quotes are dedented, but Black can't do that because it would change the string
3457ec4 Update output display to job summary (#3914) - csalerno-asml
Until we get new aiohttp wheels we need to build with 3.11. You can see an example of a fail here:
Workaround for #3919 - Will leave it open until we can move to 3.21
Is your feature request related to a problem? Please describe.
WE don't mypyc the black install in our published Docker container.
Describe the solution you'd like
mypyc compile black.
Describe alternatives you've considered
Well, we do the alternative standard native python install today :D
Additional context
Compiling is faster.
Will take pointers on the issue of best way to do this or PRs for this one 😄
Looking goof for the docker action. Should stop you all getting the scary/ugly red X for now
Gosh our build pipeline is impressive. haha
Describe the style change
The suggested style change regards the type parameter syntax introduced in Python 3.12.
When formatting lines too long, I think line-wrapping should occur in the arguments' parenthesis rather than in the type parameters' brackets.
Examples in the current Black style
This would work with any generic function using the new type parameter syntax with a long enough signature (longer than the line-length).
type SupportedDataTypes =...
Description
Resolves #2316 according to the nextline strategy. The poll is currently 10 to 6 of favor of this style, which certainly isn't a slam-dunk, but enough for me to implement it and we can progress the discussion after seeing impacts on existing code.
This is a pretty messy implementation, as can be seen by the sheer length and the complexity check having made me reformat my code several times to break out functions.
I don't love the existence of _split_first_typehint a...
Thanks, but several of the test cases look quite bad to me. I'd prefer option (1) in Sebastian's issue: parenthesize it.
Hi, I'd like to contribute something to Black as part of hacktoberfest. Should I start from good-first-issues, or are there particular issues to be looked at for the fest?
Description
Resolves https://github.com/psf/black/issues/1872.
This is a rebase of https://github.com/psf/black/pull/2996 and therefore most credit should go to @jpy-git.
I have excluded the refactoring that was included in the rebase attempt at https://github.com/psf/black/pull/3287. I feel the refactoring makes the PR unnecessarily hard to review and rebase. It's probably a very desired refactor (I can't comment on this since I don't know the codebase too well), but it ...
running into this kind of thing quite often. also looks like we don't always report tracebacks in verbose mode. i've tracked it down to some issue with the grammar pickles, but TBD...
okay seeing a diff in Grammar3.11.5.final.0.pickle in the good case vs bad case
In [17]: set(a["symbol2number"]) - set(b["symbol2number"])
Out[17]: set()
In [18]: set(b["symbol2number"]) - set(a["symbol2number"])
Out[18]:
{'paramspec',
'type_stmt',
'typeparam',
'typeparams',
'typevar',
'typevartuple'}
we've dropped python 2 support yeah
but huh there are really no reports of this grammar cache bug on issue tracker
Support for formatting Python 2 was dropped in 22.0 / https://github.com/psf/black/pull/2740
I think it's time that we do some cleanup. In particular, I was investigating some issue with the Grammar pickles and seems worth simplifying pygram.initialize
Describe the style change
For a long list, black will format it into each element occupies one row.
Examples in the current Black style
Before black format
idx = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39
]
After black format
idx = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
...
a8f68f5 Bump pypa/cibuildwheel from 2.16.1 to 2.16.2 (#... - dependabot[bot]
715f60c Drop support for parsing Python 2 (#3933) - hauntsaninja
Previously these were swallowed (unlike the ones in black/init.py)
a69bda3 Use inline flags for test cases (#3931) - JelleZijlstra
5d5bf6e Fix cache versioning when BLACK_CACHE_DIR is se... - hauntsaninja
This is really cool :)
idea shamelessly stolen from mypy 🙂
Thanks! Pushed a change moving the tests as I just changed the structure of the tests.
7aa37ea Report all stacktraces in verbose mode (#3938) - hauntsaninja
should we be thinking about 23.10?
we have had quite a few changes since 23.9, so why not?
I don't think there's other PRs that are close to being mergeable, maybe the mypy one
great!
Hi, I'd like to contribute something to Black as part of hacktoberfest. Should I start from good-first-issues, or are there particular issues to be looked at for the fest?
Posted earlier, looks like it got missed. Apologies in advance!
Thanks for your interest in contributing! The "good first issue" label is a good place to start, looks like most issues in there are docs-related
I've added some documentation to document the different installation methods and binaries. Made a draft pull reqest for it here - https://github.com/psf/black/pull/3941
Thanks. This looks correct, but we'll have to make the change in the preview style only for now.
Describe the style change
Examples in the current Black style
def f():
"Make sure this code is blackened"""
pass
Desired style
def f(
):
pass
Additional context
the weirdest issue I've seen in a while 
wtf
?
I'm looking into the different extras that can be installed with Black, and I'm a little confused about the two undocumented ones.
- jupyter - For formatting Jupyter notebooks. (Documented)
- d - Run Black as a server. (Documented)
- colorama - Enables colored diffs
- uvloop - Speeds up Black when formatting multiple files concurrently
My doubts:
- Did I miss any extras?
- Am I correct about the descriptions of the last two?
Describe the style change
Currently, black preserves the blank line before regular doc strings, but not before raw docstrings. It should preserve them before both.
Input
class TextIOWrapper(TextIOBase):
r"""Character and line based layer over a BufferedIOBase object, buffer.
[...]
"""
_CHUNK_SIZE = 2048
class TextIOWrapper(TextIOBase):
"""Character and line based layer over a BufferedIOBase object, buffer.
[......
When i try to run black over cpython it just seems to hang at some point. Is there any good way to debug this? The backtrace only shows me return self._semlock.__enter__() and -v unfortunately doesn't tell me which files it started to debug
Hi,
I've switched to VS code and pyright recently, and I realized black is breaking a lot of # pyright: ignore comments.
Input (both are valid for pyright):
asm_client: SecretsManagerClient = boto3.client("secretsmanager") # pyright: ignore[reportUnknownMemberType]
asm_client: SecretsManagerClient = boto3.client( # pyright: ignore[reportUnknownMemberType]
"secretsmanager"
)
Output:
asm_client: SecretsManagerClient = boto3.client(
"secretsman...
That sounds about right. uvloop should also speed up blackd
I'm trying it out now, I just put a print statement in format_file_in_place before it starts to print anything. A good alternative would be to use -W 1 and -v: it will run in alphabetical order and print after each file, so it should be easy to figure out which file is causing the hang
it might be Lib/test/test_traceback.py
oh I bet it's because of the function that has 800 locals assigned in a single assignment statement
Lib/test/test_traceback.py line 3470
a791 = a792 = a793 = a794 = a795 = a796 = a797 = a798 = a799 = a800 \```
Bruh
it did eventually finish for me though. Running on CPython main I got 1662 files reformatted, 152 files left unchanged, 18 files failed to reformat.
oh lol test_traceback crashed with error: cannot format /Users/jelle/py/cpython/Lib/test/test_traceback.py: maximum recursion depth exceeded
ah thanks, i didn't wait long enough (results with -W 1 -v):
1651 files reformatted, 149 files left unchanged, 23 files failed to reformat.
real 37m51,358s
user 37m14,769s
sys 0m1,981s
Fixes #3790
Slightly hacky, but I think this is correct and it should also improve performance somewhat.
^ huh, I opened a few other PRs earlier and they didn't show up here
@elder tusk would you mind reviewing psf/black#3947 psf/black#3949 psf/black#3950 when you get a chance? after that I think we should release
3950 is most important since it fixes a parsing regression
I was looking into substituting the supported Python version in the docs, and it looks like it'll require an extra Myst extension, as documented in https://myst-parser.readthedocs.io/en/latest/syntax/optional.html#substitutions-with-jinja2. Will this be required for my PR?
(Also noticed that main got updated, so will update my fork before submitting the actual PR)
I'd be open to adding an extension
Alright, I'll add it to the PR.
Not particularly familiar with blib2to3, but heuristic seems fine
bb58807 Fix parser bug where "type" was misinterpreted ... - JelleZijlstra
I'll go climb a hill and then make the actual release (couple things still running in https://github.com/psf/black/actions)
https://pypi.org/project/black/23.10.0/ did go out, seems like the GH integration here is getting flaky
we maybe could have made 3.12 wheels now that mypy 1.6 is out? I forgot the exact blocker
anyway, that can wait for the next release
I've added the code, please have a look when possible.
Is your feature request related to a problem? Please describe.
I like black's default of 88-char line length, except for docs/ renderings, where code blocks may be rendered via Sphinx with 79 char width. This leads to unnecessary scrolling left/right to see the last 9 chars.
It would be useful to enable black to have sub-configurations. This was requested in https://github.com/psf/black/issues/1370, and was postponed into the future.
Describe the solution you'd like...
Nah, the fix didn't make it into 1.6, I don't think
https://github.com/python/mypy/commit/940fceb59a55513913c3c9c1eaf89a7f5ee774d6 was the fix, only merged two days ago
yeah i made sure to merge my PR in time for jukka's promised 1.7 fast follow
Is there any interest in/ability to have those working on black take ownership over the pytest-black plugin? The project has been abandoned for about 3 years now: https://github.com/shopkeep/pytest-black/issues/70
Can't speak for other maintainers, but probably not. I don't use it and I don't see the idea of running black through pytest as very useful
Black v23.10.0
Options
--line-length=88
--safe
--preview
Input
class A:
attr1: str = 'this_is_very_very_long_this_is_very_very_long_this_is_v...
input:
f(
"lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor"
"incididunt ut labore et dolore magna aliqua Ut enim ad minim",
"",
)
Expected output:
f(
(
"lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor"
"incididunt ut labore et dolore magna aliqua Ut enim ad minim"
),
"",
)
Actual:
No change
Related:
...