Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-package] fix some warnings from mypy #3891

Merged
merged 14 commits into from
Feb 15, 2021
2 changes: 1 addition & 1 deletion python-package/lightgbm/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, best_iteration, best_score):


# Callback environment used by callbacks
CallbackEnv = collections.namedtuple(
CallbackEnv = collections.namedtuple( # type: ignore
tara-jawahar marked this conversation as resolved.
Show resolved Hide resolved
"LightGBMCallbackEnv",
["model",
"params",
Expand Down
9 changes: 4 additions & 5 deletions python-package/lightgbm/dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import socket
from collections import defaultdict
from copy import deepcopy
from typing import Any, Callable, Dict, Iterable, List, Optional, Type, Union
from typing import Any, Callable, Dict, Iterable, List, Optional, Type, Union, Set
from urllib.parse import urlparse

import numpy as np
Expand Down Expand Up @@ -77,7 +77,6 @@ def _find_open_port(worker_ip: str, local_listen_port: int, ports_to_skip: Itera
A free port on the machine referenced by ``worker_ip``.
"""
max_tries = 1000
out_port = None
found_port = False
for i in range(max_tries):
out_port = local_listen_port + i
Expand Down Expand Up @@ -117,7 +116,7 @@ def _find_ports_for_workers(client: Client, worker_addresses: Iterable[str], loc
result : Dict[str, int]
Dictionary where keys are worker addresses and values are an open port for LightGBM to use.
"""
lightgbm_ports = set()
lightgbm_ports: Set[int] = set()
worker_ip_to_port = {}
for worker_address in worker_addresses:
port = client.submit(
Expand Down Expand Up @@ -306,11 +305,11 @@ def _train(
wait(parts)

for part in parts:
if part.status == 'error':
if part.status == 'error': # type: ignore
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
tara-jawahar marked this conversation as resolved.
Show resolved Hide resolved
return part # trigger error locally

# Find locations of all parts and map them to particular Dask workers
key_to_part_dict = {part.key: part for part in parts}
key_to_part_dict = {part.key: part for part in parts} # type: ignore
who_has = client.who_has(parts)
worker_map = defaultdict(list)
for key, workers in who_has.items():
Expand Down
12 changes: 6 additions & 6 deletions python-package/lightgbm/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ def fit(self, X, y,
return self

_base_doc = LGBMModel.fit.__doc__
_base_doc = (_base_doc[:_base_doc.find('group :')]
+ _base_doc[_base_doc.find('eval_set :'):])
_base_doc = (_base_doc[:_base_doc.find('group :')] # type: ignore
+ _base_doc[_base_doc.find('eval_set :'):]) # type: ignore
tara-jawahar marked this conversation as resolved.
Show resolved Hide resolved
_base_doc = (_base_doc[:_base_doc.find('eval_class_weight :')]
+ _base_doc[_base_doc.find('eval_init_score :'):])
fit.__doc__ = (_base_doc[:_base_doc.find('eval_group :')]
Expand Down Expand Up @@ -897,8 +897,8 @@ def fit(self, X, y,
return self

_base_doc = LGBMModel.fit.__doc__
_base_doc = (_base_doc[:_base_doc.find('group :')]
+ _base_doc[_base_doc.find('eval_set :'):])
_base_doc = (_base_doc[:_base_doc.find('group :')] # type: ignore
+ _base_doc[_base_doc.find('eval_set :'):]) # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what issue did these # type: ignore comments on _base_doc fix? I ran mypy --ignore-missing-imports python-package/ on current master, and I don't see any notes related to these lines

python-package/lightgbm/dask.py:97: error: Incompatible return value type (got "Optional[int]", expected "int")
python-package/lightgbm/dask.py:120: error: Need type annotation for 'lightgbm_ports' (hint: "lightgbm_ports: Set[<type>] = ...")
python-package/lightgbm/dask.py:309: error: "Dict[str, Any]" has no attribute "status"
python-package/lightgbm/dask.py:310: error: Incompatible return value type (got "Dict[str, Any]", expected "LGBMModel")
python-package/lightgbm/dask.py:313: error: "Dict[str, Any]" has no attribute "key"; maybe "keys"?
python-package/lightgbm/dask.py:478: error: "_DaskLGBMModel" has no attribute "_other_params"
python-package/lightgbm/dask.py:496: error: "_DaskLGBMModel" has no attribute "get_params"
python-package/lightgbm/dask.py:510: error: "_DaskLGBMModel" has no attribute "set_params"
python-package/lightgbm/dask.py:516: error: "_DaskLGBMModel" has no attribute "get_params"
python-package/lightgbm/dask.py:525: error: Item "_DaskLGBMModel" of "Union[_DaskLGBMModel, LGBMModel]" has no attribute "get_params"
python-package/lightgbm/dask.py:587: error: Item "None" of "Optional[str]" has no attribute "partition"
python-package/lightgbm/dask.py:602: error: Signature of "fit" incompatible with supertype "LGBMClassifier"
python-package/lightgbm/dask.py:602: error: Signature of "fit" incompatible with supertype "LGBMModel"
python-package/lightgbm/dask.py:610: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMClassifier")
python-package/lightgbm/dask.py:636: error: Signature of "predict" incompatible with supertype "LGBMClassifier"
python-package/lightgbm/dask.py:636: error: Signature of "predict" incompatible with supertype "LGBMModel"
python-package/lightgbm/dask.py:654: error: Signature of "predict_proba" incompatible with supertype "LGBMClassifier"
python-package/lightgbm/dask.py:738: error: Item "None" of "Optional[str]" has no attribute "partition"
python-package/lightgbm/dask.py:753: error: Signature of "fit" incompatible with supertype "LGBMRegressor"
python-package/lightgbm/dask.py:753: error: Signature of "fit" incompatible with supertype "LGBMModel"
python-package/lightgbm/dask.py:761: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMRegressor")
python-package/lightgbm/dask.py:787: error: Signature of "predict" incompatible with supertype "LGBMModel"
python-package/lightgbm/dask.py:870: error: Item "None" of "Optional[str]" has no attribute "partition"
python-package/lightgbm/dask.py:885: error: Signature of "fit" incompatible with supertype "LGBMRanker"
python-package/lightgbm/dask.py:885: error: Signature of "fit" incompatible with supertype "LGBMModel"
python-package/lightgbm/dask.py:898: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMRanker")
python-package/lightgbm/dask.py:928: error: Signature of "predict" incompatible with supertype "LGBMModel"

Copy link
Contributor Author

@tara-jawahar tara-jawahar Feb 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the issues I was ignoring:

Screen Shot 2021-02-14 at 5 06 06 PM

for this block of code:

Screen Shot 2021-02-14 at 5 07 50 PM

Let me know if that's incorrect and I can take it out!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha oh! I was looking the Dask module but clearly these changes and the warning come from sklearn.py. My mistake, sorry. Thanks for the explanation!

tara-jawahar marked this conversation as resolved.
Show resolved Hide resolved
fit.__doc__ = (_base_doc[:_base_doc.find('eval_group :')]
+ _base_doc[_base_doc.find('eval_metric :'):])

Expand Down Expand Up @@ -989,8 +989,8 @@ def fit(self, X, y,
return self

_base_doc = LGBMModel.fit.__doc__
fit.__doc__ = (_base_doc[:_base_doc.find('eval_class_weight :')]
+ _base_doc[_base_doc.find('eval_init_score :'):])
fit.__doc__ = (_base_doc[:_base_doc.find('eval_class_weight :')] # type: ignore
+ _base_doc[_base_doc.find('eval_init_score :'):]) # type: ignore
tara-jawahar marked this conversation as resolved.
Show resolved Hide resolved
_base_doc = fit.__doc__
_before_early_stop, _early_stop, _after_early_stop = _base_doc.partition('early_stopping_rounds :')
fit.__doc__ = (_before_early_stop
Expand Down
2 changes: 1 addition & 1 deletion python-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def run(self):
if os.path.isfile(os.path.join(CURRENT_DIR, os.path.pardir, 'VERSION.txt')):
copy_file(os.path.join(CURRENT_DIR, os.path.pardir, 'VERSION.txt'),
os.path.join(CURRENT_DIR, 'lightgbm', 'VERSION.txt'),
verbose=0)
verbose=0) # type:ignore
version = open(os.path.join(CURRENT_DIR, 'lightgbm', 'VERSION.txt'), encoding='utf-8').read().strip()
readme = open(os.path.join(CURRENT_DIR, 'README.rst'), encoding='utf-8').read()

Expand Down