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 files style cleanup #281

Merged
merged 4 commits into from
Oct 20, 2022
Merged

Conversation

redphix
Copy link
Contributor

@redphix redphix commented Oct 12, 2022

What does this PR do?

Style clean up using black, isort and flake8

This PR touches only /api/*py files. Not including the sub directories (eg /api/lightning, etc). Will be appropriate to take in another PR.
I have isolated the changes logically into different commits to make it easier to review by commit.
@Reckless-Satoshi Let me know what you think. We can use this to discuss your gripes with black and try to configure it accordingly.

Here's a list of PEP8 linting "errors" that were fixed using flake8:

@redphix redphix changed the title Api python files style cleanup python files style cleanup Oct 12, 2022
@redphix redphix changed the title python files style cleanup Python files style cleanup Oct 12, 2022
@Reckless-Satoshi
Copy link
Collaborator

Reckless-Satoshi commented Oct 14, 2022

Thanks a lot for checking over these basics. Is this PR ready for review?

We might also want to add a lint / format workflow to check over each of our PRs as a Github Action. I believe it can be done by editing https://github.com/Reckless-Satoshi/robosats/blob/main/.github/workflows/linter.yml . The wearerequired/lint-action@v2 action supports black and flake8 .

      - name: Run linters
        uses: wearerequired/lint-action@v2
        with:
          black: true
          black_dir: api
          flake8: true
          flak8_dir: api

@redphix
Copy link
Contributor Author

redphix commented Oct 18, 2022

@Reckless-Satoshi

Is this PR ready for review?

Yup, mostly. Just fixing the conflicts and adding the linting action should make it complete. But again, just touched the api/*.py files. You think I should make it complete with running and fixing all the *py files?

@Reckless-Satoshi
Copy link
Collaborator

Reckless-Satoshi commented Oct 18, 2022

@redphix

What if in this PR we only merge the changes to .flake, pyproject.toml, requirements.txt and add the GH workflow and instructions on how to run black in setup.py .

We could also edit .github/pull_request_template.md and add - [ ] If it's a backend feature, I have ran ...

Next to merging it, I open another PR where I run black for the first time over all of the *py . That will make reviewing easier (not needed), as otherwise it could be a hazard to merge a PR where some many lines change at once (can't really check over every single change).

@redphix
Copy link
Contributor Author

redphix commented Oct 18, 2022

@redphix

What if in this PR we only merge the changes to .flake, pyproject.toml, requirements.txt and add the GH workflow and instructions on how to run black in setup.py .

We could also edit .github/pull_request_template.md and add - [ ] If it's a backend feature, I have ran ...

Next to merging it, I open another PR where I run black for the first time over all of the *py . That will make reviewing easier (not needed), as otherwise it could be a hazard to merge a PR where some many lines change at once (can't really check over every single change).

Sounds good. Also I suggest using https://pre-commit.com/ for managing pre-commit git hooks and running things like pretier, flake, black and also maybe running the command to generate the oas schemas automatically as json/yaml and placing them where required, every time someone commits. But this requires the contributors to install and configure pre-commit python package on their systems.

Also I have heard real good things about deepsource.io. I wanted to give it a try. It can run those linters and also make commits to fix those linting errors automatically on each PR (will cover people who don't configure pre-commit hooks)

@redphix
Copy link
Contributor Author

redphix commented Oct 18, 2022

I'll try including a pre-commit config here as well if you agree on including it...

@Reckless-Satoshi
Copy link
Collaborator

Sounds good. Also I suggest using https://pre-commit.com/ for managing pre-commit git hooks

That sounds good!

Before going into deepsource.io for fixing PR lint errors, we should try to do it with our workflow .github/workflows/linter.yml . I believe it can also be easily configured to run the linters and fix errors on PRs in addition to simply checking.

@redphix
Copy link
Contributor Author

redphix commented Oct 19, 2022

I believe it can also be easily configured to run the linters and fix errors on PRs in addition to simply checking.

Yup it can with linters that support it. Flake8 for instance, doesn't support fixing the issues, but just points it out. There's autopep8 for that I guess. But I don't want to include that, since it's just too much (atleast for now). Another thing to note is that any linting solution (including deepsource) can't actually auto-fix on forked repos, but it should be able to report it? Not sure...will have to test it.

Also added pyproject.toml for isort config, but later can be used to
configure various other python tools
@redphix redphix force-pushed the api-style-cleanup branch 6 times, most recently from 02a69e3 to bff3929 Compare October 19, 2022 17:24
@redphix
Copy link
Contributor Author

redphix commented Oct 19, 2022

Okay, so turns out we can't use either auto-fix or commit annotations using linters. I was hoping the actions ran on each PR touching py files and atleast report the linting errors, so that it can be fixed, but even doing that is tricky.

From the readme of wearerequired/lint-action

Limitations

Pull requests

There are currently some limitations as to how this action (or any other action) can be used in the context of pull_request events from forks:

  • The action doesn't have permission to push auto-fix changes to the fork. This is because the pull_request event runs on the upstream repo, where the github_token is lacking permissions for the fork. Source
  • The action doesn't have permission to create annotations for commits on forks unless you use the pull_request_target event. You can modify the default permissions granted to the GITHUB_TOKEN by using the permissions key and set the checks scope to write. See GitHub documentation for more.

Even though pull_request_target fix is mentioned to atleast be able to have commit annotations, it doesn't seem to work for me. Maybe it doesn't work on a PR and has to be merged first? not sure, but all in all, it doesn't seem like we can have this for forked repos.

I am leaving this with the auto_fix option as true and the job will fail because it won't have permission to commit to my fork, but you can run it as you mention earlier by opening a PR yourself

@redphix
Copy link
Contributor Author

redphix commented Oct 19, 2022

Seems like incorporating pre-commit in our dev workflow would be the logical step to enforce the style guides and lint fixes. Will write one up tomorrow, need your inputs for ts/js files. We can still have the above workflows, but only for pushes to main or when you open a PR to main?

@redphix redphix marked this pull request as ready for review October 19, 2022 17:45
@Reckless-Satoshi
Copy link
Collaborator

Reckless-Satoshi commented Oct 19, 2022

Even though pull_request_target fix is mentioned to atleast be able to have commit annotations, it doesn't seem to work for me. Maybe it doesn't work on a PR and has to be merged first? not sure, but all in all, it doesn't seem like we can have this for forked repos.

Do we at least get commit annotations if we relax the GITHUB_TOKEN permissions?
E.g. add permissions key before the jobs and use the pull_request_target event.

permissions:
  checks: write

- name: Run linters
uses: wearerequired/lint-action@v2
with:
auto_fix: true
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this action will always fail on PRs if auto_fix is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, I mentioned it above. It will fail because it won't be able to push commits to forks.

@redphix
Copy link
Contributor Author

redphix commented Oct 20, 2022

Even though pull_request_target fix is mentioned to atleast be able to have commit annotations, it doesn't seem to work for me. Maybe it doesn't work on a PR and has to be merged first? not sure, but all in all, it doesn't seem like we can have this for forked repos.

Do we at least get commit annotations if we relax the GITHUB_TOKEN permissions? E.g. add permissions key before the jobs and use the pull_request_target event.

permissions:
  checks: write

Tried that, didn't work. I am not sure why. Maybe you have to merge this PR first and then test it?

@Reckless-Satoshi Reckless-Satoshi merged commit c32c07e into RoboSats:main Oct 20, 2022
@redphix redphix mentioned this pull request Oct 20, 2022
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants