Skip to content

Commit

Permalink
Fix mypy errors. (#10907)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis authored Oct 18, 2024
1 parent 3f9bfaf commit 9053d42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
8 changes: 6 additions & 2 deletions python-package/xgboost/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
TYPE_CHECKING,
Any,
AnyStr,
Callable,
Dict,
List,
Expand All @@ -28,7 +29,10 @@
BoosterParam = Union[List, Dict[str, Any]] # better be sequence

ArrayLike = Any
PathLike = Union[str, os.PathLike]
if TYPE_CHECKING:
PathLike = Union[str, os.PathLike[str]]
else:
PathLike = Union[str, os.PathLike]
CupyT = ArrayLike # maybe need a stub for cupy arrays
NumpyOrCupy = Any
NumpyDType = Union[str, Type[np.number]] # pylint: disable=invalid-name
Expand All @@ -48,7 +52,7 @@
# c_bst_ulong corresponds to bst_ulong defined in xgboost/c_api.h
c_bst_ulong = ctypes.c_uint64 # pylint: disable=C0103

ModelIn = Union[str, bytearray, os.PathLike]
ModelIn = Union[os.PathLike[AnyStr], bytearray, str]

CTypeT = TypeVar(
"CTypeT",
Expand Down
27 changes: 16 additions & 11 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Sequence,
Tuple,
Type,
TypeGuard,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -53,6 +54,7 @@
IterationRange,
ModelIn,
NumpyOrCupy,
PathLike,
TransformedData,
c_bst_ulong,
)
Expand Down Expand Up @@ -1112,7 +1114,7 @@ def set_uint_info(self, field: str, data: ArrayLike) -> None:

dispatch_meta_backend(self, data, field, "uint32")

def save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None:
def save_binary(self, fname: PathLike, silent: bool = True) -> None:
"""Save DMatrix to an XGBoost buffer. Saved binary can be later loaded
by providing the path to :py:func:`xgboost.DMatrix` as input.
Expand Down Expand Up @@ -2735,7 +2737,7 @@ def inplace_predict(
"Data type:" + str(type(data)) + " not supported by inplace prediction."
)

def save_model(self, fname: Union[str, os.PathLike]) -> None:
def save_model(self, fname: PathLike) -> None:
"""Save the model to a file.
The model is saved in an XGBoost internal format which is universal among the
Expand Down Expand Up @@ -2808,9 +2810,12 @@ def load_model(self, fname: ModelIn) -> None:
Input file name or memory buffer(see also save_raw)
"""
if isinstance(fname, (str, os.PathLike)):
# assume file name, cannot use os.path.exist to check, file can be
# from URL.

def is_pathlike(path: ModelIn) -> TypeGuard[os.PathLike[str]]:
return isinstance(path, os.PathLike)

if isinstance(fname, str) or is_pathlike(fname):
# assume file name, cannot use os.path.exist to check, file can be from URL.
fname = os.fspath(os.path.expanduser(fname))
_check_call(_LIB.XGBoosterLoadModel(self.handle, c_str(fname)))
elif isinstance(fname, bytearray):
Expand Down Expand Up @@ -2870,8 +2875,8 @@ def num_features(self) -> int:

def dump_model(
self,
fout: Union[str, os.PathLike],
fmap: Union[str, os.PathLike] = "",
fout: PathLike,
fmap: PathLike = "",
with_stats: bool = False,
dump_format: str = "text",
) -> None:
Expand Down Expand Up @@ -2915,7 +2920,7 @@ def dump_model(

def get_dump(
self,
fmap: Union[str, os.PathLike] = "",
fmap: PathLike = "",
with_stats: bool = False,
dump_format: str = "text",
) -> List[str]:
Expand Down Expand Up @@ -2968,7 +2973,7 @@ def get_fscore(
return self.get_score(fmap, importance_type="weight")

def get_score(
self, fmap: Union[str, os.PathLike] = "", importance_type: str = "weight"
self, fmap: PathLike = "", importance_type: str = "weight"
) -> Dict[str, Union[float, List[float]]]:
"""Get feature importance of each feature.
For tree model Importance type can be defined as:
Expand Down Expand Up @@ -3033,7 +3038,7 @@ def get_score(
return results

# pylint: disable=too-many-statements
def trees_to_dataframe(self, fmap: Union[str, os.PathLike] = "") -> DataFrame:
def trees_to_dataframe(self, fmap: PathLike = "") -> DataFrame:
"""Parse a boosted tree model text dump into a pandas DataFrame structure.
This feature is only defined when the decision tree model is chosen as base
Expand Down Expand Up @@ -3199,7 +3204,7 @@ def _validate_features(self, feature_names: Optional[FeatureNames]) -> None:
def get_split_value_histogram(
self,
feature: str,
fmap: Union[os.PathLike, str] = "",
fmap: PathLike = "",
bins: Optional[int] = None,
as_pandas: bool = True,
) -> Union[np.ndarray, DataFrame]:
Expand Down
17 changes: 4 additions & 13 deletions python-package/xgboost/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
import json
import os
import warnings
from typing import (
Any,
Callable,
List,
Optional,
Sequence,
Tuple,
TypeGuard,
Union,
cast,
)
from typing import Any, Callable, List, Optional, Sequence, Tuple, TypeGuard, cast

import numpy as np

Expand All @@ -27,6 +17,7 @@
FloatCompatible,
NumpyDType,
PandasDType,
PathLike,
TransformedData,
c_bst_ulong,
)
Expand Down Expand Up @@ -1078,12 +1069,12 @@ def _from_dlpack(
return _from_cupy_array(data, missing, nthread, feature_names, feature_types)


def _is_uri(data: DataType) -> TypeGuard[Union[str, os.PathLike]]:
def _is_uri(data: DataType) -> TypeGuard[PathLike]:
return isinstance(data, (str, os.PathLike))


def _from_uri(
data: Union[str, os.PathLike],
data: PathLike,
missing: Optional[FloatCompatible],
feature_names: Optional[FeatureNames],
feature_types: Optional[FeatureTypes],
Expand Down
4 changes: 3 additions & 1 deletion python-package/xgboost/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
memory,
)

from .._typing import PathLike

hypothesis = pytest.importorskip("hypothesis")

# pylint:disable=wrong-import-position,wrong-import-order
Expand Down Expand Up @@ -746,7 +748,7 @@ class DirectoryExcursion:
"""

def __init__(self, path: Union[os.PathLike, str], cleanup: bool = False):
def __init__(self, path: PathLike, cleanup: bool = False):
self.path = path
self.curdir = os.path.normpath(os.path.abspath(os.path.curdir))
self.cleanup = cleanup
Expand Down

0 comments on commit 9053d42

Please sign in to comment.