Skip to content

Commit e81189f

Browse files
committed
Ruff EM: Error messages assigned to a var first
1 parent 7131c31 commit e81189f

22 files changed

+242
-127
lines changed

duckdb/experimental/spark/_globals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def foo(arg=pyducdkb.spark._NoValue):
3939
# Disallow reloading this module so as to preserve the identities of the
4040
# classes defined here.
4141
if "_is_loaded" in globals():
42-
raise RuntimeError("Reloading duckdb.experimental.spark._globals is not allowed")
42+
msg = "Reloading duckdb.experimental.spark._globals is not allowed"
43+
raise RuntimeError(msg)
4344
_is_loaded = True
4445

4546

duckdb/experimental/spark/errors/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def get_message_template(self, error_class: str) -> str:
8686
if main_error_class in self.error_info_map:
8787
main_error_class_info_map = self.error_info_map[main_error_class]
8888
else:
89-
raise ValueError(f"Cannot find main error class '{main_error_class}'")
89+
msg = f"Cannot find main error class '{main_error_class}'"
90+
raise ValueError(msg)
9091

9192
main_message_template = "\n".join(main_error_class_info_map["message"])
9293

@@ -101,7 +102,8 @@ def get_message_template(self, error_class: str) -> str:
101102
if sub_error_class in main_error_class_subclass_info_map:
102103
sub_error_class_info_map = main_error_class_subclass_info_map[sub_error_class]
103104
else:
104-
raise ValueError(f"Cannot find sub error class '{sub_error_class}'")
105+
msg = f"Cannot find sub error class '{sub_error_class}'"
106+
raise ValueError(msg)
105107

106108
sub_message_template = "\n".join(sub_error_class_info_map["message"])
107109
message_template = main_message_template + " " + sub_message_template

duckdb/experimental/spark/sql/column.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,17 @@ def __getattr__(self, item: Any) -> "Column":
201201
+------+
202202
"""
203203
if item.startswith("__"):
204-
raise AttributeError("Can not access __ (dunder) method")
204+
msg = "Can not access __ (dunder) method"
205+
raise AttributeError(msg)
205206
return self[item]
206207

207208
def alias(self, alias: str):
208209
return Column(self.expr.alias(alias))
209210

210211
def when(self, condition: "Column", value: Any):
211212
if not isinstance(condition, Column):
212-
raise TypeError("condition should be a Column")
213+
msg = "condition should be a Column"
214+
raise TypeError(msg)
213215
v = _get_expr(value)
214216
expr = self.expr.when(condition.expr, v)
215217
return Column(expr)

duckdb/experimental/spark/sql/dataframe.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ def createGlobalTempView(self, name: str) -> None:
108108

109109
def withColumnRenamed(self, columnName: str, newName: str) -> "DataFrame":
110110
if columnName not in self.relation:
111-
raise ValueError(f"DataFrame does not contain a column named {columnName}")
111+
msg = f"DataFrame does not contain a column named {columnName}"
112+
raise ValueError(msg)
112113
cols = []
113114
for x in self.relation.columns:
114115
col = ColumnExpression(x)
@@ -258,7 +259,8 @@ def withColumnsRenamed(self, colsMap: dict[str, str]) -> "DataFrame":
258259

259260
unknown_columns = set(colsMap.keys()) - set(self.relation.columns)
260261
if unknown_columns:
261-
raise ValueError(f"DataFrame does not contain column(s): {', '.join(unknown_columns)}")
262+
msg = f"DataFrame does not contain column(s): {', '.join(unknown_columns)}"
263+
raise ValueError(msg)
262264

263265
# Compute this only once
264266
old_column_names = list(colsMap.keys())
@@ -887,7 +889,8 @@ def __getitem__(self, item: Union[int, str, Column, list, tuple]) -> Union[Colum
887889
elif isinstance(item, int):
888890
return col(self._schema[item].name)
889891
else:
890-
raise TypeError(f"Unexpected item type: {type(item)}")
892+
msg = f"Unexpected item type: {type(item)}"
893+
raise TypeError(msg)
891894

892895
def __getattr__(self, name: str) -> Column:
893896
"""Returns the :class:`Column` denoted by ``name``.

duckdb/experimental/spark/sql/functions.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def ucase(str: "ColumnOrName") -> Column:
9292

9393
def when(condition: "Column", value: Any) -> Column:
9494
if not isinstance(condition, Column):
95-
raise TypeError("condition should be a Column")
95+
msg = "condition should be a Column"
96+
raise TypeError(msg)
9697
v = _get_expr(value)
9798
expr = CaseExpression(condition.expr, v)
9899
return Column(expr)
@@ -1480,7 +1481,8 @@ def approx_count_distinct(col: "ColumnOrName", rsd: Optional[float] = None) -> C
14801481
+---------------+
14811482
"""
14821483
if rsd is not None:
1483-
raise ValueError("rsd is not supported by DuckDB")
1484+
msg = "rsd is not supported by DuckDB"
1485+
raise ValueError(msg)
14841486
return _invoke_function_over_columns("approx_count_distinct", col)
14851487

14861488

@@ -2365,7 +2367,8 @@ def rand(seed: Optional[int] = None) -> Column:
23652367
"""
23662368
if seed is not None:
23672369
# Maybe call setseed just before but how do we know when it is executed?
2368-
raise ContributionsAcceptedError("Seed is not yet implemented")
2370+
msg = "Seed is not yet implemented"
2371+
raise ContributionsAcceptedError(msg)
23692372
return _invoke_function("random")
23702373

23712374

@@ -2842,7 +2845,8 @@ def encode(col: "ColumnOrName", charset: str) -> Column:
28422845
+----------------+
28432846
"""
28442847
if charset != "UTF-8":
2845-
raise ContributionsAcceptedError("Only UTF-8 charset is supported right now")
2848+
msg = "Only UTF-8 charset is supported right now"
2849+
raise ContributionsAcceptedError(msg)
28462850
return _invoke_function("encode", _to_column_expr(col))
28472851

28482852

@@ -3017,7 +3021,8 @@ def greatest(*cols: "ColumnOrName") -> Column:
30173021
[Row(greatest=4)]
30183022
"""
30193023
if len(cols) < 2:
3020-
raise ValueError("greatest should take at least 2 columns")
3024+
msg = "greatest should take at least 2 columns"
3025+
raise ValueError(msg)
30213026

30223027
cols = [_to_column_expr(expr) for expr in cols]
30233028
return _invoke_function("greatest", *cols)
@@ -3049,7 +3054,8 @@ def least(*cols: "ColumnOrName") -> Column:
30493054
[Row(least=1)]
30503055
"""
30513056
if len(cols) < 2:
3052-
raise ValueError("least should take at least 2 columns")
3057+
msg = "least should take at least 2 columns"
3058+
raise ValueError(msg)
30533059

30543060
cols = [_to_column_expr(expr) for expr in cols]
30553061
return _invoke_function("least", *cols)
@@ -3550,12 +3556,14 @@ def sha2(col: "ColumnOrName", numBits: int) -> Column:
35503556
+-----+----------------------------------------------------------------+
35513557
"""
35523558
if numBits not in {224, 256, 384, 512, 0}:
3553-
raise ValueError("numBits should be one of {224, 256, 384, 512, 0}")
3559+
msg = "numBits should be one of {224, 256, 384, 512, 0}"
3560+
raise ValueError(msg)
35543561

35553562
if numBits == 256:
35563563
return _invoke_function_over_columns("sha256", col)
35573564

3558-
raise ContributionsAcceptedError("SHA-224, SHA-384, and SHA-512 are not supported yet.")
3565+
msg = "SHA-224, SHA-384, and SHA-512 are not supported yet."
3566+
raise ContributionsAcceptedError(msg)
35593567

35603568

35613569
def curdate() -> Column:
@@ -5241,7 +5249,8 @@ def array_sort(col: "ColumnOrName", comparator: Optional[Callable[[Column, Colum
52415249
[Row(r=['foobar', 'foo', None, 'bar']), Row(r=['foo']), Row(r=[])]
52425250
"""
52435251
if comparator is not None:
5244-
raise ContributionsAcceptedError("comparator is not yet supported")
5252+
msg = "comparator is not yet supported"
5253+
raise ContributionsAcceptedError(msg)
52455254
else:
52465255
return _invoke_function_over_columns("list_sort", col, lit("ASC"), lit("NULLS LAST"))
52475256

@@ -5335,7 +5344,8 @@ def split(str: "ColumnOrName", pattern: str, limit: int = -1) -> Column:
53355344
if limit > 0:
53365345
# Unclear how to implement this in DuckDB as we'd need to map back from the split array
53375346
# to the original array which is tricky with regular expressions.
5338-
raise ContributionsAcceptedError("limit is not yet supported")
5347+
msg = "limit is not yet supported"
5348+
raise ContributionsAcceptedError(msg)
53395349
return _invoke_function_over_columns("regexp_split_to_array", str, lit(pattern))
53405350

53415351

duckdb/experimental/spark/sql/readwriter.py

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,12 @@ def csv(
248248
def parquet(self, *paths: str, **options: "OptionalPrimitiveType") -> "DataFrame":
249249
input = list(paths)
250250
if len(input) != 1:
251-
raise NotImplementedError("Only single paths are supported for now")
251+
msg = "Only single paths are supported for now"
252+
raise NotImplementedError(msg)
252253
option_amount = len(options.keys())
253254
if option_amount != 0:
254-
raise ContributionsAcceptedError("Options are not supported")
255+
msg = "Options are not supported"
256+
raise ContributionsAcceptedError(msg)
255257
path = input[0]
256258
rel = self.session.conn.read_parquet(path)
257259
from ..sql.dataframe import DataFrame
@@ -338,53 +340,77 @@ def json(
338340
+---+------------+
339341
"""
340342
if schema is not None:
341-
raise ContributionsAcceptedError("The 'schema' option is not supported")
343+
msg = "The 'schema' option is not supported"
344+
raise ContributionsAcceptedError(msg)
342345
if primitivesAsString is not None:
343-
raise ContributionsAcceptedError("The 'primitivesAsString' option is not supported")
346+
msg = "The 'primitivesAsString' option is not supported"
347+
raise ContributionsAcceptedError(msg)
344348
if prefersDecimal is not None:
345-
raise ContributionsAcceptedError("The 'prefersDecimal' option is not supported")
349+
msg = "The 'prefersDecimal' option is not supported"
350+
raise ContributionsAcceptedError(msg)
346351
if allowComments is not None:
347-
raise ContributionsAcceptedError("The 'allowComments' option is not supported")
352+
msg = "The 'allowComments' option is not supported"
353+
raise ContributionsAcceptedError(msg)
348354
if allowUnquotedFieldNames is not None:
349-
raise ContributionsAcceptedError("The 'allowUnquotedFieldNames' option is not supported")
355+
msg = "The 'allowUnquotedFieldNames' option is not supported"
356+
raise ContributionsAcceptedError(msg)
350357
if allowSingleQuotes is not None:
351-
raise ContributionsAcceptedError("The 'allowSingleQuotes' option is not supported")
358+
msg = "The 'allowSingleQuotes' option is not supported"
359+
raise ContributionsAcceptedError(msg)
352360
if allowNumericLeadingZero is not None:
353-
raise ContributionsAcceptedError("The 'allowNumericLeadingZero' option is not supported")
361+
msg = "The 'allowNumericLeadingZero' option is not supported"
362+
raise ContributionsAcceptedError(msg)
354363
if allowBackslashEscapingAnyCharacter is not None:
355-
raise ContributionsAcceptedError("The 'allowBackslashEscapingAnyCharacter' option is not supported")
364+
msg = "The 'allowBackslashEscapingAnyCharacter' option is not supported"
365+
raise ContributionsAcceptedError(msg)
356366
if mode is not None:
357-
raise ContributionsAcceptedError("The 'mode' option is not supported")
367+
msg = "The 'mode' option is not supported"
368+
raise ContributionsAcceptedError(msg)
358369
if columnNameOfCorruptRecord is not None:
359-
raise ContributionsAcceptedError("The 'columnNameOfCorruptRecord' option is not supported")
370+
msg = "The 'columnNameOfCorruptRecord' option is not supported"
371+
raise ContributionsAcceptedError(msg)
360372
if dateFormat is not None:
361-
raise ContributionsAcceptedError("The 'dateFormat' option is not supported")
373+
msg = "The 'dateFormat' option is not supported"
374+
raise ContributionsAcceptedError(msg)
362375
if timestampFormat is not None:
363-
raise ContributionsAcceptedError("The 'timestampFormat' option is not supported")
376+
msg = "The 'timestampFormat' option is not supported"
377+
raise ContributionsAcceptedError(msg)
364378
if multiLine is not None:
365-
raise ContributionsAcceptedError("The 'multiLine' option is not supported")
379+
msg = "The 'multiLine' option is not supported"
380+
raise ContributionsAcceptedError(msg)
366381
if allowUnquotedControlChars is not None:
367-
raise ContributionsAcceptedError("The 'allowUnquotedControlChars' option is not supported")
382+
msg = "The 'allowUnquotedControlChars' option is not supported"
383+
raise ContributionsAcceptedError(msg)
368384
if lineSep is not None:
369-
raise ContributionsAcceptedError("The 'lineSep' option is not supported")
385+
msg = "The 'lineSep' option is not supported"
386+
raise ContributionsAcceptedError(msg)
370387
if samplingRatio is not None:
371-
raise ContributionsAcceptedError("The 'samplingRatio' option is not supported")
388+
msg = "The 'samplingRatio' option is not supported"
389+
raise ContributionsAcceptedError(msg)
372390
if dropFieldIfAllNull is not None:
373-
raise ContributionsAcceptedError("The 'dropFieldIfAllNull' option is not supported")
391+
msg = "The 'dropFieldIfAllNull' option is not supported"
392+
raise ContributionsAcceptedError(msg)
374393
if encoding is not None:
375-
raise ContributionsAcceptedError("The 'encoding' option is not supported")
394+
msg = "The 'encoding' option is not supported"
395+
raise ContributionsAcceptedError(msg)
376396
if locale is not None:
377-
raise ContributionsAcceptedError("The 'locale' option is not supported")
397+
msg = "The 'locale' option is not supported"
398+
raise ContributionsAcceptedError(msg)
378399
if pathGlobFilter is not None:
379-
raise ContributionsAcceptedError("The 'pathGlobFilter' option is not supported")
400+
msg = "The 'pathGlobFilter' option is not supported"
401+
raise ContributionsAcceptedError(msg)
380402
if recursiveFileLookup is not None:
381-
raise ContributionsAcceptedError("The 'recursiveFileLookup' option is not supported")
403+
msg = "The 'recursiveFileLookup' option is not supported"
404+
raise ContributionsAcceptedError(msg)
382405
if modifiedBefore is not None:
383-
raise ContributionsAcceptedError("The 'modifiedBefore' option is not supported")
406+
msg = "The 'modifiedBefore' option is not supported"
407+
raise ContributionsAcceptedError(msg)
384408
if modifiedAfter is not None:
385-
raise ContributionsAcceptedError("The 'modifiedAfter' option is not supported")
409+
msg = "The 'modifiedAfter' option is not supported"
410+
raise ContributionsAcceptedError(msg)
386411
if allowNonNumericNumbers is not None:
387-
raise ContributionsAcceptedError("The 'allowNonNumericNumbers' option is not supported")
412+
msg = "The 'allowNonNumericNumbers' option is not supported"
413+
raise ContributionsAcceptedError(msg)
388414

389415
if isinstance(path, str):
390416
path = [path]

duckdb/experimental/spark/sql/types.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ def fromInternal(self, obj: T) -> T:
731731
return self.dataType.fromInternal(obj)
732732

733733
def typeName(self) -> str: # type: ignore[override]
734-
raise TypeError("StructField does not have typeName. Use typeName on its type explicitly instead.")
734+
msg = "StructField does not have typeName. Use typeName on its type explicitly instead."
735+
raise TypeError(msg)
735736

736737

737738
class StructType(DataType):
@@ -841,7 +842,8 @@ def add(
841842
self.names.append(field.name)
842843
else:
843844
if isinstance(field, str) and data_type is None:
844-
raise ValueError("Must specify DataType if passing name of struct_field to create.")
845+
msg = "Must specify DataType if passing name of struct_field to create."
846+
raise ValueError(msg)
845847
else:
846848
data_type_f = data_type
847849
self.fields.append(StructField(field, data_type_f, nullable, metadata))
@@ -866,16 +868,19 @@ def __getitem__(self, key: Union[str, int]) -> StructField:
866868
for field in self:
867869
if field.name == key:
868870
return field
869-
raise KeyError(f"No StructField named {key}")
871+
msg = f"No StructField named {key}"
872+
raise KeyError(msg)
870873
elif isinstance(key, int):
871874
try:
872875
return self.fields[key]
873876
except IndexError:
874-
raise IndexError("StructType index out of range")
877+
msg = "StructType index out of range"
878+
raise IndexError(msg)
875879
elif isinstance(key, slice):
876880
return StructType(self.fields[key])
877881
else:
878-
raise TypeError("StructType keys should be strings, integers or slices")
882+
msg = "StructType keys should be strings, integers or slices"
883+
raise TypeError(msg)
879884

880885
def simpleString(self) -> str:
881886
return "struct<%s>" % (",".join(f.simpleString() for f in self))
@@ -978,12 +983,14 @@ def typeName(cls) -> str:
978983
@classmethod
979984
def sqlType(cls) -> DataType:
980985
"""Underlying SQL storage type for this UDT."""
981-
raise NotImplementedError("UDT must implement sqlType().")
986+
msg = "UDT must implement sqlType()."
987+
raise NotImplementedError(msg)
982988

983989
@classmethod
984990
def module(cls) -> str:
985991
"""The Python module of the UDT."""
986-
raise NotImplementedError("UDT must implement module().")
992+
msg = "UDT must implement module()."
993+
raise NotImplementedError(msg)
987994

988995
@classmethod
989996
def scalaUDT(cls) -> str:
@@ -1013,11 +1020,13 @@ def fromInternal(self, obj: Any) -> Any:
10131020

10141021
def serialize(self, obj: Any) -> Any:
10151022
"""Converts a user-type object into a SQL datum."""
1016-
raise NotImplementedError("UDT must implement toInternal().")
1023+
msg = "UDT must implement toInternal()."
1024+
raise NotImplementedError(msg)
10171025

10181026
def deserialize(self, datum: Any) -> Any:
10191027
"""Converts a SQL datum into a user-type object."""
1020-
raise NotImplementedError("UDT must implement fromInternal().")
1028+
msg = "UDT must implement fromInternal()."
1029+
raise NotImplementedError(msg)
10211030

10221031
def simpleString(self) -> str:
10231032
return "udt"
@@ -1126,7 +1135,8 @@ def __new__(cls, **kwargs: Any) -> "Row": ...
11261135

11271136
def __new__(cls, *args: Optional[str], **kwargs: Optional[Any]) -> "Row":
11281137
if args and kwargs:
1129-
raise ValueError("Can not use both args and kwargs to create Row")
1138+
msg = "Can not use both args and kwargs to create Row"
1139+
raise ValueError(msg)
11301140
if kwargs:
11311141
# create row objects
11321142
row = tuple.__new__(cls, list(kwargs.values()))
@@ -1163,7 +1173,8 @@ def asDict(self, recursive: bool = False) -> dict[str, Any]:
11631173
True
11641174
"""
11651175
if not hasattr(self, "__fields__"):
1166-
raise TypeError("Cannot convert a Row class into dict")
1176+
msg = "Cannot convert a Row class into dict"
1177+
raise TypeError(msg)
11671178

11681179
if recursive:
11691180

@@ -1224,7 +1235,8 @@ def __getattr__(self, item: str) -> Any:
12241235

12251236
def __setattr__(self, key: Any, value: Any) -> None:
12261237
if key != "__fields__":
1227-
raise RuntimeError("Row is read-only")
1238+
msg = "Row is read-only"
1239+
raise RuntimeError(msg)
12281240
self.__dict__[key] = value
12291241

12301242
def __reduce__(

0 commit comments

Comments
 (0)