HyukjinKwon opened a new pull request, #873:
URL: https://github.com/apache/spark-connect-rust/pull/873
### What changes were proposed in this pull request?
A follow-up of #854 (SPARK-62010) and #860 (SPARK-62019). The methods of the
containers of the drop-in's Rust protobuf runtime (`pyspark.sql.connect.proto`,
`crates/pyspark-rs/src/pbmsg/containers.rs`) take their arguments as upb's do
(protobuf 7.36.2, the implementation that pyspark-client 4.3.0 uses), with
upb's errors. Before, they had PyO3's arguments and errors. Each case was
probed on both clients.
**Maps.** upb's maps are of its C types `ScalarMapContainer` and
`MessageMapContainer`.
- `MergeFrom`, `clear`, `GetEntryClass` and `get_or_create` are C methods of
one argument (`METH_O`) or of none (`METH_NOARGS`). They take positional
arguments only, with CPython's errors naming upb's class:
- `ScalarMapContainer.MergeFrom() takes exactly one argument (0 given)`;
- `MessageMapContainer.clear() takes no keyword arguments`.
The runtime's errors named `MapContainer`, and `MergeFrom` took an
argument named `other`. These now go through the helpers `no_arguments`,
`one_argument` and `no_keywords`. An argument error of `MergeFrom` comes before
the new version of the map, as upb's comes before its C function runs.
- `setdefault` follows upb's checks in order:
1. it takes no keyword arguments;
2. a map of messages raises its `NotImplementedError`, whatever the
arguments;
3. a map of scalars counts the arguments as `PyArg_UnpackTuple` does
(`setdefault expected at least 1 argument, got 0`).
Before, `setdefault("k", default="d")` set the key.
- `get` parses its `key` and `default` as `PyArg_ParseTupleAndKeywords(args,
kwargs, "O|O", {"key", "default"})` does. Its errors come in CPython's order
(`get_arguments`):
- too many arguments: `function takes at most 2 arguments (3 given)`, or
`... 2 keyword arguments ...` when all are keywords;
- a missing key: `function missing required argument 'key' (pos 1)`;
- an argument given twice: `argument for function given by name ('key')
and position (1)`;
- an unknown keyword: `'x' is an invalid keyword argument for this
function`.
- `pop`, `popitem`, `keys`, `values` and `items` are `collections.abc`'s
`MutableMapping` and `Mapping` methods for upb's maps. They now call those
functions (`abc_method`), so their results and their argument errors are
Python's, in any version: `MutableMapping.pop() missing 1 required positional
argument: 'key'`. `update` already followed `MutableMapping.update` (#854).
**Repeated containers.** `RepeatedScalarContainer` and
`RepeatedCompositeContainer` take positional arguments only, as upb's do.
- `append`, `extend`, `remove` and `MergeFrom` take exactly one argument
(`METH_O`).
- `insert` and `pop` parse theirs as `PyArg_ParseTuple`'s `"nO"` and `"|n"`
do:
- `function takes exactly 2 arguments (1 given)`;
- `function takes at most 1 argument (2 given)`;
- an index of its `__index__`, with `Python int too large to convert to C
ssize_t`, which `ssize_t_of` gives through `PyNumber_Index` and
`PyLong_AsSsize_t`. Before, PyO3's text said "C long".
- `__deepcopy__` takes any positional arguments and no keyword argument.
- Before, these all succeeded where upb raises `TypeError: ... takes no
keyword arguments`: `append(value=...)`, `insert(key=..., value=...)`,
`pop(key=...)`, `remove(elem=...)`, `MergeFrom(other=...)` and
`__deepcopy__(memo=...)`.
- The bodies of `extend` and `MergeFrom` move into `extend_values`, which
both call.
**Docs** (`docs/compatibility.md`).
- The difference list states that the containers' methods take upb's
arguments and errors.
- It also states the one exception, which is inherent. A dunder method of a
map called directly with the wrong arguments (`map.__eq__()`) gets the error of
CPython's slot wrapper of the native type, `expected 1 argument, got 0`. upb's
`__eq__` of a map is `Mapping.__eq__`, a Python function, whose error is
`Mapping.__eq__() missing 1 required positional argument: 'other'`.
- The paragraph that #854 left with the short line "either. A message of
the" is reflowed.
### Why are the changes needed?
The containers took keyword arguments that upb refuses, so code that runs on
the drop-in failed on pyspark-client. Their errors differed from upb's too. One
example: `setdefault("k", default="d")` set the key where upb raises
`TypeError`.
### Does this PR introduce _any_ user-facing change?
Yes, the arguments and errors of these methods, as upb's:
| | pyspark-client 4.3.0, and now | before |
|---|---|---|
| `options.MergeFrom()` | `TypeError: ScalarMapContainer.MergeFrom() takes
exactly one argument (0 given)` | `TypeError: MapContainer.MergeFrom() missing
1 required positional argument: 'other'` |
| `options.MergeFrom(other=[("a", "b")])` | `TypeError:
ScalarMapContainer.MergeFrom() takes no keyword arguments` | merged |
| `options.setdefault("k", default="d")` | `TypeError:
ScalarMapContainer.setdefault() takes no keyword arguments` | `'d'` |
| `options.get("k", x=1)` | `TypeError: 'x' is an invalid keyword argument
for this function` | `TypeError: MapContainer.get() got an unexpected keyword
argument 'x'` |
| `options.pop()` | `TypeError: MutableMapping.pop() missing 1 required
positional argument: 'key'` | `TypeError: MapContainer.pop() missing 1 required
positional argument: 'key'` |
| `probabilities.append(value=0.5)` | `TypeError:
RepeatedScalarContainer.append() takes no keyword arguments` | appended |
| `probabilities.insert(0)` | `TypeError: function takes exactly 2 arguments
(1 given)` | `TypeError: RepeatedScalarContainer.insert() missing 1 required
positional argument: 'value'` |
| `elements.pop(2**70)` | `OverflowError: Python int too large to convert to
C ssize_t` | `OverflowError: Python int too large to convert to C long` |
### How was this patch tested?
- **A new test in `python/tests/test_proto_rust_offline.py`**,
`test_methods_of_containers_raise_upbs_errors_of_their_arguments`. It fails on
master and passes with this change. It compares against upb's classes:
- 42 calls of the methods of maps, on a map of scalars and on one of
messages;
- 33 calls of the methods of repeated fields, on a repeated scalar and on
a repeated message field.
Each call compares its outcome and the bytes of the message after it.
- **Probes of both clients** (130 cases of maps, 94 of repeated containers).
All give upb's results except the documented differences: the names of the
types in an `AttributeError`, and a dunder method of a map called directly.
- **Rust gates:**
- `cargo fmt --all --check`;
- `cargo +1.100.0 clippy -p apache-spark-connect-core -p
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect
-p apache-spark-connect-core`;
- `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it: 15
passed.
- **Python gates:**
- ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and
`scripts/audit_no_stubs.sh`;
- `gen_type_stubs.py --check` and `gen_fn_dispatch.py --check`, and the
guards of the test lists;
- `test_type_stubs_offline.py` with mypy 2.4.1: 13 passed and 3 skipped.
- **The offline suite of `coverage.yml`:** 6495 passed and 77 skipped, the
skips of master.
- **`test_dropin_e2e.py` against a Spark 4.3.0 server:** 333 passed.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]