Leaf.bracket_depth not initialized
Describe the bug
This is the exact same bug as #3605 . The original issue did not provide a suggestion on how to resolve the issue.
blib2to3.pytree.Leaf.bracket_depth is annotated as int, but it's never initialized. In black.lines.is_line_short_enough, if leaf.bracket_depth + 1 > len(commas): --> AttributeError.
The resulting error is:
Traceback (most recent call last): File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/init.py", line 939, in reformat_one if changed is not Changed.CACHED and format_file_in_place( File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/init.py", line 982, in format_file_in_place dst_contents = format_file_contents( File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/init.py", line 1118, in format_file_contents dst_contents = format_str(src_contents, mode=mode, lines=lines) File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/init.py", line 1247, in format_str dst_contents = _format_str_once(src_contents, mode=mode, lines=lines) File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/init.py", line 1306, in _format_str_once for line in transform_line( File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/linegen.py", line 746, in transform_line is_line_short_enough(line, mode=mode, line_str=line_str_hugging_power_ops) File "/mnt/sda1/dev_parent/logging_strict/.venv/lib/python3.10/site-packages/black/lines.py", line 1279, in is_line_short_enough if leaf.bracket_depth + 1 > len(commas): AttributeError: 'Leaf' object has no attribute 'bracket_depth' error: cannot format tests/test_registry_config.py: 'Leaf' object has no attribute 'bracket_depth'
Expected behavior
In blib2to3.pytree.Leaf.init explicitly initialize attributes: bracket_depth and opening_bracket.
Run black patched
python run_black.py tests/test_registry_config.py --verbose --diff --target-version=py310
import sys
from blib2to3.pytree import Leaf
# Patch first
if not hasattr(Leaf, '_original_init'):
Leaf._original_init = Leaf.__init__
def patched_init(self, *args, **kwargs):
Leaf._original_init(self, *args, **kwargs)
self.bracket_depth = 0
self.opening_bracket = None
Leaf.__init__ = patched_init
# Import and run Black
from black import patched_main
sys.argv[0] = "black"
sys.exit(patched_main())Environment
- Black's version: 26.5.1
- OS and Python version: Linux py310
Source: psf/black