Skip to content

[Repo Assist] [Python] Fix String.IndexOf/LastIndexOf ignoring StringComparison argument#4468

Merged
dbrattli merged 4 commits intomainfrom
repo-assist/improve-py-indexof-stringcomparison-2026-03-548762428ccbf4c1
Apr 2, 2026
Merged

[Repo Assist] [Python] Fix String.IndexOf/LastIndexOf ignoring StringComparison argument#4468
dbrattli merged 4 commits intomainfrom
repo-assist/improve-py-indexof-stringcomparison-2026-03-548762428ccbf4c1

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

  • Fixes String.IndexOf(str, StringComparison) in Python: the StringComparison enum value was being passed through to Python's str.find() as a start-index, silently producing wrong results (e.g. "abcdbc".IndexOf("b", StringComparison.Ordinal) returned 4 instead of 1)
  • Fixes String.IndexOf(str, startIndex, StringComparison) in Python: previously caused a compile error ("The only extra argument accepted…")
  • Same fixes for String.LastIndexOf
  • Adds four regression tests to tests/Python/TestString.fs

Root cause: The Python Replacements.fs pattern-matched on argument types but had no cases for StringComparison enum values, so (String, Ordinal) matched the (String, Int32) pattern (treating the enum as a start-index) and (String, Int32, Ordinal) fell through to the error case.

Fix approach: Mirror the JS/TS target: detect StringComparisonEnumValue args and filter them out before calling the underlying Python method. Ordinal is the default Python string comparison, so this is semantically correct for Ordinal. Note that OrdinalIgnoreCase is still not supported (same limitation as JS/TS — requires a separate implementation).

Test plan

  • Run ./build.sh test python to verify the four new tests pass
  • Verify no other Python string tests regress

Related

🤖 Generated with [Claude Code]((claude.ai/redacted)

Note

🔒 Integrity filtering filtered 183 items

Integrity filtering activated and filtered the following items during workflow execution.
This happens when a tool call accesses a resource that does not meet the required integrity or secrecy level of the workflow.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

Previously, calling `String.IndexOf("s", StringComparison.Ordinal)` in
Python would either produce a compile error (three-arg form) or silently
misinterpret the StringComparison enum value as a start-index (two-arg
form, since the enum int value is passed to `find()`).

Add pattern cases to filter out the StringComparison argument, matching
the approach used in the JS/TS target. Also add regression tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added automation Automated changes repo-assist Created by Repo Assist labels Mar 31, 2026
@dbrattli dbrattli marked this pull request as ready for review April 1, 2026 04:52
@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions bot commented Apr 1, 2026

Python Type Checking Results (Pyright)

Metric Value
Total errors 18
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_hash_set.py 3 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 1 Excluded

dbrattli and others added 2 commits April 2, 2026 09:27
Replace 4 duplicated match arms with List.filter to strip StringComparison
args before pattern matching, matching the JS/TS target's approach.
Add IndexOf char with StringComparison test.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dbrattli dbrattli merged commit a6ad738 into main Apr 2, 2026
24 checks passed
@dbrattli dbrattli deleted the repo-assist/improve-py-indexof-stringcomparison-2026-03-548762428ccbf4c1 branch April 2, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Automated changes repo-assist Created by Repo Assist

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant