This is an automated email from the ASF dual-hosted git repository.
HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new 1a3d2e09 [SPARK-61609][FOLLOWUP][CONNECT][PYTHON] Raise PySpark's
TypeError for a keyword key that is not a str in the readers, writers and
streams of fixed parameters, and in Builder.config
1a3d2e09 is described below
commit 1a3d2e0939a999e77619ab61fa96c494d3bb9f25
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 15:58:13 2026 +0900
[SPARK-61609][FOLLOWUP][CONNECT][PYTHON] Raise PySpark's TypeError for a
keyword key that is not a str in the readers, writers and streams of fixed
parameters, and in Builder.config
### What changes were proposed in this pull request?
A follow-up to #604 for the methods whose PyO3 signatures have no
`**kwargs`. With a keyword key that is not a str, as in `spark.read.json(p,
**{1: "1"})`, these now raise PySpark's `TypeError: keywords must be strings`,
before anything is sent. The methods:
- `DataFrameReader.json`, `csv`, `orc`, `text` and `xml`;
- `DataStreamReader.json`, `parquet`, `csv`, `text`, `orc` and `xml`;
- `DataFrameWriter.parquet`, `json`, `csv`, `orc`, `text` and `xml`;
- `SparkSession.builder.config`.
Each takes a catch-all `**unexpected`, which its pinned `text_signature`
does not show, and checks it first with a new helper next to
`check_keyword_keys` in `options.rs`. An unexpected str key, as `foo=1`, raises
the same `got an unexpected keyword argument 'foo'` as before.
### Why are the changes needed?
PyO3 rejected the key itself, with `<Class>.<method>() got an unexpected
keyword argument '1'`, before #604's check could run. PySpark's methods are
Python functions, for which CPython raises `keywords must be strings`. A
post-merge review of #604 found the difference.
### Does this PR introduce _any_ user-facing change?
Yes. The `TypeError` of a non-str keyword key now matches PySpark 4.3.0.
The visible signatures do not change.
### How was this patch tested?
- A new offline test covers all 18 methods with `{1: "1"}` and `{"foo":
1}`. It asserts both exact messages, and that no request is sent, through a
session without a server. Another test checks that known keywords still bind.
- `test_dropin_offline.py`: 1992 passed, 8 skipped.
- The reader, writer and session subset of `test_dropin_e2e.py` against a
Spark 4.3.0 Connect server: 38 passed.
- `__text_signature__` and `inspect.signature` are identical before and
after, for all 18 methods.
- Against the official `pyspark-client` 4.3.0, every method raises the same
two messages, except one. `builder.config` with an unknown str key says
`Builder.config()` where PySpark says `SparkSession.Builder.config()`, and that
wording predates this change.
### 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.
Closes #622 from HyukjinKwon/SPARK-61609-followup-keyword-keys.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
crates/pyspark-rs/src/dataframe.rs | 24 ++++++++++++-----
crates/pyspark-rs/src/options.rs | 18 +++++++++++++
crates/pyspark-rs/src/readwriter.rs | 20 ++++++++++----
crates/pyspark-rs/src/session.rs | 4 ++-
crates/pyspark-rs/src/streaming.rs | 26 +++++++++++++-----
python/tests/test_dropin_offline.py | 54 +++++++++++++++++++++++++++++++++++++
6 files changed, 127 insertions(+), 19 deletions(-)
diff --git a/crates/pyspark-rs/src/dataframe.rs
b/crates/pyspark-rs/src/dataframe.rs
index acde1133..faf49350 100644
--- a/crates/pyspark-rs/src/dataframe.rs
+++ b/crates/pyspark-rs/src/dataframe.rs
@@ -3983,7 +3983,7 @@ impl PyDataFrameWriter {
/// Write as PARQUET - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, mode=None, partitionBy=None, compression=None))]
+ #[pyo3(signature = (path, mode=None, partitionBy=None, compression=None,
**unexpected), text_signature = "($self, path, mode=None, partitionBy=None,
compression=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn parquet(
&self,
@@ -3992,7 +3992,9 @@ impl PyDataFrameWriter {
mode: Option<&Bound<'_, PyAny>>,
partitionBy: Option<&Bound<'_, PyAny>>,
compression: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.parquet",
unexpected)?;
let mut w = self.inner.get();
if let Some(m) = mode {
w = w.mode(&m.str()?.to_string());
@@ -4017,7 +4019,7 @@ impl PyDataFrameWriter {
/// Write as JSON - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, mode=None, compression=None, dateFormat=None,
timestampFormat=None, lineSep=None, encoding=None, ignoreNullFields=None))]
+ #[pyo3(signature = (path, mode=None, compression=None, dateFormat=None,
timestampFormat=None, lineSep=None, encoding=None, ignoreNullFields=None,
**unexpected), text_signature = "($self, path, mode=None, compression=None,
dateFormat=None, timestampFormat=None, lineSep=None, encoding=None,
ignoreNullFields=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn json(
&self,
@@ -4030,7 +4032,9 @@ impl PyDataFrameWriter {
lineSep: Option<&Bound<'_, PyAny>>,
encoding: Option<&Bound<'_, PyAny>>,
ignoreNullFields: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.json",
unexpected)?;
let mut w = self.inner.get();
if let Some(m) = mode {
w = w.mode(&m.str()?.to_string());
@@ -4055,7 +4059,7 @@ impl PyDataFrameWriter {
/// Write as CSV - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, mode=None, compression=None, sep=None,
quote=None, escape=None, header=None, nullValue=None, escapeQuotes=None,
quoteAll=None, dateFormat=None, timestampFormat=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None,
charToEscapeQuoteEscaping=None, encoding=None, emptyValue=None, lineSep=None))]
+ #[pyo3(signature = (path, mode=None, compression=None, sep=None,
quote=None, escape=None, header=None, nullValue=None, escapeQuotes=None,
quoteAll=None, dateFormat=None, timestampFormat=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None,
charToEscapeQuoteEscaping=None, encoding=None, emptyValue=None, lineSep=None,
**unexpected), text_signature = "($self, path, mode=None, compression=None,
sep=None, quote=None, escape=None, header=None, nullValue=None,
escapeQuotes=None [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn csv(
&self,
@@ -4078,7 +4082,9 @@ impl PyDataFrameWriter {
encoding: Option<&Bound<'_, PyAny>>,
emptyValue: Option<&Bound<'_, PyAny>>,
lineSep: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.csv",
unexpected)?;
let mut w = self.inner.get();
if let Some(m) = mode {
w = w.mode(&m.str()?.to_string());
@@ -4113,7 +4119,7 @@ impl PyDataFrameWriter {
/// Write as ORC - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, mode=None, partitionBy=None, compression=None))]
+ #[pyo3(signature = (path, mode=None, partitionBy=None, compression=None,
**unexpected), text_signature = "($self, path, mode=None, partitionBy=None,
compression=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn orc(
&self,
@@ -4122,7 +4128,9 @@ impl PyDataFrameWriter {
mode: Option<&Bound<'_, PyAny>>,
partitionBy: Option<&Bound<'_, PyAny>>,
compression: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.orc",
unexpected)?;
let mut w = self.inner.get();
if let Some(m) = mode {
w = w.mode(&m.str()?.to_string());
@@ -4145,7 +4153,7 @@ impl PyDataFrameWriter {
/// Write as TEXT - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, compression=None, lineSep=None))]
+ #[pyo3(signature = (path, compression=None, lineSep=None, **unexpected),
text_signature = "($self, path, compression=None, lineSep=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn text(
&self,
@@ -4153,7 +4161,9 @@ impl PyDataFrameWriter {
path: &Bound<'_, PyAny>,
compression: Option<&Bound<'_, PyAny>>,
lineSep: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.text",
unexpected)?;
let mut w = self.inner.get();
w = set_options_skip_none(w, &[("compression", compression),
("lineSep", lineSep)])?;
// PySpark sets the mode/options/format on the writer itself, then
saves.
@@ -4165,7 +4175,7 @@ impl PyDataFrameWriter {
/// Write as XML - full pyspark signature; each named option is
/// applied when provided.
- #[pyo3(signature = (path, rowTag=None, mode=None, attributePrefix=None,
valueTag=None, rootTag=None, declaration=None, arrayElementName=None,
nullValue=None, dateFormat=None, timestampFormat=None, compression=None,
encoding=None, validateName=None))]
+ #[pyo3(signature = (path, rowTag=None, mode=None, attributePrefix=None,
valueTag=None, rootTag=None, declaration=None, arrayElementName=None,
nullValue=None, dateFormat=None, timestampFormat=None, compression=None,
encoding=None, validateName=None, **unexpected), text_signature = "($self,
path, rowTag=None, mode=None, attributePrefix=None, valueTag=None,
rootTag=None, declaration=None, arrayElementName=None, nullValue=None,
dateFormat=None, timestampFormat=None, compression=None, enc [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn xml(
&self,
@@ -4184,7 +4194,9 @@ impl PyDataFrameWriter {
compression: Option<&Bound<'_, PyAny>>,
encoding: Option<&Bound<'_, PyAny>>,
validateName: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
+ crate::options::check_unexpected_keywords("DataFrameWriter.xml",
unexpected)?;
let mut w = self.inner.get();
w = set_option_skip_none(w, "rowTag", rowTag)?;
if let Some(m) = mode {
diff --git a/crates/pyspark-rs/src/options.rs b/crates/pyspark-rs/src/options.rs
index 319648c7..6824c766 100644
--- a/crates/pyspark-rs/src/options.rs
+++ b/crates/pyspark-rs/src/options.rs
@@ -78,6 +78,24 @@ pub(crate) fn check_keyword_keys(options: Option<&Bound<'_,
PyDict>>) -> PyResul
Ok(())
}
+/// The keywords of a method that has a catch-all `**unexpected` for the
keywords it does not take,
+/// which keeps them out of its signature: the keys are checked as
[`check_keyword_keys`] does, then
+/// the first key raises the `TypeError` of an unexpected keyword argument,
worded as PyO3 words it
+/// for a method without the catch-all, with `method` as its `Class.method`
name.
+pub(crate) fn check_unexpected_keywords(
+ method: &str,
+ unexpected: Option<&Bound<'_, PyDict>>,
+) -> PyResult<()> {
+ check_keyword_keys(unexpected)?;
+ if let Some(key) = unexpected.and_then(|dict| dict.keys().iter().next()) {
+ let key: String = key.extract()?;
+ return Err(pyo3::exceptions::PyTypeError::new_err(format!(
+ "{method}() got an unexpected keyword argument '{key}'"
+ )));
+ }
+ Ok(())
+}
+
/// The key of an option that PySpark keeps in a dict, which raises the
`TypeError` of an
/// unhashable key, and sends in a protobuf string field as it is, as
[`str_argument`] converts it:
/// the options of the writers, and of `SparkSession.builder.config`.
diff --git a/crates/pyspark-rs/src/readwriter.rs
b/crates/pyspark-rs/src/readwriter.rs
index 2ad077d8..d8f0bca0 100644
--- a/crates/pyspark-rs/src/readwriter.rs
+++ b/crates/pyspark-rs/src/readwriter.rs
@@ -431,7 +431,7 @@ impl PyDataFrameReader {
/// Read JSON file(s) - full pyspark signature; each named option is
/// applied as a read option when provided.
- #[pyo3(signature = (path, schema=None, primitivesAsString=None,
prefersDecimal=None, allowComments=None, allowUnquotedFieldNames=None,
allowSingleQuotes=None, allowNumericLeadingZero=None,
allowBackslashEscapingAnyCharacter=None, mode=None,
columnNameOfCorruptRecord=None, dateFormat=None, timestampFormat=None,
multiLine=None, allowUnquotedControlChars=None, lineSep=None,
samplingRatio=None, dropFieldIfAllNull=None, encoding=None, locale=None,
pathGlobFilter=None, recursiveFileLookup= [...]
+ #[pyo3(signature = (path, schema=None, primitivesAsString=None,
prefersDecimal=None, allowComments=None, allowUnquotedFieldNames=None,
allowSingleQuotes=None, allowNumericLeadingZero=None,
allowBackslashEscapingAnyCharacter=None, mode=None,
columnNameOfCorruptRecord=None, dateFormat=None, timestampFormat=None,
multiLine=None, allowUnquotedControlChars=None, lineSep=None,
samplingRatio=None, dropFieldIfAllNull=None, encoding=None, locale=None,
pathGlobFilter=None, recursiveFileLookup= [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn json(
&mut self,
@@ -461,7 +461,9 @@ impl PyDataFrameReader {
modifiedAfter: Option<&Bound<'_, PyAny>>,
allowNonNumericNumbers: Option<&Bound<'_, PyAny>>,
useUnsafeRow: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataFrameReader.json",
unexpected)?;
let mut r = self.inner.clone();
r = set_options_skip_none(
r,
@@ -535,7 +537,7 @@ impl PyDataFrameReader {
/// Read CSV file(s) - full pyspark signature; each named option is
/// applied as a read option when provided.
- #[pyo3(signature = (path, schema=None, sep=None, encoding=None,
quote=None, escape=None, comment=None, header=None, inferSchema=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, nullValue=None,
nanValue=None, positiveInf=None, negativeInf=None, dateFormat=None,
timestampFormat=None, maxColumns=None, maxCharsPerColumn=None,
maxMalformedLogPerPartition=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, charToEscapeQuoteEscaping=None, samplingRatio=None, e [...]
+ #[pyo3(signature = (path, schema=None, sep=None, encoding=None,
quote=None, escape=None, comment=None, header=None, inferSchema=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, nullValue=None,
nanValue=None, positiveInf=None, negativeInf=None, dateFormat=None,
timestampFormat=None, maxColumns=None, maxCharsPerColumn=None,
maxMalformedLogPerPartition=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, charToEscapeQuoteEscaping=None, samplingRatio=None, e [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn csv(
&mut self,
@@ -573,7 +575,9 @@ impl PyDataFrameReader {
modifiedBefore: Option<&Bound<'_, PyAny>>,
modifiedAfter: Option<&Bound<'_, PyAny>>,
unescapedQuoteHandling: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataFrameReader.csv",
unexpected)?;
let mut r = self.inner.clone();
r = set_options_skip_none(
r,
@@ -623,7 +627,7 @@ impl PyDataFrameReader {
/// Read ORC file(s) - full pyspark signature; each named option is
/// applied as a read option when provided.
- #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None, modifiedBefore=None, modifiedAfter=None))]
+ #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None, modifiedBefore=None, modifiedAfter=None,
**unexpected), text_signature = "($self, path, mergeSchema=None,
pathGlobFilter=None, recursiveFileLookup=None, modifiedBefore=None,
modifiedAfter=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn orc(
&mut self,
@@ -633,7 +637,9 @@ impl PyDataFrameReader {
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
modifiedBefore: Option<&Bound<'_, PyAny>>,
modifiedAfter: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataFrameReader.orc",
unexpected)?;
let mut r = self.inner.clone();
r = set_options_skip_none(
r,
@@ -654,7 +660,7 @@ impl PyDataFrameReader {
/// applied as a read option when provided. As PySpark's
`wholetext=False`, an omitted
/// `wholetext` sets `false`, also over an earlier `wholetext` option.
#[pyo3(
- signature = (paths, wholetext=DefaultedOption::Omitted, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None, modifiedBefore=None,
modifiedAfter=None),
+ signature = (paths, wholetext=DefaultedOption::Omitted, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None, modifiedBefore=None,
modifiedAfter=None, **unexpected),
text_signature = "($self, paths, wholetext=False, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None, modifiedBefore=None,
modifiedAfter=None)"
)]
#[allow(non_snake_case, clippy::too_many_arguments)]
@@ -668,7 +674,9 @@ impl PyDataFrameReader {
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
modifiedBefore: Option<&Bound<'_, PyAny>>,
modifiedAfter: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataFrameReader.text",
unexpected)?;
let wholetext = wholetext.or_bool(py, false);
let mut r = self.inner.clone();
r = set_options_skip_none(
@@ -689,7 +697,7 @@ impl PyDataFrameReader {
/// Read XML file(s) - full pyspark signature; each named option is
/// applied as a read option when provided.
- #[pyo3(signature = (path, rowTag=None, schema=None, excludeAttribute=None,
attributePrefix=None, valueTag=None, ignoreSurroundingSpaces=None,
rowValidationXSDPath=None, ignoreNamespace=None, wildcardColName=None,
encoding=None, inferSchema=None, nullValue=None, dateFormat=None,
timestampFormat=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, samplingRatio=None, locale=None))]
+ #[pyo3(signature = (path, rowTag=None, schema=None, excludeAttribute=None,
attributePrefix=None, valueTag=None, ignoreSurroundingSpaces=None,
rowValidationXSDPath=None, ignoreNamespace=None, wildcardColName=None,
encoding=None, inferSchema=None, nullValue=None, dateFormat=None,
timestampFormat=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, samplingRatio=None, locale=None, **unexpected), text_signature
= "($self, path, rowTag=None, schema=None, excludeAttribute=None, [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn xml(
&mut self,
@@ -713,7 +721,9 @@ impl PyDataFrameReader {
multiLine: Option<&Bound<'_, PyAny>>,
samplingRatio: Option<&Bound<'_, PyAny>>,
locale: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataFrameReader.xml",
unexpected)?;
let mut r = self.inner.clone();
r = set_option_skip_none(r, "rowTag", rowTag)?;
if let Some(v) = schema {
diff --git a/crates/pyspark-rs/src/session.rs b/crates/pyspark-rs/src/session.rs
index d0e62eeb..cfae0960 100644
--- a/crates/pyspark-rs/src/session.rs
+++ b/crates/pyspark-rs/src/session.rs
@@ -98,14 +98,16 @@ impl PySparkSessionBuilder {
/// `SparkSession.Builder.config(key=None, value=None, *, map=None,
conf=None)`.
/// `spark.remote` sets the connect endpoint; other keys are applied as
runtime confs
/// on connect. `conf` accepts a `SparkConf` (its `getAll()` pairs are
applied).
- #[pyo3(signature = (key=None, value=None, map=None, conf=None))]
+ #[pyo3(signature = (key=None, value=None, map=None, conf=None,
**unexpected), text_signature = "($self, key=None, value=None, map=None,
conf=None)")]
fn config<'py>(
mut slf: PyRefMut<'py, Self>,
key: Option<&Bound<'_, PyAny>>,
value: Option<&Bound<'_, PyAny>>,
map: Option<&Bound<'_, PyDict>>,
conf: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyRefMut<'py, Self>> {
+ crate::options::check_unexpected_keywords("Builder.config",
unexpected)?;
let py = slf.py();
// PySpark keeps the options in a dict, and sends each in a `KeyValue`
of the key as it
// is, whose constructor leaves a key of None unset, the empty key.
diff --git a/crates/pyspark-rs/src/streaming.rs
b/crates/pyspark-rs/src/streaming.rs
index 5a593031..455d7b27 100644
--- a/crates/pyspark-rs/src/streaming.rs
+++ b/crates/pyspark-rs/src/streaming.rs
@@ -438,7 +438,7 @@ impl PyDataStreamReader {
/// Load an XML streaming source. Mirrors `DataStreamReader.xml(...)`: set
each named
/// option, then `format("xml").load(path)`.
- #[pyo3(signature = (path, rowTag=None, schema=None, excludeAttribute=None,
attributePrefix=None, valueTag=None, ignoreSurroundingSpaces=None,
rowValidationXSDPath=None, ignoreNamespace=None, wildcardColName=None,
encoding=None, inferSchema=None, nullValue=None, dateFormat=None,
timestampFormat=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, samplingRatio=None, locale=None))]
+ #[pyo3(signature = (path, rowTag=None, schema=None, excludeAttribute=None,
attributePrefix=None, valueTag=None, ignoreSurroundingSpaces=None,
rowValidationXSDPath=None, ignoreNamespace=None, wildcardColName=None,
encoding=None, inferSchema=None, nullValue=None, dateFormat=None,
timestampFormat=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, samplingRatio=None, locale=None, **unexpected), text_signature
= "($self, path, rowTag=None, schema=None, excludeAttribute=None, [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn xml(
&mut self,
@@ -462,7 +462,9 @@ impl PyDataStreamReader {
multiLine: Option<&Bound<'_, PyAny>>,
samplingRatio: Option<&Bound<'_, PyAny>>,
locale: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.xml",
unexpected)?;
let mut r = self.inner.clone();
r = set_stream_option_skip_none(r, "rowTag", rowTag)?;
if let Some(v) = schema {
@@ -502,7 +504,7 @@ impl PyDataStreamReader {
Ok(PyDataFrame::new(df))
}
- #[pyo3(signature = (path, schema=None, primitivesAsString=None,
prefersDecimal=None, allowComments=None, allowUnquotedFieldNames=None,
allowSingleQuotes=None, allowNumericLeadingZero=None,
allowBackslashEscapingAnyCharacter=None, mode=None,
columnNameOfCorruptRecord=None, dateFormat=None, timestampFormat=None,
multiLine=None, allowUnquotedControlChars=None, lineSep=None, locale=None,
dropFieldIfAllNull=None, encoding=None, pathGlobFilter=None,
recursiveFileLookup=None, allowNonNumeri [...]
+ #[pyo3(signature = (path, schema=None, primitivesAsString=None,
prefersDecimal=None, allowComments=None, allowUnquotedFieldNames=None,
allowSingleQuotes=None, allowNumericLeadingZero=None,
allowBackslashEscapingAnyCharacter=None, mode=None,
columnNameOfCorruptRecord=None, dateFormat=None, timestampFormat=None,
multiLine=None, allowUnquotedControlChars=None, lineSep=None, locale=None,
dropFieldIfAllNull=None, encoding=None, pathGlobFilter=None,
recursiveFileLookup=None, allowNonNumeri [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn json(
&mut self,
@@ -529,7 +531,9 @@ impl PyDataStreamReader {
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
allowNonNumericNumbers: Option<&Bound<'_, PyAny>>,
useUnsafeRow: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.json",
unexpected)?;
let mut r = self.inner.clone();
if let Some(v) = schema {
r = apply_stream_schema(r, v)?;
@@ -568,7 +572,7 @@ impl PyDataStreamReader {
Ok(PyDataFrame::new(r.json(&path)))
}
- #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None, datetimeRebaseMode=None, int96RebaseMode=None))]
+ #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None, datetimeRebaseMode=None, int96RebaseMode=None,
**unexpected), text_signature = "($self, path, mergeSchema=None,
pathGlobFilter=None, recursiveFileLookup=None, datetimeRebaseMode=None,
int96RebaseMode=None)")]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn parquet(
&mut self,
@@ -578,7 +582,9 @@ impl PyDataStreamReader {
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
datetimeRebaseMode: Option<&Bound<'_, PyAny>>,
int96RebaseMode: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.parquet",
unexpected)?;
let mut r = self.inner.clone();
r = set_stream_options_skip_none(
r,
@@ -595,7 +601,7 @@ impl PyDataStreamReader {
Ok(PyDataFrame::new(r.parquet(&path)))
}
- #[pyo3(signature = (path, schema=None, sep=None, encoding=None,
quote=None, escape=None, comment=None, header=None, inferSchema=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, nullValue=None,
nanValue=None, positiveInf=None, negativeInf=None, dateFormat=None,
timestampFormat=None, maxColumns=None, maxCharsPerColumn=None,
maxMalformedLogPerPartition=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, charToEscapeQuoteEscaping=None, enforceSchema=None, e [...]
+ #[pyo3(signature = (path, schema=None, sep=None, encoding=None,
quote=None, escape=None, comment=None, header=None, inferSchema=None,
ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, nullValue=None,
nanValue=None, positiveInf=None, negativeInf=None, dateFormat=None,
timestampFormat=None, maxColumns=None, maxCharsPerColumn=None,
maxMalformedLogPerPartition=None, mode=None, columnNameOfCorruptRecord=None,
multiLine=None, charToEscapeQuoteEscaping=None, enforceSchema=None, e [...]
#[allow(non_snake_case, clippy::too_many_arguments)]
fn csv(
&mut self,
@@ -630,7 +636,9 @@ impl PyDataStreamReader {
pathGlobFilter: Option<&Bound<'_, PyAny>>,
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
unescapedQuoteHandling: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.csv",
unexpected)?;
let mut r = self.inner.clone();
if let Some(v) = schema {
r = apply_stream_schema(r, v)?;
@@ -674,7 +682,7 @@ impl PyDataStreamReader {
Ok(PyDataFrame::new(r.csv(&path)))
}
- #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None))]
+ #[pyo3(signature = (path, mergeSchema=None, pathGlobFilter=None,
recursiveFileLookup=None, **unexpected), text_signature = "($self, path,
mergeSchema=None, pathGlobFilter=None, recursiveFileLookup=None)")]
#[allow(non_snake_case)]
fn orc(
&mut self,
@@ -682,7 +690,9 @@ impl PyDataStreamReader {
mergeSchema: Option<&Bound<'_, PyAny>>,
pathGlobFilter: Option<&Bound<'_, PyAny>>,
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.orc",
unexpected)?;
let mut r = self.inner.clone();
r = set_stream_options_skip_none(
r,
@@ -700,10 +710,10 @@ impl PyDataStreamReader {
/// As PySpark's `wholetext=False`, an omitted `wholetext` sets `False`,
also over an earlier
/// `wholetext` option.
#[pyo3(
- signature = (path, wholetext=DefaultedOption::Omitted, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None),
+ signature = (path, wholetext=DefaultedOption::Omitted, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None, **unexpected),
text_signature = "($self, path, wholetext=False, lineSep=None,
pathGlobFilter=None, recursiveFileLookup=None)"
)]
- #[allow(non_snake_case)]
+ #[allow(non_snake_case, clippy::too_many_arguments)]
fn text(
&mut self,
py: Python<'_>,
@@ -712,7 +722,9 @@ impl PyDataStreamReader {
lineSep: Option<&Bound<'_, PyAny>>,
pathGlobFilter: Option<&Bound<'_, PyAny>>,
recursiveFileLookup: Option<&Bound<'_, PyAny>>,
+ unexpected: Option<&Bound<'_, PyDict>>,
) -> PyResult<PyDataFrame> {
+ crate::options::check_unexpected_keywords("DataStreamReader.text",
unexpected)?;
let wholetext = wholetext.or_bool(py, false);
let mut r = self.inner.clone();
r = set_stream_options_skip_none(
diff --git a/python/tests/test_dropin_offline.py
b/python/tests/test_dropin_offline.py
index 2dfc2f5b..3d8e901b 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -24163,6 +24163,60 @@ def
test_show_counts_are_proto_int32_as_pyspark(mock_connect):
assert (len(service.execute_requests), len(service.analyze_requests)) ==
sent
+# Each method takes a catch-all of the keywords it does not name, which its
visible signature does
+# not show. The keywords are passed as a dict argument, so that the lambda is
not refused itself.
+UNEXPECTED_KEYWORD_CALLS = [
+ ("DataFrameReader.json", lambda s, kw: s.read.json("p", **kw)),
+ ("DataFrameReader.csv", lambda s, kw: s.read.csv("p", **kw)),
+ ("DataFrameReader.orc", lambda s, kw: s.read.orc("p", **kw)),
+ ("DataFrameReader.text", lambda s, kw: s.read.text("p", **kw)),
+ ("DataFrameReader.xml", lambda s, kw: s.read.xml("p", **kw)),
+ ("DataStreamReader.json", lambda s, kw: s.readStream.json("p", **kw)),
+ ("DataStreamReader.parquet", lambda s, kw: s.readStream.parquet("p",
**kw)),
+ ("DataStreamReader.csv", lambda s, kw: s.readStream.csv("p", **kw)),
+ ("DataStreamReader.text", lambda s, kw: s.readStream.text("p", **kw)),
+ ("DataStreamReader.orc", lambda s, kw: s.readStream.orc("p", **kw)),
+ ("DataStreamReader.xml", lambda s, kw: s.readStream.xml("p", **kw)),
+ ("DataFrameWriter.parquet", lambda s, kw: s.range(1).write.parquet("p",
**kw)),
+ ("DataFrameWriter.json", lambda s, kw: s.range(1).write.json("p", **kw)),
+ ("DataFrameWriter.csv", lambda s, kw: s.range(1).write.csv("p", **kw)),
+ ("DataFrameWriter.orc", lambda s, kw: s.range(1).write.orc("p", **kw)),
+ ("DataFrameWriter.text", lambda s, kw: s.range(1).write.text("p", **kw)),
+ ("DataFrameWriter.xml", lambda s, kw: s.range(1).write.xml("p", **kw)),
+ # PyO3 words the class of the builder as `Builder`, as it did before the
catch-all.
+ ("Builder.config", lambda s, kw: s.builder.config(**kw)),
+]
+
+
[email protected](
+ "method, call", UNEXPECTED_KEYWORD_CALLS, ids=[method for method, _ in
UNEXPECTED_KEYWORD_CALLS]
+)
+def test_unexpected_keywords_are_keywords_as_pyspark(offline_spark, method,
call):
+ # As PySpark's methods, which are Python functions with no **kwargs: a key
that is not a str
+ # raises `keywords must be strings`, and a str key that the method does
not take raises the
+ # unexpected keyword argument of the method. Both are raised before the
call does anything.
+ # The lazy session has no server, so a call that sent a request would fail
with a connection
+ # error instead of these TypeErrors, which is how the lack of a request is
checked here.
+ with pytest.raises(TypeError) as error:
+ call(offline_spark, {1: "1"})
+ assert str(error.value) == "keywords must be strings"
+ with pytest.raises(TypeError) as error:
+ call(offline_spark, {"foo": 1})
+ assert str(error.value) == f"{method}() got an unexpected keyword argument
'foo'"
+
+
+def
test_known_keywords_still_bind_with_the_unexpected_catch_all(offline_spark):
+ # The catch-all takes only the keywords that the method does not name:
those bind as before.
+ from pyspark.sql import DataFrame
+
+ reader = offline_spark.read
+ assert isinstance(reader.json("p", multiLine=True), DataFrame)
+ assert reader._format == "json" and reader._options == {"multiLine":
"true"}
+ reader = offline_spark.read
+ reader.csv("p", sep=";", header=True)
+ assert reader._format == "csv" and reader._options == {"sep": ";",
"header": "true"}
+
+
def test_reader_paths_are_checked_as_pyspark(offline_spark):
from pyspark.errors import PySparkTypeError
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]