项目文件夹

文件
Elías Snorrason 8b60a381e4 validation.py: Annotate function args and return values (#317)
* 🏷️ annotate function args and return values

Starting with the validation module:

- I think (X, y) might need some custom Union type to handle both numpy arrays and pandas dataframe, etc.
- All of the "assert" functions return None.

Ref #307

* refactor: 🏷️ swap npt.NDArray -> np.ndarray

np.ndarray seems more consistent with the rest of the repo.
Maybe it's necessary to go back to npt.NDArray when disallowing generics?

See numpy docs: https://numpy.org/devdocs/reference/typing.html#numpy.typing.NDArray

* refactor: 🔥 remove unused import

* 🏷️ unconstrain X input types

* 🐛 handle label type issues

- Have to restrict the output of labels_to_array to pass mypy checks.
- Returning the values of pd.Series isn't type-stable.

* 🏷️ include np.generic in arg-type union

* test:  test labels_to_array

* 🏷️ add type aliases for X and y

* 🚨 ignore type-checks for pandas indexing assertions

CI typechecker runs on Python 3.10 which gives this error:

'cleanlab/internal/validation.py:125: error: No overload variant of "__getitem__" of "_iLocIndexerSeries" matches argument type "List[int]"'

It should be fine to let mypy ignore these expressions as they don't return anything.

* 🥅 specify errors to ignore

"type: ignore" doesn't pass strict mypy type-checks unless the specific errors are provided

* 🏷️ annotate label series to array
2022-07-26 12:58:37 -07:00

30 行
925 B
Python

# coding: utf-8
from cleanlab.internal import validation
import numpy as np
import pandas as pd
import pytest
@pytest.mark.parametrize("y_list", [["a", "b", "a"], [0, 1, 2]])
@pytest.mark.parametrize("format", [list, np.array, pd.Series, pd.DataFrame])
def test_labels_to_array_return_types(y_list, format):
y = format(y_list)
labels = validation.labels_to_array(y)
assert isinstance(labels, np.ndarray)
@pytest.mark.parametrize("y_list", [["a", "b", "a"], [0, 1, 2]])
@pytest.mark.parametrize("format", [list, np.array, pd.Series])
def test_labels_to_array_return_values(y_list, format):
y = format(y_list)
labels = validation.labels_to_array(y)
assert np.array_equal(y, labels)
def test_label_to_array_raises_error():
# Pandas DataFrame should have only one column
y = pd.DataFrame({"a": [0, 1], "b": [2, 3]})
with pytest.raises(ValueError):
validation.labels_to_array(y)