Commit c83f219
Add start/end params to str.startswith/endswith, allow named default in dict.pop (#172)
Summary:
This PR makes three Bazel-compatible improvements to Starlark built-in methods:
1. **`str.startswith(prefix[, start[, end]])`** — Add optional `start` and `end` parameters, matching the [Starlark spec](https://github.com/bazelbuild/starlark/blob/master/spec.md#string%C2%B7startswith) 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()`.
## Motivation
These changes are needed for Bazel compatibility. Real-world Bazel BUILD files and `.bzl` macros use these features extensively:
```python
# startswith with start/end (common in monorepo BUILD files)
[b for b in GLOBAL_BINARIES if b.startswith("//", 0, 2)]
# dict.pop with named default (common in rule macros)
workdir = kwargs.pop("workdir", default = None)
```
## Related Issues
- **[bazelbuild/starlark#56](bazelbuild/starlark#56 — The Starlark spec was updated to include `start`/`end` params for `startswith`/`endswith`, but `starlark-rust` never implemented them.
- **Conformance test suite inconsistency** — The [bazelbuild/starlark conformance tests](https://github.com/bazelbuild/starlark/blob/master/test_suite/testdata/go/dict.star) document this with: `# _inconsistency_: rust fails when default=None`
## Implementation Details
### `startswith` / `endswith`
Reuses the existing `convert_str_indices` infrastructure (already used by `find()`, `count()`, `index()`, `rfind()`, `rindex()`) to slice the string before testing. Supports both single string and tuple-of-strings prefix/suffix arguments.
### `dict.pop`
Removes `#[starlark(require = pos)]` from the `default` parameter, allowing it to be passed as either positional or named. The `key` parameter remains positional-only.
## Testing
- All 981 existing tests pass (828 lib + 153 doc)
- New tests added for each change:
- `test_startswith_with_start_end` — 7 assertions covering basic, negative indices, and tuple prefix
- `test_endswith_with_start_end` — 4 assertions covering basic, negative indices, and tuple suffix
- `test_dict_pop_default_named` — 3 assertions covering missing key, existing key, and fallback value
Pull Request resolved: #172
Reviewed By: IanChilds
Differential Revision: D102404513
Pulled By: cjhopman
fbshipit-source-id: 61ef8f00e530a17cb8e5653aba6d7d8ef394f9ee1 parent 58ce6c1 commit c83f219
2 files changed
Lines changed: 61 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| |||
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
388 | 396 | | |
389 | 397 | | |
390 | 398 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
225 | | - | |
| 224 | + | |
| 225 | + | |
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| 230 | + | |
| 231 | + | |
230 | 232 | | |
231 | 233 | | |
232 | 234 | | |
233 | 235 | | |
234 | 236 | | |
235 | 237 | | |
| 238 | + | |
| 239 | + | |
236 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
237 | 245 | | |
238 | | - | |
239 | | - | |
| 246 | + | |
| 247 | + | |
240 | 248 | | |
241 | 249 | | |
242 | 250 | | |
| |||
1121 | 1129 | | |
1122 | 1130 | | |
1123 | 1131 | | |
1124 | | - | |
1125 | | - | |
| 1132 | + | |
| 1133 | + | |
1126 | 1134 | | |
1127 | 1135 | | |
1128 | 1136 | | |
| |||
1131 | 1139 | | |
1132 | 1140 | | |
1133 | 1141 | | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
1134 | 1146 | | |
1135 | 1147 | | |
1136 | 1148 | | |
1137 | 1149 | | |
1138 | 1150 | | |
1139 | 1151 | | |
| 1152 | + | |
| 1153 | + | |
1140 | 1154 | | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
1141 | 1159 | | |
1142 | | - | |
1143 | | - | |
| 1160 | + | |
| 1161 | + | |
1144 | 1162 | | |
1145 | 1163 | | |
1146 | 1164 | | |
| |||
1309 | 1327 | | |
1310 | 1328 | | |
1311 | 1329 | | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
1312 | 1356 | | |
0 commit comments