Skip to content

Commit 1ae6a10

Browse files
docs: indicate where OutOfBoundsError and ColumnNotFoundError are raised (#761)
### Summary of Changes Indicate in the docstrings where `OutOfBoundsError` and `ColumnNotFoundError` are raised. Previously, we only specified generic errors like `ValueError` and `KeyError`. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
1 parent c6a4073 commit 1ae6a10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+145
-143
lines changed

src/safeds/_validation/_normalize_and_check_file_path.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ def _normalize_and_check_file_path(
4444
if not path.suffix:
4545
path = path.with_suffix(canonical_file_extension)
4646
elif path.suffix not in valid_file_extensions:
47-
raise FileExtensionError(path, valid_file_extensions)
47+
message = _build_file_extension_error_message(path.suffix, valid_file_extensions)
48+
raise FileExtensionError(message)
4849

4950
# Check if file exists
5051
if check_if_file_exists and not path.is_file():
5152
raise FileNotFoundError(f"File not found: {path}")
5253

5354
return path
55+
56+
57+
def _build_file_extension_error_message(actual_file_extension: str, valid_file_extensions: list[str]) -> str:
58+
return f"Expected path with extension in {valid_file_extensions} but got {actual_file_extension}."

src/safeds/data/labeled/containers/_tabular_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TabularDataset:
4141
4242
Raises
4343
------
44-
KeyError
44+
ColumnNotFoundError
4545
If a column name is not found in the data.
4646
ValueError
4747
If the target column is also an extra column.
@@ -214,6 +214,7 @@ def _into_dataloader_with_classes(self, batch_size: int, num_of_classes: int) ->
214214
generator=torch.Generator(device=_get_device()),
215215
)
216216

217+
217218
# TODO
218219
def _create_dataset(features: Tensor, target: Tensor) -> Dataset:
219220
import torch

src/safeds/data/labeled/containers/_time_series_dataset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ def _into_dataloader_with_window(self, window_size: int, forecast_horizon: int,
199199
200200
Raises
201201
------
202-
OutOfBoundsError:
202+
OutOfBoundsError
203203
If window_size or forecast_horizon is below 1
204-
ValueError:
204+
ValueError
205205
If the size is smaller or even than forecast_horizon + window_size
206206
207207
Returns
@@ -262,9 +262,9 @@ def _into_dataloader_with_window_predict(
262262
263263
Raises
264264
------
265-
OutOfBoundsError:
265+
OutOfBoundsError
266266
If window_size or forecast_horizon is below 1
267-
ValueError:
267+
ValueError
268268
If the size is smaller or even than forecast_horizon + window_size
269269
270270
Returns

src/safeds/data/tabular/containers/_row.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def get_value(self, name: str) -> Cell:
7979
-------
8080
value:
8181
The value of the column.
82+
83+
Raises
84+
------
85+
ColumnNotFoundError
86+
If the column name does not exist.
8287
"""
8388

8489
@abstractmethod
@@ -95,6 +100,11 @@ def get_column_type(self, name: str) -> DataType:
95100
-------
96101
type:
97102
The type of the column.
103+
104+
Raises
105+
------
106+
ColumnNotFoundError
107+
If the column name does not exist.
98108
"""
99109

100110
@abstractmethod

src/safeds/data/tabular/containers/_table.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ def from_json_file(path: str | Path) -> Table:
238238
import polars as pl
239239

240240
path = _normalize_and_check_file_path(path, ".json", [".json"], check_if_file_exists=True)
241-
return Table._from_polars_data_frame(pl.read_json(path))
241+
242+
try:
243+
return Table._from_polars_data_frame(pl.read_json(path))
244+
except pl.PolarsPanicError:
245+
# Can happen if the JSON file is empty (https://github.com/pola-rs/polars/issues/10234)
246+
return Table()
242247

243248
@staticmethod
244249
def from_parquet_file(path: str | Path) -> Table:
@@ -347,8 +352,14 @@ def __str__(self) -> str:
347352

348353
@property
349354
def _data_frame(self) -> pl.DataFrame:
355+
import polars as pl
356+
350357
if self.__data_frame_cache is None:
351-
self.__data_frame_cache = self._lazy_frame.collect()
358+
try:
359+
self.__data_frame_cache = self._lazy_frame.collect()
360+
except pl.NoDataError:
361+
# Can happen if e.g. a CSV file is empty
362+
self.__data_frame_cache = pl.DataFrame()
352363

353364
return self.__data_frame_cache
354365

@@ -364,7 +375,7 @@ def column_names(self) -> list[str]:
364375
>>> table.column_names
365376
['a', 'b']
366377
"""
367-
return self._lazy_frame.columns
378+
return self.schema.column_names
368379

369380
@property
370381
def number_of_columns(self) -> int:
@@ -378,7 +389,13 @@ def number_of_columns(self) -> int:
378389
>>> table.number_of_columns
379390
2
380391
"""
381-
return self._lazy_frame.width
392+
import polars as pl
393+
394+
try:
395+
return self._lazy_frame.width
396+
except pl.NoDataError:
397+
# Can happen if e.g. a CSV file is empty
398+
return 0
382399

383400
@property
384401
def number_of_rows(self) -> int:
@@ -404,7 +421,13 @@ def plot(self) -> TablePlotter:
404421
@property
405422
def schema(self) -> Schema:
406423
"""The schema of the table."""
407-
return _PolarsSchema(self._lazy_frame.schema)
424+
import polars as pl
425+
426+
try:
427+
return _PolarsSchema(self._lazy_frame.schema)
428+
except pl.NoDataError:
429+
# Can happen if e.g. a CSV file is empty
430+
return _PolarsSchema({})
408431

409432
# ------------------------------------------------------------------------------------------------------------------
410433
# Column operations
@@ -534,7 +557,7 @@ def get_column(self, name: str) -> Column:
534557
535558
Raises
536559
------
537-
KeyError
560+
ColumnNotFoundError
538561
If the column does not exist.
539562
540563
Examples
@@ -571,7 +594,7 @@ def get_column_type(self, name: str) -> DataType:
571594
572595
Raises
573596
------
574-
KeyError
597+
ColumnNotFoundError
575598
If the column does not exist.
576599
577600
Examples
@@ -684,7 +707,7 @@ def remove_columns_except(
684707
685708
Raises
686709
------
687-
KeyError
710+
ColumnNotFoundError
688711
If a column does not exist.
689712
690713
Examples
@@ -800,7 +823,7 @@ def rename_column(self, old_name: str, new_name: str) -> Table:
800823
801824
Raises
802825
------
803-
KeyError
826+
ColumnNotFoundError
804827
If no column with the old name exists.
805828
806829
Examples
@@ -848,7 +871,7 @@ def replace_column(
848871
849872
Raises
850873
------
851-
KeyError
874+
ColumnNotFoundError
852875
If no column with the old name exists.
853876
854877
Examples
@@ -941,7 +964,7 @@ def transform_column(
941964
942965
Raises
943966
------
944-
KeyError
967+
ColumnNotFoundError
945968
If no column with the specified name exists.
946969
947970
Examples
@@ -1067,7 +1090,7 @@ def remove_rows_by_column(
10671090
10681091
Raises
10691092
------
1070-
KeyError
1093+
ColumnNotFoundError
10711094
If the column does not exist.
10721095
10731096
Examples
@@ -1359,7 +1382,7 @@ def sort_rows_by_column(
13591382
13601383
Raises
13611384
------
1362-
KeyError
1385+
ColumnNotFoundError
13631386
If the column does not exist.
13641387
13651388
Examples
@@ -1417,7 +1440,7 @@ def split_rows(
14171440
14181441
Raises
14191442
------
1420-
ValueError
1443+
OutOfBoundsError
14211444
If `percentage_in_first` is not between 0 and 1.
14221445
14231446
Examples

src/safeds/data/tabular/plotting/_table_plotter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def line_plot(self, x_name: str, y_name: str) -> Image:
227227
228228
Raises
229229
------
230-
KeyError
230+
ColumnNotFoundError
231231
If a column does not exist.
232232
TypeError
233233
If a column is not numeric.
@@ -290,7 +290,7 @@ def scatter_plot(self, x_name: str, y_name: str) -> Image:
290290
291291
Raises
292292
------
293-
KeyError
293+
ColumnNotFoundError
294294
If a column does not exist.
295295
TypeError
296296
If a column is not numeric.

src/safeds/data/tabular/transformation/_discretizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> Discretizer:
6161
If the table is empty.
6262
NonNumericColumnError
6363
If one of the columns, that should be fitted is non-numeric.
64-
UnknownColumnNameError
64+
ColumnNotFoundError
6565
If one of the columns, that should be fitted is not in the table.
6666
"""
6767
from sklearn.preprocessing import KBinsDiscretizer as sk_KBinsDiscretizer
@@ -112,7 +112,7 @@ def transform(self, table: Table) -> Table:
112112
If the transformer has not been fitted yet.
113113
ValueError
114114
If the table is empty.
115-
UnknownColumnNameError
115+
ColumnNotFoundError
116116
If one of the columns, that should be transformed is not in the table.
117117
NonNumericColumnError
118118
If one of the columns, that should be fitted is non-numeric.

src/safeds/data/tabular/transformation/_label_encoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> LabelEncoder:
4040
4141
Raises
4242
------
43-
UnknownColumnNameError
43+
ColumnNotFoundError
4444
If column_names contain a column name that is missing in the table.
4545
ValueError
4646
If the table contains 0 rows.
@@ -99,7 +99,7 @@ def transform(self, table: Table) -> Table:
9999
------
100100
TransformerNotFittedError
101101
If the transformer has not been fitted yet.
102-
UnknownColumnNameError
102+
ColumnNotFoundError
103103
If the input table does not contain all columns used to fit the transformer.
104104
ValueError
105105
If the table contains 0 rows.
@@ -141,7 +141,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
141141
------
142142
TransformerNotFittedError
143143
If the transformer has not been fitted yet.
144-
UnknownColumnNameError
144+
ColumnNotFoundError
145145
If the input table does not contain all columns used to fit the transformer.
146146
NonNumericColumnError
147147
If the specified columns of the input table contain non-numerical data.

src/safeds/data/tabular/transformation/_one_hot_encoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> OneHotEncoder:
9696
9797
Raises
9898
------
99-
UnknownColumnNameError
99+
ColumnNotFoundError
100100
If column_names contain a column name that is missing in the table.
101101
ValueError
102102
If the table contains 0 rows.
@@ -169,7 +169,7 @@ def transform(self, table: Table) -> Table:
169169
------
170170
TransformerNotFittedError
171171
If the transformer has not been fitted yet.
172-
UnknownColumnNameError
172+
ColumnNotFoundError
173173
If the input table does not contain all columns used to fit the transformer.
174174
ValueError
175175
If the table contains 0 rows.
@@ -252,7 +252,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
252252
------
253253
TransformerNotFittedError
254254
If the transformer has not been fitted yet.
255-
UnknownColumnNameError
255+
ColumnNotFoundError
256256
If the input table does not contain all columns used to fit the transformer.
257257
NonNumericColumnError
258258
If the transformed columns of the input table contain non-numerical data.

src/safeds/data/tabular/transformation/_range_scaler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> RangeScaler:
5757
5858
Raises
5959
------
60-
UnknownColumnNameError
60+
ColumnNotFoundError
6161
If column_names contain a column name that is missing in the table.
6262
NonNumericColumnError
6363
If at least one of the specified columns in the table contains non-numerical data.
@@ -121,7 +121,7 @@ def transform(self, table: Table) -> Table:
121121
------
122122
TransformerNotFittedError
123123
If the transformer has not been fitted yet.
124-
UnknownColumnNameError
124+
ColumnNotFoundError
125125
If the input table does not contain all columns used to fit the transformer.
126126
NonNumericColumnError
127127
If at least one of the columns in the input table that is used to fit contains non-numerical data.
@@ -180,7 +180,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
180180
------
181181
TransformerNotFittedError
182182
If the transformer has not been fitted yet.
183-
UnknownColumnNameError
183+
ColumnNotFoundError
184184
If the input table does not contain all columns used to fit the transformer.
185185
NonNumericColumnError
186186
If the transformed columns of the input table contain non-numerical data.

src/safeds/data/tabular/transformation/_simple_imputer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> SimpleImputer:
132132
133133
Raises
134134
------
135-
UnknownColumnNameError
135+
ColumnNotFoundError
136136
If column_names contain a column name that is missing in the table
137137
ValueError
138138
If the table contains 0 rows
@@ -213,7 +213,7 @@ def transform(self, table: Table) -> Table:
213213
------
214214
TransformerNotFittedError
215215
If the transformer has not been fitted yet.
216-
UnknownColumnNameError
216+
ColumnNotFoundError
217217
If the input table does not contain all columns used to fit the transformer.
218218
ValueError
219219
If the table contains 0 rows.

src/safeds/data/tabular/transformation/_standard_scaler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> StandardScaler:
3939
4040
Raises
4141
------
42-
UnknownColumnNameError
42+
ColumnNotFoundError
4343
If column_names contain a column name that is missing in the table.
4444
NonNumericColumnError
4545
If at least one of the specified columns in the table contains non-numerical data.
@@ -103,7 +103,7 @@ def transform(self, table: Table) -> Table:
103103
------
104104
TransformerNotFittedError
105105
If the transformer has not been fitted yet.
106-
UnknownColumnNameError
106+
ColumnNotFoundError
107107
If the input table does not contain all columns used to fit the transformer.
108108
NonNumericColumnError
109109
If at least one of the columns in the input table that is used to fit contains non-numerical data.
@@ -162,7 +162,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
162162
------
163163
TransformerNotFittedError
164164
If the transformer has not been fitted yet.
165-
UnknownColumnNameError
165+
ColumnNotFoundError
166166
If the input table does not contain all columns used to fit the transformer.
167167
NonNumericColumnError
168168
If the transformed columns of the input table contain non-numerical data.

0 commit comments

Comments
 (0)