Skip to content

Commit

Permalink
[python-package] add type hints for functions accepting dtypes (#5773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Mar 9, 2023
1 parent 1585ee1 commit 0cff4f8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _is_numpy_column_array(data: Any) -> bool:
return len(shape) == 2 and shape[1] == 1


def _cast_numpy_array_to_dtype(array: np.ndarray, dtype: np.dtype) -> np.ndarray:
def _cast_numpy_array_to_dtype(array: np.ndarray, dtype: "np.typing.DTypeLike") -> np.ndarray:
"""Cast numpy array to given dtype."""
if array.dtype == dtype:
return array
Expand All @@ -264,7 +264,7 @@ def _is_1d_collection(data: Any) -> bool:

def _list_to_1d_numpy(
data: Any,
dtype=np.float32,
dtype: "np.typing.DTypeLike" = np.float32,
name: str = 'list'
) -> np.ndarray:
"""Convert data to numpy 1-D array."""
Expand Down Expand Up @@ -303,7 +303,11 @@ def _is_2d_collection(data: Any) -> bool:
)


def _data_to_2d_numpy(data: Any, dtype: type = np.float32, name: str = 'list') -> np.ndarray:
def _data_to_2d_numpy(
data: Any,
dtype: "np.typing.DTypeLike" = np.float32,
name: str = 'list'
) -> np.ndarray:
"""Convert data to numpy 2-D array."""
if _is_numpy_2d_array(data):
return _cast_numpy_array_to_dtype(data, dtype)
Expand Down Expand Up @@ -612,7 +616,7 @@ def _c_int_array(data):
return (ptr_data, type_data, data) # return `data` to avoid the temporary copy is freed


def _is_allowed_numpy_dtype(dtype) -> bool:
def _is_allowed_numpy_dtype(dtype: type) -> bool:
float128 = getattr(np, 'float128', type(None))
return (
issubclass(dtype, (np.integer, np.floating, np.bool_))
Expand Down

0 comments on commit 0cff4f8

Please sign in to comment.