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

introduce notebook cleaning and linting scripts with an example #535

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

rfl-urbaniak
Copy link
Collaborator

@rfl-urbaniak rfl-urbaniak commented Apr 4, 2024

This partially resolves #409 .

  • two scripts for cleaning and linting notebooks are introduced
  • Makefile is modified accordingly
  • scripts are used on tutorial_i only for now. To pass lint, all imports are moved to the top of the file, and two function names are modified. Other changes are the result of auto-formatting.
  • nbqa is added to dependencies.

What is not covered in this PR:

  • implementing this for other notebooks
  • introducing a corresponding workflow
    reason: we need to make sure we agree about these methods first

Once we are on the same page about this, I will add the workflow and work on other notebooks one by one, to cover the same notebooks as the ones tested in test_notebooks. This will most likely require manual revisions to notebooks and re-running them completely, so I prefer to do these in separate small PRs.

@rfl-urbaniak rfl-urbaniak added the status:WIP Work-in-progress not yet ready for review label Apr 4, 2024
@rfl-urbaniak rfl-urbaniak added status:awaiting review Awaiting response from reviewer and removed status:WIP Work-in-progress not yet ready for review labels Apr 4, 2024
@rfl-urbaniak rfl-urbaniak requested a review from eb8680 April 4, 2024 12:40
@SamWitty
Copy link
Collaborator

SamWitty commented Apr 4, 2024

This looks good to me. One thing we might want to consider is just having make format and make lint run both regular code and notebook versions of the shell scripts rather than adding a new make command specific to notebooks.

@eb8680 , any thoughts?

@eb8680
Copy link
Contributor

eb8680 commented Apr 4, 2024

To pass lint, all imports are moved to the top of the file, and two function names are modified. Other changes are the result of auto-formatting.

I haven't used nbqa before. Just so I understand, what failure related to the imports wasn't fixed by autoformatting with isort and black? Does this mean running nbqa isort --check <filename> can still fail even if you run nbqa isort <filename> first?

@eb8680
Copy link
Contributor

eb8680 commented Apr 4, 2024

One thing we might want to consider is just having make format and make lint run both regular code and notebook versions of the shell scripts rather than adding a new make command specific to notebooks.

I think it's OK to keep the notebook part separate for now.

@eb8680 eb8680 added the testing label Apr 4, 2024
@rfl-urbaniak
Copy link
Collaborator Author

To pass lint, all imports are moved to the top of the file, and two function names are modified. Other changes are the result of auto-formatting.

I haven't used nbqa before. Just so I understand, what failure related to the imports wasn't fixed by autoformatting with isort and black? Does this mean running nbqa isort --check <filename> can still fail even if you run nbqa isort <filename> first?

I think this is more about flake8 being included in the lint, it sometimes complains about things that make clean doesn't fix, like lines too long or unused imports. I've seen this happen outside of nbqa as well sometimes. In the case of notebooks it also seems that isort sorts imports within cells, but if some imports are not in the top cell, flake8 complains.

Relatedly, I resolved this in the collab repo by adding autoflake --remove-all-unused-imports --in-place ... to the cleaning script. I assume we do not want to remove unused imports in modules as, for example, this is a bit annoying for modules that are WIP, but perhaps could be useful for notebook cleaning, as notebooks will be only added to the list once assumed done?

@SamWitty
Copy link
Collaborator

Relatedly, I resolved this in the collab repo by adding autoflake --remove-all-unused-imports --in-place ... to the cleaning script. I assume we do not want to remove unused imports in modules as, for example, this is a bit annoying for modules that are WIP, but perhaps could be useful for notebook cleaning, as notebooks will be only added to the list once assumed done?

Yes, I think it would be a mistake to have make clean automatically remove unused imports. My preference is that we automate things that developers will almost never care about, like import orders or trailing whitespace, but avoid being too heavy handed beyond that.

Anyway, who would you like to review this @rfl-urbaniak ? I always find it confusing when there are two reviewers listed.

@rfl-urbaniak
Copy link
Collaborator Author

Sorry for being confusing. I wanted @eb8680 to approve the general strategy, as this is what would be applied across all notebooks in the repo, and @SamWitty to approve the resulting changes to the notebook,

@SamWitty
Copy link
Collaborator

Sorry for being confusing. I wanted @eb8680 to approve the general strategy, as this is what would be applied across all notebooks in the repo, and @SamWitty to approve the resulting changes to the notebook,

Ok, thanks for clarifying. The changes to the notebook have my stamp of approval. @eb8680 , ball's in your court to review the broader PR.

@SamWitty SamWitty removed their request for review April 12, 2024 12:26
Copy link
Contributor

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

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

Seems OK to me, thanks for taking this on!

@eb8680 eb8680 merged commit af79c20 into master Apr 12, 2024
7 checks passed
@eb8680 eb8680 deleted the ru-notebook-format-lint branch April 12, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:awaiting review Awaiting response from reviewer testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Notebooks should be linted and typechecked in CI
3 participants