Skip to content

python's args regex only balances one level of nested parens, silently zeroing args for deeply-nested default values #1527

Closed

Description

@squid-protocol

What

Found while chasing the remaining ~1.4% args_exact_match gap after #1524/#1525 fixed the
tree-sitter audit tool's own node-type undercounting (63.0% → 98.6%). A separate, smaller slice of
the remaining mismatches (all 12 in language-crucible/data/python/fastapi/tests/test_dependency_partial.py)
is a real limitation in GitGalaxy's own engine, not the audit tool.

Python's args rule in gitgalaxy/standards/language_standards.py:

re.compile(
    r'(?:async[ \t]+)?def[ \t]+(\w+)(?:\[(?:[^\[\]]|\[[^\[\]]*\])*\])?[ \t]*(\((?:[^()]|\([^()]*\))*\))|\blambda\b[ \t]*([^:]*):',
    re.MULTILINE,
)

The parameter-list capture group \((?:[^()]|\([^()]*\))*\) balances exactly one level of
nested parens (outer paren + one inner non-nested paren group). A default value with two levels of
nested calls -- e.g. FastAPI's dependency-injection idiom:

async def get_partial_callable_gen_dependency(
    value: Annotated[
        str,
        Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency")),
    ],
) -> str:

(Depends( is depth 1, partial( is depth 2) doesn't match at all -- the whole def statement is
skipped by the args regex (confirmed via direct finditer against the real file: zero matches
for this function). func_start's simpler anchor still finds the function name (so it shows up as
"found" against tree-sitter ground truth), but args_count silently defaults to 0 instead of the
real 1.

Scope / context

Verified this isn't unique to python -- JS/TS's own args rules use \([^)]*\), i.e. zero
levels of nesting, an even tighter bound. This looks like a deliberate ReDoS-safety tradeoff
(bounded nesting depth instead of unbounded recursive matching) rather than an oversight, so a fix
here is a real regex-engine change to weigh against that tradeoff, not a mechanical whitelist
addition.

Why this is its own issue, not part of #1524/#1525

#1524/#1525 fixed a bug in the audit tool's ground-truth measurement (a test-only file). This
one is in GitGalaxy's actual production parsing logic (language_standards.py), so per this repo's
"Differential Scan" PR protocol any fix here needs crucible_check.py verification and a
golden_master_audit.json/golden_master_zero_dep_audit.json re-bless before it can ship --
deliberately scoped out of the smaller, lower-risk audit-tool fix.

Suggested direction (not a full design, needs its own pass)

Extending the capture group to balance two levels while keeping it bounded (e.g.
\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\)) is the mechanical fix, but real-world signatures aren't
bounded to any fixed nesting depth in general (three+ levels would still break), so this only
narrows the gap rather than closing it outright -- worth deciding whether one more level is worth
the added regex complexity/backtracking cost before implementing, or whether this should be
documented as an accepted limitation the way PEP 695's whitespace gap already is in
docs/language_status/python.md §5.

Activity

  1. added
    bug
    core-engine
    python
    priority: low
    on Aug 14, 2026
  2. added this to the v2.4.8 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginepriority: lowpython

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions