Skip to content

[python-package] use dataclass for CallbackEnv - #6048

Merged
jameslamb merged 4 commits into
masterfrom
python/dataclasses
Aug 21, 2023
Merged

jameslamb merged 4 commits into
masterfrom
python/dataclasses

Conversation

@jameslamb

Copy link
Copy Markdown
Member

Contributes #3756.
Contributes to #3867.

Proposes converting CallbackEnv in the Python package to a dataclass.

Also proposes removing uses of import collections in favor of importing only exactly what LightGBM needs from that module.

Restating the benefits I mentioned when I last tried this in #5765, dataclasses are preferable to namedtuple in the following ways:

  • easier to set type annotations + default values than named tuples
    • makes it easier for both human developers and and tools like mypy to understand the expected types of properties
  • possible to attach other methods or computed properties with @property (just like normal classes)

I realized today that "use dataclassses" and "drop Python 3.6 support" don't have to be coupled... I was wrong to suggest that coupling in #5765 and #6022 (comment).

This proposes taking on the dataclasses package as a required dependency only for Python 3.6. For details on that mechanism, see PEP 508 and PEP 631:

That package still exists on PyPI exactly for cases like this... it's intended to be used to continue to provide dataclasses support for Python 3.6: https://pypi.org/project/dataclasses/.

@jameslamb

Copy link
Copy Markdown
Member Author

Thanks @jmoralez ! Sorry I didn't think of this back in #5765.

@jameslamb
jameslamb merged commit 4ea170f into master Aug 21, 2023
@jameslamb
jameslamb deleted the python/dataclasses branch August 21, 2023 17:05
@jmoralez

Copy link
Copy Markdown
Collaborator

No worries. I forgot about that pip package as well haha

@github-actions

Copy link
Copy Markdown
Contributor

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 Nov 22, 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