Skip to content

Commit

Permalink
move ctype pointer to stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengyang Gu committed Mar 15, 2022
1 parent e6098c1 commit 296fd01
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
25 changes: 1 addition & 24 deletions python-package/xgboost/_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ctypes
import os
from typing import Optional, List, Any, TypeVar, Union, Dict
from typing import Optional, List, Any, Union, TypeVar

import numpy as np

Expand All @@ -16,31 +16,8 @@
cupy_t = ArrayLike # maybe need a stub for cupy arrays
numpy_or_cupy_t = Union[np.ndarray, cupy_t]

# ctypes
# c_bst_ulong corresponds to bst_ulong defined in xgboost/c_api.h
c_bst_ulong = ctypes.c_uint64

ctype_t = Union[
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int,
ctypes.c_float, ctypes.c_uint, ctypes.c_size_t
]

# supported numeric types
c_numeric_t = Union[
ctypes.c_float, ctypes.c_double, ctypes.c_uint,
ctypes.c_uint64, ctypes.c_int32, ctypes.c_int64
]

# c pointer types
c_str_ptr_t = ctypes.pointer[ctypes.c_char]
c_str_pptr_t = ctypes.pointer[ctypes.c_char_p]
c_float_ptr_t = ctypes.pointer[ctypes.c_float]

c_numeric_ptr_t = Union[
ctypes.pointer[ctypes.c_float], ctypes.pointer[ctypes.c_double],
ctypes.pointer[ctypes.c_uint], ctypes.pointer[ctypes.c_uint64],
ctypes.pointer[ctypes.c_int32], ctypes.pointer[ctypes.c_int64]
]

# template parameter
T = TypeVar("T")
45 changes: 45 additions & 0 deletions python-package/xgboost/_typing.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ctypes
import os
from typing import Optional, List, Any, TypeVar, Union, Dict

import numpy as np

# os.PathLike/string/numpy.array/scipy.sparse/pd.DataFrame/dt.Frame/cudf.DataFrame/cupy.array/dlpack
DataType = Any

# xgboost accepts some other possible types in practice due to historical reason, which is
# lesser tested. For now we encourage users to pass a simple list of string.
FeatNamesT = Optional[List[str]]

ArrayLike = Any
PathLike = Union[str, os.PathLike]
cupy_t = ArrayLike # maybe need a stub for cupy arrays
numpy_or_cupy_t = Union[np.ndarray, cupy_t]

# ctypes
c_bst_ulong = ctypes.c_uint64

ctype_t = Union[
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int,
ctypes.c_float, ctypes.c_uint, ctypes.c_size_t
]

# supported numeric types
c_numeric_t = Union[
ctypes.c_float, ctypes.c_double, ctypes.c_uint,
ctypes.c_uint64, ctypes.c_int32, ctypes.c_int64
]

# c pointer types
c_str_ptr_t = ctypes.pointer[ctypes.c_char]
c_str_pptr_t = ctypes.pointer[ctypes.c_char_p]
c_float_ptr_t = ctypes.pointer[ctypes.c_float]

c_numeric_ptr_t = Union[
ctypes.pointer[ctypes.c_float], ctypes.pointer[ctypes.c_double],
ctypes.pointer[ctypes.c_uint], ctypes.pointer[ctypes.c_uint64],
ctypes.pointer[ctypes.c_int32], ctypes.pointer[ctypes.c_int64]
]

# template parameter
T = TypeVar("T")

0 comments on commit 296fd01

Please sign in to comment.