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] add more type hints in basic.py #5255

Merged
merged 4 commits into from
Jun 5, 2022
Merged

[python] add more type hints in basic.py #5255

merged 4 commits into from
Jun 5, 2022

Conversation

jameslamb
Copy link
Collaborator

@jameslamb jameslamb commented May 31, 2022

Contributes to #3756.

Proposes adding a few more hints in basic.py. Just quickly scanned through this file tonight and chose a few hints that looked easy to determine just from looking at the code and docstrings, trying to slowly make progress on #3756 as I have time.

@@ -3953,7 +3957,7 @@ def __get_eval_info(self):
name.startswith(('auc', 'ndcg@', 'map@', 'average_precision')) for name in self.__name_inner_eval
]

def attr(self, key):
def attr(self, key: str) -> Any:
Copy link
Collaborator

@StrikerRUS StrikerRUS Jun 5, 2022

Choose a reason for hiding this comment

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

Suggested change
def attr(self, key: str) -> Any:
def attr(self, key: str) -> Optional[str]:

if value is not None:
if not isinstance(value, str):
raise ValueError("Only string values are accepted")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh interesting, I didn't realize this could only be set by Booster.set_attr().

Can you help me understand the purpose of these methods? It seems they were added back in 2016 (eba6d20#diff-5e0fb2a7b1ec4988ccde5e74a6c4b609841c1143f281da2afc85e8c464d37b3b) and today are only used in one place, a test.

git grep -E '\.attr\('

assert gbm.booster_.attr('attr_set_inside_callback') == '40'

git grep -E '\.set_attr\('

env.model.set_attr(attr_set_inside_callback=str(env.iteration * 10))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree either way about the type hint so I don't think this conversation needs to block this PR. But I'm wondering if there's an opportunity here to remove these methods and the internal object Booster.__attr.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you help me understand the purpose of these methods?

I'm sorry, I can't.

I guess it's a legacy code that was initially borrowed from XGBoost. In XGBoost those methods are used for distributed training with Rabit: dmlc/xgboost@ecb3a27.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have no objection to remove set_attr() and attr() methods in a separate PR to make LightGBM codebase clearer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok thanks! I'll make a separate PR that proposes removing them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

created #5272

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

LGTM!

@jameslamb jameslamb merged commit 1dc7a29 into master Jun 5, 2022
@jameslamb jameslamb deleted the type-hints branch June 5, 2022 22:32
@jameslamb jameslamb mentioned this pull request Oct 7, 2022
40 tasks
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants