Skip to content

Commit

Permalink
[python] add type hints to python-package/setup.py (#4376)
Browse files Browse the repository at this point in the history
* add type hints to python-package/setup.py

* fix return type hints on python-package/setup.py

Co-authored-by: James Lamb <jaylamb20@gmail.com>

Co-authored-by: James Lamb <jaylamb20@gmail.com>
  • Loading branch information
isxwor and jameslamb authored Jun 15, 2021
1 parent df79713 commit 3d1c229
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions python-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from distutils.dir_util import copy_tree, create_tree, remove_tree
from distutils.file_util import copy_file
from platform import system
from typing import List, Optional

from setuptools import find_packages, setup
from setuptools.command.install import install
Expand All @@ -34,7 +35,7 @@
]


def find_lib():
def find_lib() -> List[str]:
libpath_py = os.path.join(CURRENT_DIR, 'lightgbm', 'libpath.py')
libpath = {'__file__': libpath_py}
exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath)
Expand All @@ -44,9 +45,9 @@ def find_lib():
return LIB_PATH


def copy_files(integrated_opencl=False, use_gpu=False):
def copy_files(integrated_opencl: bool = False, use_gpu: bool = False) -> None:

def copy_files_helper(folder_name):
def copy_files_helper(folder_name: str) -> None:
src = os.path.join(CURRENT_DIR, os.path.pardir, folder_name)
if os.path.exists(src):
dst = os.path.join(CURRENT_DIR, 'compile', folder_name)
Expand Down Expand Up @@ -90,7 +91,7 @@ def copy_files_helper(folder_name):
verbose=0)


def clear_path(path):
def clear_path(path: str) -> None:
if os.path.isdir(path):
contents = os.listdir(path)
for file_name in contents:
Expand All @@ -101,7 +102,7 @@ def clear_path(path):
remove_tree(file_path)


def silent_call(cmd, raise_error=False, error_msg=''):
def silent_call(cmd: List[str], raise_error: bool = False, error_msg: str = '') -> int:
try:
with open(LOG_PATH, "ab") as log:
subprocess.check_call(cmd, stderr=log, stdout=log)
Expand All @@ -112,11 +113,22 @@ def silent_call(cmd, raise_error=False, error_msg=''):
return 1


def compile_cpp(use_mingw=False, use_gpu=False, use_cuda=False, use_mpi=False,
use_hdfs=False, boost_root=None, boost_dir=None,
boost_include_dir=None, boost_librarydir=None,
opencl_include_dir=None, opencl_library=None,
nomp=False, bit32=False, integrated_opencl=False):
def compile_cpp(
use_mingw: bool = False,
use_gpu: bool = False,
use_cuda: bool = False,
use_mpi: bool = False,
use_hdfs: bool = False,
boost_root: Optional[str] = None,
boost_dir: Optional[str] = None,
boost_include_dir: Optional[str] = None,
boost_librarydir: Optional[str] = None,
opencl_include_dir: Optional[str] = None,
opencl_library: Optional[str] = None,
nomp: bool = False,
bit32: bool = False,
integrated_opencl: bool = False
) -> None:

if os.path.exists(os.path.join(CURRENT_DIR, "build_cpp")):
remove_tree(os.path.join(CURRENT_DIR, "build_cpp"))
Expand Down Expand Up @@ -204,7 +216,7 @@ def compile_cpp(use_mingw=False, use_gpu=False, use_cuda=False, use_mpi=False,

class CustomInstallLib(install_lib):

def install(self):
def install(self) -> List[str]:
outfiles = install_lib.install(self)
src = find_lib()[0]
dst = os.path.join(self.install_dir, 'lightgbm')
Expand All @@ -217,7 +229,7 @@ class CustomInstall(install):

user_options = install.user_options + LIGHTGBM_OPTIONS

def initialize_options(self):
def initialize_options(self) -> None:
install.initialize_options(self)
self.mingw = 0
self.integrated_opencl = 0
Expand All @@ -235,7 +247,7 @@ def initialize_options(self):
self.nomp = 0
self.bit32 = 0

def run(self):
def run(self) -> None:
if (8 * struct.calcsize("P")) != 64:
if self.bit32:
logger.warning("You're installing 32-bit version. "
Expand All @@ -260,7 +272,7 @@ class CustomBdistWheel(bdist_wheel):

user_options = bdist_wheel.user_options + LIGHTGBM_OPTIONS

def initialize_options(self):
def initialize_options(self) -> None:
bdist_wheel.initialize_options(self)
self.mingw = 0
self.integrated_opencl = 0
Expand All @@ -278,7 +290,7 @@ def initialize_options(self):
self.nomp = 0
self.bit32 = 0

def finalize_options(self):
def finalize_options(self) -> None:
bdist_wheel.finalize_options(self)

install = self.reinitialize_command('install')
Expand All @@ -302,7 +314,7 @@ def finalize_options(self):

class CustomSdist(sdist):

def run(self):
def run(self) -> None:
copy_files(integrated_opencl=True, use_gpu=True)
open(os.path.join(CURRENT_DIR, '_IS_SOURCE_PACKAGE.txt'), 'w').close()
if os.path.exists(os.path.join(CURRENT_DIR, 'lightgbm', 'Release')):
Expand Down

0 comments on commit 3d1c229

Please sign in to comment.