Skip to content

Commit

Permalink
add type hints for data.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengyang Gu committed Mar 26, 2022
1 parent 0267a1a commit 533f68f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 136 deletions.
6 changes: 5 additions & 1 deletion python-package/xgboost/_typing.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
"""Shared typing definition."""
import ctypes
import os
from typing import Optional, List, Any, TypeVar, Union
from typing import Optional, List, Any, TypeVar, Union, Type

# os.PathLike/string/numpy.array/scipy.sparse/pd.DataFrame/dt.Frame/
# cudf.DataFrame/cupy.array/dlpack
import numpy as np

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.
FeatureNames = Optional[List[str]]
FeatureTypes = Optional[List[str]]

ArrayLike = Any
PathLike = Union[str, os.PathLike]
CupyT = ArrayLike # maybe need a stub for cupy arrays
NumpyOrCupy = Any
NumpyDType = Union[str, Type[np.number]]

# ctypes
# c_bst_ulong corresponds to bst_ulong defined in xgboost/c_api.h
Expand Down
2 changes: 1 addition & 1 deletion python-package/xgboost/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def py_str(x: CStrPtr) -> str:
return x.decode('utf-8') # type: ignore


def lazy_isinstance(instance: Type, module: str, name: str) -> bool:
def lazy_isinstance(instance: Type[object], module: str, name: str) -> bool:
"""Use string representation to identify a type."""

# Notice, we use .__class__ as opposed to type() in order
Expand Down
Loading

0 comments on commit 533f68f

Please sign in to comment.