Skip to content

[python-package] allow access to eval result attributes by name - #7161

Merged
jameslamb merged 16 commits into
mainfrom
python/more-eval-tuples
Jul 18, 2026
Merged

jameslamb merged 16 commits into
mainfrom
python/more-eval-tuples

Conversation

@jameslamb

@jameslamb jameslamb commented Feb 22, 2026 •

Copy link
Copy Markdown
Member

Closes #6748

Wraps evaluation result tuples in the Python package in a typing.NamedTuple, to allow named-attribute access to eval result tuples.

Notes for Reviewers

Benefits of this change

  • simpler, less error-prone code
  • improves readability
  • improves the ability of type-checkers like mypy to catch bugs
  • eliminates awkward workarounds for different-sized tuples with and without a standard deviation (only added by cv())
  • tab completion when modifying the code

And provides all of these benefits for code that uses lightgbm, especially code that touches evaluation results like optuna.

Is this safe?

I think so. Any existing code treating these values (like the output of Booster.eval_valid()) as Python tuples should continue to work exactly as they always have.

As more evidence of this, this PR is passing CI without any changes to existing unit tests.

References

See https://snarky.ca/dont-use-named-tuples-in-new-apis/

@jameslamb jameslamb changed the title WIP: [python-package] allow access to eval result attributes by name [python-package] allow access to eval result attributes by name Jul 12, 2026
]


class EvalResult(NamedTuple):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You know I'm usually very cautious about expanding the public API of the project... I think it is worth it for this case. This would allow users to get the full benefits of this change and eliminate positional references and tuple unpacking in their own code.

I'm especially thinking of user code that implements custom early stopping callbacks, and hyperparameter tuning frameworks like hyperopt and optuna.

@jameslamb

Copy link
Copy Markdown
Member Author

Finally came back and finish this... I think this is ready for review. But no rush... it's ok with me if it doesn't make it into the v4.7.0 release.

@jameslamb
jameslamb marked this pull request as ready for review July 12, 2026 05:45

@borchero borchero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool, this will make code much more readable :)

@jameslamb
jameslamb merged commit 497b57f into main Jul 18, 2026
60 checks passed
@jameslamb
jameslamb deleted the python/more-eval-tuples branch July 18, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python-package] factor out positional indexing for eval result tuples

2 participants