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 Flake8 linter workflow to replace stickler-ci #1786

Merged
merged 30 commits into from Jul 31, 2023

Conversation

reepoi
Copy link
Contributor

@reepoi reepoi commented Jun 26, 2023

Adds GitHub workflow to replace stickler-ci for Python code linting with flake8. The new workflow annotates the PR changes using the errors outputted by flake8. Here is an example showing what the annotations look like: https://github.com/reepoi/pvlib-python/pull/1/files

The workflow implementation is based on the GitHub action here: https://github.com/pr-annotators/flake8-pr-annotator.

@reepoi
Copy link
Contributor Author

reepoi commented Jun 26, 2023

Stickler CI appears to only lint the functions changed inside a file, but this workflow lints the entire file. I am working on changing it to only add annotations for changed functions.

@echedey-ls
Copy link
Contributor

Personally, I don't like excluding errors or warnings for all files, especially W504.

For reference, these are currently excluded in this PR:

  • E201, Whitespace after '('
  • E226, Missing whitespace around arithmetic operator
  • E241, Multiple spaces after ','
  • W503, Line break occurred before a binary operator
  • W504, Line break occurred after a binary operator

flake8 does not provide a way to disable errors for code blocks, but per file with a configuration file (or per line as we know).
I'm not suggesting changing current behaviour, I only want to point that out.

@kandersolar kandersolar added the build tools Pipelines, CI, GH actions label Jul 6, 2023
@kandersolar kandersolar added this to the v0.10.2 milestone Jul 6, 2023
Copy link
Member

@kandersolar kandersolar left a comment

Choose a reason for hiding this comment

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

This PR looks great! I didn't know (or maybe forgot) that jobs could create annotations like this. Very cool. Seems to work as intended.

One thing to note is that this job flags all errors in the changed files, whereas I think (although my memory is a bit hazy) our former setup with stickler-ci wouldn't flag unchanged lines. The old behavior had pros and cons: it wouldn't pester the contributor with errors unrelated to their PR, but it also wouldn't necessarily catch things like unused imports. In our case, flagging all errors means making a tiny change to some files will result in dozens of errors. See for example: https://github.com/pvlib/pvlib-python/actions/runs/5476853711/jobs/9975004553?pr=1786

Is there some way to not report those unrelated errors? Unless we go through and fix all of those (which I'm not sure is a good idea), I think they will make this CI job a lot less useful for many PRs.

Something else is that there is a maximum of 10 annotations. Not really a big deal. You can always look at the job text output to see the full list of lint errors.

For reference, these are currently excluded in this PR

I think @reepoi just copied those exclusions over from our current lint configuration in .stickler.yml. We can certainly talk about changing those rules, but let's focus on just getting the linter running again in this PR.

@wholmgren
Copy link
Member

do we want to use an older version of flake8 that supports the deprecated --diff option?

@reepoi
Copy link
Contributor Author

reepoi commented Jul 12, 2023

Using an older version of flake8 that supports the --diff option is a good idea, and I changed the workflow to do that. If we didn't, we'd need to find the changed Python files and filter their linting errors by line number. The easiest I found to get the line numbers of changes in a file is by parsing the output of git diff, which is what the older versions of flake8 do.

As a bonus, flake8's --diff option appears to ignore changes in files that are not Python files automatically. The workflow's code to do that is removed now.

Copy link
Member

@kandersolar kandersolar left a comment

Choose a reason for hiding this comment

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

LGTM! Does anyone else have any comments before we merge this?

@reepoi can you make a short 0.10.2 whatsnew entry and list yourself as a contributor? And remove the demonstration lint error of course :)

Copy link
Member

@cwhanse cwhanse left a comment

Choose a reason for hiding this comment

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

My only and entirely optional comment is whether we want to continue ignoring E226, Missing whitespace around arithmetic operator. I find the whitespace makes equations easier to read.

@kandersolar
Copy link
Member

I think whitespace is often but not always an improvement. For example, I prefer no whitespace around the ** operators here:

tground = (tinoct**4 - backrat * (tinoct**4 - 293.15**4))**0.25

I lean towards continuing to ignore that linter rule, but I don't feel strongly about it.

@echedey-ls
Copy link
Contributor

@kandersolar @cwhanse Idk if Kevin's point is only for exponentiation operator, but E226 does not raise warnings for operator **. I've also tested it to be sure with latest flake8.

Just wanted to point that out, with reviewers doing their job there shouldn't be any unreadable code pushed.

@kandersolar
Copy link
Member

Thanks @echedey-ls, that's good to know! The ** operator was the first that came to mind, but I expect there are other cases where we'd like to not be hassled about operator whitespace too.

Anyway I'm going to go ahead and merge this so that we have an operational linter again. Thanks @reepoi for setting this up and everyone else for reviewing!

@kandersolar kandersolar merged commit 23453b4 into pvlib:main Jul 31, 2023
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build tools Pipelines, CI, GH actions development_workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stickler-ci is shutting down Add lint checks to CI (without Stickler CI)
5 participants