Automatic Python Code Format with GitHub Actions

This post introduces a method for beautifying Python Code with GitHub Actions. The method utilizes three tools: autopep8, Black, isort. In addition, a Pull Request, which contains formatted codes is created by GitHub Actions.

The YAML file shows the c…


This content originally appeared on DEV Community and was authored by Tomoyuki KOYAMA

This post introduces a method for beautifying Python Code with GitHub Actions. The method utilizes three tools: autopep8, Black, isort. In addition, a Pull Request, which contains formatted codes is created by GitHub Actions.

The YAML file shows the configuration of the automatic code formatter.

name: Format Python Code
on: push
jobs:
  python-code-format:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
          architecture: "x64"
      - name: Display Python version
        run: python --version
      - name: Install packages
        run: pip install black autopep8 isort
      - name: Formatter
        run: |
          black .
          autopep8 --recursive --in-place --aggressive --aggressive .
          isort .
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
          commit-message: Auto code format
          title: Fixes by format action
          body: This is an auto-generated PR with fixes.
          labels: automated pr
          branch: python-code-format-patches

The summary of this configuration file is shown as follows:

  • Setup Ubuntu 20.04
  • Setup Python 3.10
  • Install Python packages (black, autopep8, isort) by pip
  • Formatting Python Codes
  • Create Pull Request

The CI works as follows:

Image description


This content originally appeared on DEV Community and was authored by Tomoyuki KOYAMA


Print Share Comment Cite Upload Translate Updates
APA

Tomoyuki KOYAMA | Sciencx (2022-06-18T02:28:19+00:00) Automatic Python Code Format with GitHub Actions. Retrieved from https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/

MLA
" » Automatic Python Code Format with GitHub Actions." Tomoyuki KOYAMA | Sciencx - Saturday June 18, 2022, https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/
HARVARD
Tomoyuki KOYAMA | Sciencx Saturday June 18, 2022 » Automatic Python Code Format with GitHub Actions., viewed ,<https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/>
VANCOUVER
Tomoyuki KOYAMA | Sciencx - » Automatic Python Code Format with GitHub Actions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/
CHICAGO
" » Automatic Python Code Format with GitHub Actions." Tomoyuki KOYAMA | Sciencx - Accessed . https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/
IEEE
" » Automatic Python Code Format with GitHub Actions." Tomoyuki KOYAMA | Sciencx [Online]. Available: https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/. [Accessed: ]
rf:citation
» Automatic Python Code Format with GitHub Actions | Tomoyuki KOYAMA | Sciencx | https://www.scien.cx/2022/06/18/automatic-python-code-format-with-github-actions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.