From f0f76259c920bfbc6c34f0239e74561fea8f4ec3 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Fri, 22 Apr 2022 19:07:51 +0800 Subject: [PATCH] Remove `STRING_TYPES`. (#7827) --- python-package/xgboost/callback.py | 3 +-- python-package/xgboost/compat.py | 3 --- python-package/xgboost/core.py | 14 +++++++------- python-package/xgboost/rabit.py | 4 ++-- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/python-package/xgboost/callback.py b/python-package/xgboost/callback.py index a0189c15a..32d408f3a 100644 --- a/python-package/xgboost/callback.py +++ b/python-package/xgboost/callback.py @@ -16,7 +16,6 @@ import numpy from . import rabit from .core import Booster, DMatrix, XGBoostError, _get_booster_layer_trees -from .compat import STRING_TYPES __all__ = [ @@ -82,7 +81,7 @@ def _aggcv(rlist: List[str]) -> List[Tuple[str, float, float]]: results = [] for (_, name), s in sorted(cvmap.items(), key=lambda x: x[0][0]): as_arr = numpy.array(s) - if not isinstance(msg, STRING_TYPES): + if not isinstance(msg, str): msg = msg.decode() mean, std = numpy.mean(as_arr), numpy.std(as_arr) results.extend([(name, mean, std)]) diff --git a/python-package/xgboost/compat.py b/python-package/xgboost/compat.py index 256a77adf..1967ffc8e 100644 --- a/python-package/xgboost/compat.py +++ b/python-package/xgboost/compat.py @@ -10,9 +10,6 @@ import numpy as np assert (sys.version_info[0] == 3), 'Python 2 is no longer supported.' -# pylint: disable=invalid-name, redefined-builtin -STRING_TYPES = (str,) - def py_str(x): """convert c string back to python string""" diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 1c537d365..5972db02f 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -17,7 +17,7 @@ from inspect import signature, Parameter import numpy as np import scipy.sparse -from .compat import STRING_TYPES, DataFrame, py_str, PANDAS_INSTALLED +from .compat import DataFrame, py_str, PANDAS_INSTALLED from .libpath import find_lib_path from ._typing import ( CStrPptr, @@ -1387,7 +1387,7 @@ class Booster: _check_call( _LIB.XGBoosterUnserializeFromBuffer(self.handle, ptr, length)) self.__dict__.update(state) - elif isinstance(model_file, (STRING_TYPES, os.PathLike, bytearray)): + elif isinstance(model_file, (str, os.PathLike, bytearray)): self.load_model(model_file) elif model_file is None: pass @@ -1629,7 +1629,7 @@ class Booster: """ for key, value in kwargs.items(): if value is not None: - if not isinstance(value, STRING_TYPES): + if not isinstance(value, str): raise ValueError("Set Attr only accepts string values") value = c_str(str(value)) _check_call(_LIB.XGBoosterSetAttr( @@ -1705,7 +1705,7 @@ class Booster: """ if isinstance(params, Mapping): params = params.items() - elif isinstance(params, STRING_TYPES) and value is not None: + elif isinstance(params, str) and value is not None: params = [(params, value)] for key, val in params: if val is not None: @@ -1796,7 +1796,7 @@ class Booster: for d in evals: if not isinstance(d[0], DMatrix): raise TypeError(f"expected DMatrix, got {type(d[0]).__name__}") - if not isinstance(d[1], STRING_TYPES): + if not isinstance(d[1], str): raise TypeError(f"expected string, got {type(d[1]).__name__}") self._validate_features(d[0]) @@ -2192,7 +2192,7 @@ class Booster: Output file name """ - if isinstance(fname, (STRING_TYPES, os.PathLike)): # assume file name + if isinstance(fname, (str, os.PathLike)): # assume file name fname = os.fspath(os.path.expanduser(fname)) _check_call(_LIB.XGBoosterSaveModel( self.handle, c_str(fname))) @@ -2301,7 +2301,7 @@ class Booster: dump_format : string, optional Format of model dump file. Can be 'text' or 'json'. """ - if isinstance(fout, (STRING_TYPES, os.PathLike)): + if isinstance(fout, (str, os.PathLike)): fout = os.fspath(os.path.expanduser(fout)) # pylint: disable=consider-using-with fout_obj = open(fout, 'w', encoding="utf-8") diff --git a/python-package/xgboost/rabit.py b/python-package/xgboost/rabit.py index 29723f4d0..a28448df8 100644 --- a/python-package/xgboost/rabit.py +++ b/python-package/xgboost/rabit.py @@ -6,7 +6,7 @@ from typing import Any, TypeVar, Callable, Optional, cast, List, Union import numpy as np -from .core import _LIB, c_str, STRING_TYPES, _check_call +from .core import _LIB, c_str, _check_call def _init_rabit() -> None: @@ -73,7 +73,7 @@ def tracker_print(msg: Any) -> None: msg : str The message to be printed to tracker. """ - if not isinstance(msg, STRING_TYPES): + if not isinstance(msg, str): msg = str(msg) is_dist = _LIB.RabitIsDistributed() if is_dist != 0: