Add start/end params to str.startswith/endswith, allow named default in dict.pop#172
Add start/end params to str.startswith/endswith, allow named default in dict.pop#172twitchax wants to merge 1 commit intofacebook:mainfrom
Conversation
…in dict.pop
This commit makes three Bazel-compatible improvements to Starlark built-in methods:
1. **str.startswith(prefix[, start[, end]])** — Add optional `start` and `end`
parameters to `startswith()`, matching the Starlark spec and Python semantics.
The substring `s[start:end]` is tested instead of the full string.
2. **str.endswith(suffix[, start[, end]])** — Same as above for `endswith()`.
3. **dict.pop(key[, default])** — Allow `default` to be passed as a named keyword
argument (e.g., `d.pop("key", default=None)`), not just positional. This
matches Bazel's Java Starlark implementation and Python's dict.pop().
These changes address:
- bazelbuild/starlark#56 — the Starlark spec was updated to include start/end
params for startswith/endswith, but starlark-rust never implemented them.
- The known inconsistency documented in the conformance test suite at
bazelbuild/starlark test_suite/testdata/go/dict.star:
`# _inconsistency_: rust fails when default=None`
All existing tests pass. New tests added for each change.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Hi @twitchax! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Full disclosure: identified and implemented mostly with LLM. Hope that's OK. Happy to make artisanal edits. |
There was a problem hiding this comment.
Pull request overview
This PR adds Bazel-compatible enhancements to Starlark string and dict methods by implementing optional start/end parameters for str.startswith() and str.endswith(), and allowing dict.pop() to accept default as a named keyword argument. These changes align starlark-rust with the official Starlark specification and Bazel's Java implementation.
Changes:
- Add optional
startandendparameters tostr.startswith()andstr.endswith()methods, enabling substring range checking - Allow
dict.pop()to acceptdefaultas a named keyword argument (e.g.,default=None) in addition to positional argument - Add comprehensive test coverage for all three enhancements
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| starlark/src/values/types/string/methods.rs | Adds start and end parameters to startswith() and endswith() methods using convert_str_indices() infrastructure; updates documentation with new examples; adds test cases for new functionality |
| starlark/src/values/types/dict/methods.rs | Removes require = pos constraint from dict.pop() default parameter to allow named keyword usage; adds test cases verifying named parameter functionality |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
@cjhopman has imported this pull request. If you are a Meta employee, you can view this in D102404513. (Because this pull request was imported automatically, there will not be any future comments.) |
Summary
This PR makes three Bazel-compatible improvements to Starlark built-in methods:
str.startswith(prefix[, start[, end]])— Add optionalstartandendparameters, matching the Starlark spec and Python semantics. The substrings[start:end]is tested instead of the full string.str.endswith(suffix[, start[, end]])— Same as above forendswith().dict.pop(key[, default])— Allowdefaultto be passed as a named keyword argument (e.g.,d.pop("key", default=None)), not just positional. This matches Bazel's Java Starlark implementation and Python'sdict.pop().Motivation
These changes are needed for Bazel compatibility. Real-world Bazel BUILD files and
.bzlmacros use these features extensively:Related Issues
start/endparams forstartswith/endswith, butstarlark-rustnever implemented them.# _inconsistency_: rust fails when default=NoneImplementation Details
startswith/endswithReuses the existing
convert_str_indicesinfrastructure (already used byfind(),count(),index(),rfind(),rindex()) to slice the string before testing. Supports both single string and tuple-of-strings prefix/suffix arguments.dict.popRemoves
#[starlark(require = pos)]from thedefaultparameter, allowing it to be passed as either positional or named. Thekeyparameter remains positional-only.Testing
test_startswith_with_start_end— 7 assertions covering basic, negative indices, and tuple prefixtest_endswith_with_start_end— 4 assertions covering basic, negative indices, and tuple suffixtest_dict_pop_default_named— 3 assertions covering missing key, existing key, and fallback value