import numpy as npfrom pathlib import Pathdef find_files(file_path: Union[str, Path], file_ext: str) ->list:"""Recursively find files of the stated type along the given file path."""# We have a really long comment on this line just for demonstration purposes so that we can generate a few errors that need lintingtry:returnlist(Path(file_path).rglob(f"*{file_ext}"))except:raise
black sample.pyflake8 sample.pypylint sample.pypytest test_sample.py
Linting manually
black
❱ black sample.pyAll done! ✨ 🍰 ✨1 file changed.
flake8
❱ flake8 sample.pysample.py:1:1: D100 Missing docstring in public modulesample.py:1:1: F401 'numpy as np' imported but unusedsample.py:2:1: F401 'pandas as pd' imported but unusedsample.py:7:36: F821 undefined name 'Union'sample.py:8:80: E501 line too long (87> 79 characters)sample.py:9:80: E501 line too long (135> 79 characters)sample.py:12:5: E722 do not use bare 'except'
pylint
❱ pylint sample.py************* Module samplesample.py:9:0: C0301: Line too long (135/120)(line-too-long)sample.py:1:0: C0114: Missing module docstring (missing-module-docstring)sample.py:7:35: E0602: Undefined variable 'Union'(undefined-variable)sample.py:12:4: W0706: The except handler raises immediately (try-except-raise)sample.py:4:0: C0411: standard import "from pathlib import Path" should be placed before "import numpy as np"(wrong-import-order)sample.py:1:0: W0611: Unused numpy imported as np (unused-import)sample.py:2:0: W0611: Unused pandas imported as pd (unused-import)-------------------------------------Your code has been rated at -10.00/10
pytest
❱ pylint test_sample.py================= test session starts =================platform linux -- Python 3.7.11, pytest-7.1.1, pluggy-1.0.0rootdir: /home/neil/work/projects/pre-commit/assets/pythonplugins: hydra-core-1.2.0, regtest-1.5.0, cov-3.0.0collected 0 items / 1 error======================= ERRORS ========================___________ ERROR collecting test_sample.py ___________test_sample.py:1: in <module>from .sample import find_filessample.py:7: in <module>def find_and_load_files(file_path: Union[str, Path], file_type: str):E NameError: name 'Union' is not defined================ short test summary info ===============ERROR test_sample.py - NameError: name 'Union' is not defined!!!!!!!! Interrupted: 1 error during collection !!!!!!!!=================== 1 error in 0.49s ===================
Supports : C, C++, R, Java, JavaScript, PHP, LISP, Markdown, Go, Bash, Ansible, Docker, Lua, Jupyter Notebooks and more.
What are Hooks?
Actions that are run prior to or in response to a given action.
❱ ls -lha .git/hooksdrwxr-xr-x neil neil 4.0 KB Mon Oct 24 10:26:37 2022 .drwxr-xr-x neil neil 4.0 KB Tue Jan 3 18:48:37 2023 ...rwxr-xr-x neil neil 478 B Sun Aug 14 13:35:27 2022 applypatch-msg.sample.rwxr-xr-x neil neil 896 B Sun Aug 14 13:35:27 2022 commit-msg.sample.rwxr-xr-x neil neil 4.6 KB Sun Aug 14 13:35:27 2022 fsmonitor-watchman.sample.rwxr-xr-x neil neil 189 B Sun Aug 14 13:35:27 2022 post-update.sample.rwxr-xr-x neil neil 424 B Sun Aug 14 13:35:27 2022 pre-applypatch.sample.rwxr-xr-x neil neil 1.6 KB Sun Aug 14 13:35:27 2022 pre-commit.sample.rwxr-xr-x neil neil 416 B Sun Aug 14 13:35:27 2022 pre-merge-commit.sample.rwxr-xr-x neil neil 1.3 KB Sun Aug 14 13:35:27 2022 pre-push.sample.rwxr-xr-x neil neil 4.8 KB Sun Aug 14 13:35:27 2022 pre-rebase.sample.rwxr-xr-x neil neil 544 B Sun Aug 14 13:35:27 2022 pre-receive.sample.rwxr-xr-x neil neil 1.5 KB Sun Aug 14 13:35:27 2022 prepare-commit-msg.sample.rwxr-xr-x neil neil 2.7 KB Sun Aug 14 13:35:27 2022 push-to-checkout.sample.rwxr-xr-x neil neil 3.6 KB Sun Aug 14 13:35:27 2022 update.sample
-repo: https://github.com/pre-commit/pre-commit-hooksrev: v4.3.0 # Use the rev you want to point athooks:-id: trailing-whitespacetypes:[file, text]-id: end-of-file-fixertypes:[file, text]-id: check-docstring-first-id: check-case-conflict-id: check-yaml
❱ pre-commit run --all-files[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.[INFO] Initializing environment for https://github.com/psf/black.[INFO] Initializing environment for https://github.com/pycqa/flake8.git.[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.[INFO] Once installed this environment will be reused.[INFO] This may take a few minutes...[INFO] Installing environment for https://github.com/psf/black.[INFO] Once installed this environment will be reused.[INFO] This may take a few minutes...[INFO] Installing environment for https://github.com/pycqa/flake8.git[INFO] Once installed this environment will be reused.[INFO] This may take a few minutes...Check Yaml.....................................................PassedFix End of Files...............................................PassedCheck for case conflicts.......................................PassedCheck docstring is first.......................................FailedTrim Trailing Whitespace.......................................Failed- hook id: trailing-whitespace- exit code: 1Files were modified by this hook. Additional output:Fixing sample.pyblack..........................................................Failedreformatted sample.pyAll done! ✨ 🍰 ✨1 file reformatted.flake8.........................................................Failed- hook id: flake8- exit code: 1sample.py:1:1: D100 Missing docstring in public modulesample.py:1:1: F401 'numpy as np' imported but unusedsample.py:2:1: F401 'pandas as pd' imported but unusedsample.py:7:36: F821 undefined name 'Union'sample.py:8:80: E501 line too long (87> 79 characters)sample.py:9:80: E501 line too long (135> 79 characters)sample.py:12:5: E722 do not use bare 'except'pylint.........................................................Failed- hook id: pylint- exit code: 1************* Module python.samplesample.py:9:0: C0301: Line too long (135/120)(line-too-long)sample.py:1:0: C0114: Missing module docstring (missing-module-docstring)sample.py:7:35: E0602: Undefined variable 'Union'(undefined-variable)sample.py:12:4: W0706: The except handler raises immediately (try-except-raise)sample.py:4:0: C0411: standard import "from pathlib import Path" should be placed before "import numpy as np"(wrong-import-order)sample.py:1:0: W0611: Unused numpy imported as np (unused-import)sample.py:2:0: W0611: Unused pandas imported as pd (unused-import)-------------------------------------Your code has been rated at -10.00/10pytest.........................................................Failed================= test session starts =================platform linux -- Python 3.7.11, pytest-7.1.1, pluggy-1.0.0rootdir: /home/neil/work/projects/pre-commit/assets/pythonplugins: hydra-core-1.2.0, regtest-1.5.0, cov-3.0.0collected 0 items / 1 error======================= ERRORS ========================___________ ERROR collecting test_sample.py ___________test_sample.py:1: in <module>from .sample import find_filessample.py:7: in <module>def find_and_load_files(file_path: Union[str, Path], file_type: str):E NameError: name 'Union' is not defined================ short test summary info ===============ERROR test_sample.py - NameError: name 'Union' is not defined!!!!!!!! Interrupted: 1 error during collection !!!!!!!!=================== 1 error in 0.49s ===================
import numpy as npfrom pathlib import Pathdef find_files(file_path: Union[str, Path], file_ext: str) -> List:"""Recursively find files of the stated type along the given file path."""# Short commenttry:returnlist(Path(file_path).rglob(f"*{file_ext}"))except:raise
pylint errors
************* Module python.samplesample.py:9:0: C0301: Line too long (135/120)(line-too-long)sample.py:1:0: C0114: Missing module docstring (missing-module-docstring)sample.py:7:35: E0602: Undefined variable 'Union'(undefined-variable)sample.py:12:4: W0706: The except handler raises immediately (try-except-raise)sample.py:4:0: C0411: standard import "from pathlib import Path" should be placed before "import numpy as np"(wrong-import-order)sample.py:1:0: W0611: Unused numpy imported as np (unused-import)-------------------------------------Your code has been rated at -10.00/10
Linted
"""Find files of a given type """from pathlib import Pathfrom typing import Uniondef find_files(file_path: Union[str, Path], file_type: str) ->list:"""Recursively find files of the stated type along the given file path."""returnlist(Path(file_path).rglob(f"*{file_type}"))
Add and commit changes
git add sample.pygit commit -m"Linting sample.py."Check Yaml.....................................................PassedFix End of Files...............................................PassedCheck for case conflicts.......................................PassedCheck docstring is first.......................................PassedTrim Trailing Whitespace.......................................Passedblack..........................................................Passedflake8.........................................................Passedpylint.........................................................Passedpytest.........................................................Passed[INFO] Restored changes from /home/neil/.cache/pre-commit/patch1674045267-394193.[main 05b1568] Linting sample.py1 file changed, 2 insertions(+), 2 deletions(-)
Continuous Integration/Delivery (CI/CD)
Runs hooks on GitHub/GitLab/etc. in response to specific tasks/actions.
Zero configuration, just need .pre-commit-config.yaml.
Corrects & commits some formatting issues automatically without need for developer to reformat.
Only runs on Pull Request commits (not on individual branches)
Automatically updates .pre-commit-config.yaml for you (e.g. new rev).
Free for open source repositories (paid for version for private/organisation repositories).
Configuration (.pre-commit-config.yaml)
ci:autofix_prs:trueautofix_commit_msg:'[pre-commit.ci] Fixing issues with pre-commit'autoupdate_schedule: weeklyautoupdate_commit_msg:'[pre-commit.ci] pre-commit automatically updated.'skip:[pylint, pytest] # Optionally list ids of hooks to skip on CI