Skip to content

fix(parse): add parentheses for operator precedence in minItems check#1600

Open
xodn348 wants to merge 1 commit into
anthropics:mainfrom
xodn348:fix/transform-minItems-precedence
Open

fix(parse): add parentheses for operator precedence in minItems check#1600
xodn348 wants to merge 1 commit into
anthropics:mainfrom
xodn348:fix/transform-minItems-precedence

Conversation

@xodn348
Copy link
Copy Markdown
Contributor

@xodn348 xodn348 commented May 25, 2026

What

Adds explicit parentheses to the minItems condition in transform_schema() to clarify operator precedence, and adds missing test coverage for minItems: 0 and minItems: 1.

Why

The condition on line 149 of _transform.py:

if min_items is not None and min_items == 0 or min_items == 1:

evaluates as (min_items is not None and min_items == 0) or (min_items == 1) due to Python's operator precedence (and binds tighter than or). The intended grouping is min_items is not None and (min_items == 0 or min_items == 1).

Both produce identical results for all practical inputs (None, 0, 1, 2+), so this is not a behavioral bug — but the missing parentheses make the intent ambiguous and could mislead future readers or refactors.

Changes

  • src/anthropic/lib/_parse/_transform.py: Add parentheses around the or clause
  • tests/lib/_parse/test_transform.py: Add test_array_schema_min_items_zero and test_array_schema_min_items_one to cover the previously untested code path

Verification

  • Build: pass
  • Tests: pass (15 passed)
  • Lint: pass
  • Type check: pass (pyright 0 errors)

The condition on line 149 of _transform.py evaluated as:
  (min_items is not None and min_items == 0) or (min_items == 1)
instead of the intended:
  min_items is not None and (min_items == 0 or min_items == 1)

While both produce the same result for all practical inputs,
the explicit parentheses clarify intent and prevent future
misreadings. Adds test coverage for minItems 0 and 1 cases.
@xodn348 xodn348 requested a review from a team as a code owner May 25, 2026 05:21
@kiwigitops
Copy link
Copy Markdown

This looks behavior-preserving rather than a parser fix. The original condition already only keeps minItems when the value is 0 or 1; min_items == 1 is false for None, so the added tests would pass on the current code too.

If the goal is readability, it may be clearer to frame this as a small cleanup. If there is an actual failing schema case, the regression test should cover a value that fails before this change.

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.

2 participants