Skip to content

Initialize Leaf bracket depth#5215

Open
michaelsam94 wants to merge 1 commit into
psf:mainfrom
michaelsam94:codex/fix-leaf-bracket-depth
Open

Initialize Leaf bracket depth#5215
michaelsam94 wants to merge 1 commit into
psf:mainfrom
michaelsam94:codex/fix-leaf-bracket-depth

Conversation

@michaelsam94

Copy link
Copy Markdown

Description

Fixes #5214.

Leaf.bracket_depth is annotated and later read by formatting code, but a newly created leaf did not initialize the instance attribute until bracket tracking visited it. This initializes new leaves with depth 0, matching the default pre-bracket-tracking state, and adds a small regression test covering the leaf metadata defaults.

Checklist

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

No style or documentation changes are needed for this crash fix.

Tests

  • .venv/bin/python -m pytest tests/test_pytree.py
  • .venv/bin/python -m pytest tests/test_format.py -k fmtskip_after_bracket_with_comment
  • .venv/bin/python -m black --check src/blib2to3/pytree.py tests/test_pytree.py

@msftcangoblowm

msftcangoblowm commented Jul 1, 2026

Copy link
Copy Markdown

Thank you for addressing this issue so quickly. Not in my wildest dreams did i expect that kinda turnaround.

This PR is sufficient to resolve this particular crash.

But this entire class of issues can all be resolved! 100s or 1000s of them.

  • Proper (authoritative) stubs (not just typing within code modules)
  • stubtest
  • static typing (not runtime) tests using pytest-mypy-plugins

For example, before assert leaf.bracket_depth == 0, a runtime value check, should be a static typing check:

from typing_extensions import assert_type
assert_type(leaf.bracket_depth, int)

Then continue for the entire Leaf interface, not just the one attribute.
This static typing tests live within pytest-mypy-plugins yml files.

Of course, rtfm pytest-mypy-plugins. Can then visit types-strictyaml to see more realistic static typing tests.

@cobaltt7

cobaltt7 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks! One nit: could you put the test in an existing file instead of making a new one? Please also add a python test case file that provides an example of code that crashes without this fix.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (f651f39) to main (c4c9a93):

--preview style: no changes

--stable style: no changes


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

@msftcangoblowm

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leaf.bracket_depth not initialized

3 participants