[python-package] prefix all internal object names with "_" #5313
Description
Short Description
I'm opening this RFC to propose the following:
all objects in the Python package which are intended to be internal should have names beginning with
_
The convention "anything starting with _
is internal" is common in Python programming. If LightGBM's Python package followed that convention consistently, it would simplify LightGBM's position on which parts of the Python package are considered "internal" and subject to breaking changes in any release.
Detailed Description
In pursuit of the v4.0.0 release (#5153), we have been accepting breaking changes which simplify the project and reduce its maintenance burden, so that limited maintainer time can be focused on providing a good experience with LightGBM's main functionality.
I think "all internal objects should begin with _
" is one such type of change. Making it clearer which parts of the Python package are considered internal reduces the surface area of the public API which users need to consider, and increases contributors' flexibility to change implementations without breaking user code.
R packages support a "namespace" concept (see "Writing R extensions"), which clearly separates exported and internal objects, and I think that mechanism has been really helpful. The closest thing that Python has, to my knowledge, is the __all__
member for modules (https://docs.python.org/3/tutorial/modules.html#importing-from-a-package). __all__
controls the behavior of "star imports".
For example, if you run the following:
from lightgbm import *
dir()
the only LightGBM objects you'll see are those in the package's __all__
.
This mechanism is a good start, but prefixing all internal objects with _
would be an even stronger signal about which things are internal. It would also improve the consistency of the Python package's code.
Specific Proposal
expand to see original proposal (click me)
I am proposing that the following be prefixed with a _
and treated as internal:
LightGBM/python-package/lightgbm/basic.py
Line 128 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 144 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 155 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 160 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 168 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 175 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 190 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 240 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 248 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 256 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 264 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 272 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 277 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 282 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 292 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Lines 436 to 470 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 472 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 482 in ff947bf
LightGBM/python-package/lightgbm/basic.py
Line 502 in ff947bf
Considerations for Implementation
Check that any name being modified is only used at runtime and not part of the state for objects like Booster
or LGBMClassifier
which might be pickled (since pickle
only stores references to imports and expects to be able to reference them when running load()
).
Status: Accepted
@StrikerRUS @jmoralez @shiyu1994 @guolinke Whenever you have time, will you please comment here on whether or not you support this proposal? Thank you for your time and consideration.
This was originally opened as a request for comment, but it's been accepted and we're now accepting contributions to make more of the Python package's namespace private?
How to Contribute
Anyone is welcome to submit a pull request to help contribute to this effort.
Please limit pull requests to 1 function/method/module per pull request, to make them easier to review.
When you open pull requests, please put Contributes to #5313
in the description.
Add __all__
entries to modules.
-
python-package/basic.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/callback.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/dask.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/engine.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/libpath.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/plotting.py
([python-package] make public API members explicit with module-level __all__ variables #5655) -
python-package/sklearn.py
([python-package] make public API members explicit with module-level __all__ variables #5655)
Prefix objects, functions, class attributes with _
-
basic.ZERO_THRESHOLD
([python-package] prefix internal objects with '_' #5654) -
basic.NUMERIC_TYPES
([python-package] prefix NUMERIC_TYPES with _ to make it a internal object #5409) -
basic.is_numeric()
([python] Prefixbasic.is_numeric()
with _ #5421) -
basic.is_numpy_1d_array()
([python-package] prefix is_numpy_1d_array with _ #5520) -
basic.is_numpy_column_array()
([python-package] prefix is_numpy_column_array with _ #5531) -
basic.cast_numpy_array_to_dtype()
([python-package] prefix cast_numpy_array_to_dtype with _ #5532) -
basic.is_1d_list()
([python-package] prefix several internal functions with _ #5545) -
basic.list_to_1d_numpy()
([python-package] prefix several internal functions with _ #5545) -
basic.cfloat32_array_to_numpy()
([python-package] prefix several internal functions with _ #5545) -
basic.cfloat64_array_to_numpy()
([python-package] prefix several internal functions with _ #5545) -
basic.cint32_array_to_numpy()
([python-package] prefix several internal functions with _ #5545) -
basic.cint64_array_to_numpy()
([python-package] prefix several internal functions with _ #5545) -
basic.c_str()
([python-package] prefix several internal functions with _ #5545) -
basic.c_array()
([python-package] prefix several internal functions with _ #5545) -
basic.json_default_with_numpy()
([python-package] prefix internal objects with '_' #5654) -
basic.param_dict_to_str()
([python-package] prefix internal objects with '_' #5654) -
basic.MAX_INT32
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_DTYPE_FLOAT32
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_DTYPE_FLOAT64
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_DTYPE_INT32
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_DTYPE_INT64
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_IS_ROW_MAJOR
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_PREDICT_NORMAL
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_PREDICT_RAW_SCORE
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_PREDICT_LEAF_INDEX
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_PREDICT_CONTRIB
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_MATRIX_TYPE_CSR
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_MATRIX_TYPE_CSC
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_FEATURE_IMPORTANCE_SPLIT
([python-package] prefix internal objects with '_' #5654) -
basic.C_API_FEATURE_IMPORTANCE_GAIN
([python-package] prefix internal objects with '_' #5654) -
basic.FIELD_TYPE_MAPPER
([python-package] prefix internal objects with '_' #5654) -
basic.FEATURE_IMPORTANCE_TYPE_MAPPER
([python-package] prefix internal objects with '_' #5654) -
basic.convert_from_sliced_object()
([python-package] prefix basic.convert_from_sliced_object with _ #5613) -
basic.c_float_array()
([python-package] prefix c_int_array and c_float_array with _ #5614) -
basic.c_int_array()
([python-package] prefix c_int_array and c_float_array with _ #5614)
Activity