bytes: support keyword arguments in hex() - #8312
Conversation
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including ⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughAdds a shared ChangesHex options integration
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [ ] test: cpython/Lib/test/test_array.py (TODO: 55) dependencies: dependent tests: (102 tests)
[x] lib: cpython/Lib/zoneinfo dependencies:
dependent tests: (2 tests)
[x] test: cpython/Lib/test/test_list.py (TODO: 4) dependencies: dependent tests: (no tests depend on list) [x] test: cpython/Lib/test/test_module (TODO: 2) dependencies: dependent tests: (no tests depend on module) [ ] test: cpython/Lib/test/test_set.py (TODO: 6) dependencies: dependent tests: (no tests depend on set) [x] test: cpython/Lib/test/test_resource.py (TODO: 4) dependencies: dependent tests: (4 tests)
[x] lib: cpython/Lib/re dependencies:
dependent tests: (81 tests)
[ ] test: cpython/Lib/test/test_memoryview.py (TODO: 8) dependencies: dependent tests: (no tests depend on memoryview) [x] lib: cpython/Lib/weakref.py dependencies:
dependent tests: (222 tests)
[x] lib: cpython/Lib/io.py dependencies:
dependent tests: (108 tests)
[ ] test: cpython/Lib/test/test_bytes.py (TODO: 21) dependencies: dependent tests: (no tests depend on bytes) [x] lib: cpython/Lib/lzma.py dependencies:
dependent tests: (101 tests)
[ ] lib: cpython/Lib/test/support dependencies:
dependent tests: (2 tests)
[x] lib: cpython/Lib/socket.py dependencies:
dependent tests: (101 tests)
[x] lib: cpython/Lib/csv.py dependencies:
dependent tests: (4 tests)
Legend:
|
yaay, can you please remove that test mark as well? |
| #[derive(FromArgs)] | ||
| pub(crate) struct ByteInnerHexOptions { | ||
| #[pyarg(any, optional)] | ||
| pub sep: OptionalArg<Either<PyStrRef, PyBytesRef>>, |
There was a problem hiding this comment.
Does sep strictly allow str or bytes? Otherwise, could you please check if ArgStrOrBytesLike fits better here? ArgStrOrBytesLike allows a little bit more than bytes. So it might not fit.
There was a problem hiding this comment.
Thanks for the suggestion! It was definitely worth double-checking.
sep strictly accepts only str or bytes. This means ArgStrOrBytesLike would be too permissive for this case.
Since ArgStrOrBytesLike falls back to ArgBytesLike for non-string inputs, it accepts any object that implements the buffer protocol (like bytearray, memoryview, and array). CPython explicitly rejects these for sep, so I kept Either<PyStrRef, PyBytesRef> to match CPython's strict type checking exactly.
I also unmarked test_memoryview_hex_separator as requested. I verified it locally and the test passed (test_memoryview -> SUCCESS, run=171).
There was a problem hiding this comment.
Thank you so much for confiriming!
* bytes: support keyword arguments in hex() * bytes: unmark test_memoryview_hex_separator
Summary
bytes/bytearray/memoryview'shex()rejected itssepandbytes_per_separguments when passed by keyword, even though CPython accepts them either
positionally or by keyword.
Cause
hex()took its arguments as bareOptionalArg<T>method parameters, whoseFromArgsonly consumes positionals (take_positional()) and never keywords.The argument names were never registered, so
sep=/bytes_per_sep=stayed inkwargsand trippedcheck_kwargs_empty.Fix
Move the arguments into a
#[derive(FromArgs)]struct (ByteInnerHexOptions)with
#[pyarg(any, optional)]fields, which generatestake_positional_keywordso both positional and keyword forms are accepted — matching CPython's
positional-or-keyword signature (no
/or*in its Argument Clinic definition).Applied consistently to
bytes,bytearray, andmemoryview; the computationstill delegates to the existing
inner.hex(...), so the error cases are preserved(
None→TypeError, separatorlen != 1→ValueError). Re-enablestest_hex_separator_basicsby removing itsexpectedFailuremarker.Test
Verified locally:
cargo run -- -m test test_bytes -v→Tests result: SUCCESS(Ran 317 tests,OK (skipped=15, expected failures=18))test_hex_separator_basics(BytesTest/ByteArrayTest) ... oktest_hex_separator_five_bytes/test_hex_separator_six_bytes/test_hex... okAssisted-by: Claude Code:claude-opus-4-8
Summary by CodeRabbit
bytes,bytearray, andmemoryview..hex()behavior remains unchanged while argument handling is more consistent across these types.