Skip to content

Commit 0abfcf5

Browse files
nikolauspschuetzuros-b
authored andcommitted
[MINOR][PYTHON] Fix wrong expected_type in DataFrame.sort ascending type error
### What changes were proposed in this pull request? `DataFrame.sort` / `orderBy` / `sortWithinPartitions` raise `NOT_EXPECTED_TYPE` for an invalid `ascending` argument with `expected_type` = `"Column, int or str"`. But `ascending` accepts `bool`, `int`, or `list` — never `Column` or `str`. The literal was copied from the column-argument error a few lines above in `_preapare_cols_for_sort`. This corrects it to `"bool, int or list"`. ### Why are the changes needed? The message names types `ascending` never accepts (`Column`, `str`) and omits the ones it does (`bool`, `list`), misleading users about how to fix the call. The `ascending` parameter is documented as `bool or list`. ### Does this PR introduce _any_ user-facing change? Yes, the error message changes. Before: ``` [NOT_EXPECTED_TYPE] Argument `ascending` should be Column, int or str, got str. ``` After: ``` [NOT_EXPECTED_TYPE] Argument `ascending` should be bool, int or list, got str. ``` ### How was this patch tested? Added `test_sort_ascending_invalid_type` in `python/pyspark/sql/tests/test_dataframe.py` asserting the corrected `expected_type`. Verified against a released `pyspark` session: `df.sort("age", ascending="asc")` reports `bool, int or list` with the fix (previously `Column, int or str`); valid `ascending` values (`False`, `[True]`) are unaffected. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 4.8) Closes #57522 from nikolauspschuetz/minor-sort-ascending-expected-type. Authored-by: Nikolaus Schuetz <[email protected]> Signed-off-by: Uros Bojanic <[email protected]> (cherry picked from commit 1222a59) Signed-off-by: Uros Bojanic <[email protected]>
1 parent 5163a03 commit 0abfcf5

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎python/pyspark/sql/dataframe.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3351,7 +3351,7 @@ def _get_col(
33513351
raise PySparkTypeError(
33523352
errorClass="NOT_EXPECTED_TYPE",
33533353
messageParameters={
3354-
"expected_type": "Column, int or str",
3354+
"expected_type": "bool, int or list",
33553355
"arg_name": "ascending",
33563356
"arg_type": type(ascending).__name__,
33573357
},

‎python/pyspark/sql/tests/test_dataframe.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ def test_with_columns_renamed(self):
261261
messageParameters={"expected_type": "dict", "arg_name": "colsMap", "arg_type": "tuple"},
262262
)
263263

264+
def test_sort_ascending_invalid_type(self):
265+
df = self.spark.createDataFrame([("Alice", 10)], ["name", "age"])
266+
with self.assertRaises(PySparkTypeError) as pe:
267+
df.sort("age", ascending="asc")
268+
self.check_error(
269+
exception=pe.exception,
270+
errorClass="NOT_EXPECTED_TYPE",
271+
messageParameters={
272+
"expected_type": "bool, int or list",
273+
"arg_name": "ascending",
274+
"arg_type": "str",
275+
},
276+
)
277+
264278
def test_with_columns_renamed_with_duplicated_names(self):
265279
df1 = self.spark.createDataFrame([(1, "v1")], ["id", "value"])
266280
df2 = self.spark.createDataFrame([(1, "x", "v2")], ["id", "a", "value"])

0 commit comments

Comments
 (0)