HyukjinKwon commented on a change in pull request #32555:
URL: https://github.com/apache/spark/pull/32555#discussion_r633023697
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError(f'Parameter `n` (number of rows) must be an int')
Review comment:
Can we just a plain string instead of f-string
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError(f'Parameter `n` (number of rows) must be an int')
+
+ if not isinstance(vertical, bool):
+ raise TypeError(f'Parameter `vertical` must be a bool')
Review comment:
ditto
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
Review comment:
While we're here, would you mind fix the docs above `truncate : bool,
optional` -> `truncate : bool or int, optional`.
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError(f'Parameter `n` (number of rows) must be an int')
+
+ if not isinstance(vertical, bool):
+ raise TypeError(f'Parameter `vertical` must be a bool')
+
if isinstance(truncate, bool) and truncate:
print(self._jdf.showString(n, 20, vertical))
else:
- print(self._jdf.showString(n, int(truncate), vertical))
+ try:
+ int_truncate = int(truncate)
+ print(self._jdf.showString(n, int_truncate, vertical))
Review comment:
I think you should pull this call out, and don't combine with parameter
validation logic together.
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError(f'Parameter `n` (number of rows) must be an int')
Review comment:
I would just say it as `'n'` instead of `` `n` ``
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,22 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
Review comment:
why do we need to check if `n` is `bool`? bool inherits `int` anyway so
it's no-op to remove
##########
File path: python/pyspark/sql/tests/test_dataframe.py
##########
@@ -837,6 +837,21 @@ def test_input_files(self):
finally:
shutil.rmtree(tpath)
+ def test_df_show(self):
+ df = self.spark.createDataFrame([('foo',)])
Review comment:
I would add a comment with a JIRA, see also
https://spark.apache.org/contributing.html
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,23 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError("Parameter 'n' (number of rows) must be an int")
+
+ if not isinstance(vertical, bool):
+ raise TypeError("Parameter 'vertical' must be a bool")
+
if isinstance(truncate, bool) and truncate:
print(self._jdf.showString(n, 20, vertical))
else:
- print(self._jdf.showString(n, int(truncate), vertical))
+ try:
+ int_truncate = int(truncate)
+ except ValueError:
+ raise ValueError(f"Non-bool parameter 'truncate={truncate}'"
+ f"could not be converted to an int")
Review comment:
```suggestion
"could not be converted to an int")
```
##########
File path: python/pyspark/sql/dataframe.py
##########
@@ -482,10 +482,23 @@ def show(self, n=20, truncate=True, vertical=False):
age | 5
name | Bob
"""
+
+ if not isinstance(n, int) or isinstance(n, bool):
+ raise TypeError("Parameter 'n' (number of rows) must be an int")
+
+ if not isinstance(vertical, bool):
+ raise TypeError("Parameter 'vertical' must be a bool")
+
if isinstance(truncate, bool) and truncate:
print(self._jdf.showString(n, 20, vertical))
else:
- print(self._jdf.showString(n, int(truncate), vertical))
+ try:
+ int_truncate = int(truncate)
+ except ValueError:
+ raise ValueError(f"Non-bool parameter 'truncate={truncate}'"
Review comment:
Can we throw a `TypeError` and fix the error message like: "Parameter
'truncate={truncate}' should be either bool or int."?
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]