Skip to content

[MNT] Remove mutable objects from defaults#1699

Merged
fkiraly merged 20 commits intosktime:mainfrom
eugenio-mercuriali:feature/issue-1668
Nov 9, 2024
Merged

[MNT] Remove mutable objects from defaults#1699
fkiraly merged 20 commits intosktime:mainfrom
eugenio-mercuriali:feature/issue-1668

Conversation

@eugenio-mercuriali
Copy link
Contributor

@eugenio-mercuriali eugenio-mercuriali commented Oct 20, 2024

Description

This PR is a fix for #1668. Removes mutable default arguments and replaces them with internal newly initialized mutable defaults.

Checklist

  • Linked issues (if existing)
  • Amended changelog for large changes (and added myself there as contributor)
  • Added/modified tests
  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.
    To run hooks independent of commit, execute pre-commit run --all-files

Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

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

Nice!

I think this is great and can be in a single PR.

Some small issues:

  • the tests are failing due to type annotations, have a look at the logs
  • not a strict requirement, but it might be safer in case of __init__ to write the resolved argument to a parameter self._parametername, to ensure self.parametername is always identical with the __init__ input. This is a "dataclass-like" interface which may be useful later in interfacing sklearn and similar libraries.

@eugenio-mercuriali
Copy link
Contributor Author

Nice!

I think this is great and can be in a single PR.

Some small issues:

  • the tests are failing due to type annotations, have a look at the logs
  • not a strict requirement, but it might be safer in case of __init__ to write the resolved argument to a parameter self._parametername, to ensure self.parametername is always identical with the __init__ input. This is a "dataclass-like" interface which may be useful later in interfacing sklearn and similar libraries.

I see, thanks for the feedback!
Added some changes - how does this look?

Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

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

Looks great!

Though, for the __init__-s, you would still do self.paramname = paramname, just not change it, e.g.,

self.groups = groups
self._groups = do_sth_with_groups(groups)

so the class behaves like a dataclass with respect to its init parameters.

There were also a lot of linting failures related to the introduction of stricter requirements in ruff, I fixed most of them. The remaining ones seem genuine code issues, could you have ea look?

@codecov
Copy link

codecov bot commented Oct 22, 2024

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@eugenio-mercuriali
Copy link
Contributor Author

Looks great!

Though, for the __init__-s, you would still do self.paramname = paramname, just not change it, e.g.,

self.groups = groups
self._groups = do_sth_with_groups(groups)

so the class behaves like a dataclass with respect to its init parameters.

There were also a lot of linting failures related to the introduction of stricter requirements in ruff, I fixed most of them. The remaining ones seem genuine code issues, could you have ea look?

Just for me to understand, let's take the __init__ of encoders.py as an example:
if instead of:

self._groups = list(groups) if groups is not None else []

we do:

self.groups = groups
self._groups = list(groups) if groups is not None else []

then we would have to refactor usages of self.groups to self._groups in order to use the resolved argument within the class (+ no need to create a groups property) - is this what you suggest?

@fkiraly
Copy link
Collaborator

fkiraly commented Oct 25, 2024

is this what you suggest?

Yes, exactly what I mean.
I confirm your understanding agrees with mine, @eugenio-mercuriali.

The high-level rationale is that the class then behaves, at all times of usage, like a dataclass with respect to its init parameters.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@eugenio-mercuriali
Copy link
Contributor Author

is this what you suggest?

Yes, exactly what I mean. I confirm your understanding agrees with mine, @eugenio-mercuriali.

The high-level rationale is that the class then behaves, at all times of usage, like a dataclass with respect to its init parameters.

@fkiraly added changes based on the above. Could you take a look?

@fkiraly fkiraly added the maintenance Continuous integration, unit testing & package distribution label Oct 26, 2024
@eugenio-mercuriali
Copy link
Contributor Author

@fkiraly are there any adjustments needed or good to merge?

Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Sorry, I thought I had replied.

@fkiraly fkiraly merged commit 193f539 into sktime:main Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Continuous integration, unit testing & package distribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants