Unverified Commit 7de8d8cf authored by Miguel Ángel Cárdenas's avatar Miguel Ángel Cárdenas Committed by GitHub
Browse files

[python-package] add type hints on some functions in `basic.py` (#5362)

* refactor: 🎨 add statuc typing to a subset of basic functions

* refactor: 🎨 add statuc typing to a subset of basic functions

* revert black format on python basic functions file

* style: 🎨

 updating typing for c_array function

* branch rebase

* fix number of changed lines

* Update python-package/lightgbm/basic.py
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent 83627ff0
...@@ -241,7 +241,7 @@ def _data_to_2d_numpy(data: Any, dtype: type = np.float32, name: str = 'list') - ...@@ -241,7 +241,7 @@ def _data_to_2d_numpy(data: Any, dtype: type = np.float32, name: str = 'list') -
"It should be list of lists, numpy 2-D array or pandas DataFrame") "It should be list of lists, numpy 2-D array or pandas DataFrame")
def cfloat32_array_to_numpy(cptr, length): def cfloat32_array_to_numpy(cptr: ctypes.POINTER, length: int) -> np.ndarray:
"""Convert a ctypes float pointer array to a numpy array.""" """Convert a ctypes float pointer array to a numpy array."""
if isinstance(cptr, ctypes.POINTER(ctypes.c_float)): if isinstance(cptr, ctypes.POINTER(ctypes.c_float)):
return np.ctypeslib.as_array(cptr, shape=(length,)).copy() return np.ctypeslib.as_array(cptr, shape=(length,)).copy()
...@@ -249,7 +249,7 @@ def cfloat32_array_to_numpy(cptr, length): ...@@ -249,7 +249,7 @@ def cfloat32_array_to_numpy(cptr, length):
raise RuntimeError('Expected float pointer') raise RuntimeError('Expected float pointer')
def cfloat64_array_to_numpy(cptr, length): def cfloat64_array_to_numpy(cptr: ctypes.POINTER, length: int) -> np.ndarray:
"""Convert a ctypes double pointer array to a numpy array.""" """Convert a ctypes double pointer array to a numpy array."""
if isinstance(cptr, ctypes.POINTER(ctypes.c_double)): if isinstance(cptr, ctypes.POINTER(ctypes.c_double)):
return np.ctypeslib.as_array(cptr, shape=(length,)).copy() return np.ctypeslib.as_array(cptr, shape=(length,)).copy()
...@@ -257,7 +257,7 @@ def cfloat64_array_to_numpy(cptr, length): ...@@ -257,7 +257,7 @@ def cfloat64_array_to_numpy(cptr, length):
raise RuntimeError('Expected double pointer') raise RuntimeError('Expected double pointer')
def cint32_array_to_numpy(cptr, length): def cint32_array_to_numpy(cptr: ctypes.POINTER, length: int) -> np.ndarray:
"""Convert a ctypes int pointer array to a numpy array.""" """Convert a ctypes int pointer array to a numpy array."""
if isinstance(cptr, ctypes.POINTER(ctypes.c_int32)): if isinstance(cptr, ctypes.POINTER(ctypes.c_int32)):
return np.ctypeslib.as_array(cptr, shape=(length,)).copy() return np.ctypeslib.as_array(cptr, shape=(length,)).copy()
...@@ -265,7 +265,7 @@ def cint32_array_to_numpy(cptr, length): ...@@ -265,7 +265,7 @@ def cint32_array_to_numpy(cptr, length):
raise RuntimeError('Expected int32 pointer') raise RuntimeError('Expected int32 pointer')
def cint64_array_to_numpy(cptr, length): def cint64_array_to_numpy(cptr: ctypes.POINTER, length: int) -> np.ndarray:
"""Convert a ctypes int pointer array to a numpy array.""" """Convert a ctypes int pointer array to a numpy array."""
if isinstance(cptr, ctypes.POINTER(ctypes.c_int64)): if isinstance(cptr, ctypes.POINTER(ctypes.c_int64)):
return np.ctypeslib.as_array(cptr, shape=(length,)).copy() return np.ctypeslib.as_array(cptr, shape=(length,)).copy()
...@@ -273,12 +273,12 @@ def cint64_array_to_numpy(cptr, length): ...@@ -273,12 +273,12 @@ def cint64_array_to_numpy(cptr, length):
raise RuntimeError('Expected int64 pointer') raise RuntimeError('Expected int64 pointer')
def c_str(string): def c_str(string: str) -> ctypes.c_char_p:
"""Convert a Python string to C string.""" """Convert a Python string to C string."""
return ctypes.c_char_p(string.encode('utf-8')) return ctypes.c_char_p(string.encode('utf-8'))
def c_array(ctype, values): def c_array(ctype: type, values: List[Any]) -> ctypes.Array:
"""Convert a Python array to C array.""" """Convert a Python array to C array."""
return (ctype * len(values))(*values) return (ctype * len(values))(*values)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment