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

chore: add pre-commit hook for pylint #1630

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

philippwaller
Copy link

Description

This PR adds a pre-commit hook for pylint to ensure code quality checks are performed automatically before each commit.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests

Checklist

  • The documentation has been adjusted accordingly
  • Tests have been added that prove the fix is effective or that the feature works
  • The changes are documented in the changelog (docs/changelog.md)

Copy link

codecov bot commented Jan 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.92%. Comparing base (16a1023) to head (3ecdd79).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1630   +/-   ##
=======================================
  Coverage   96.92%   96.92%           
=======================================
  Files         160      160           
  Lines       10688    10688           
=======================================
  Hits        10359    10359           
  Misses        329      329           

@farmio
Copy link
Member

farmio commented Jan 6, 2025

This was not part of pre-commit since pylint was uncomfortably slow - and ruff covered most anyway. Is this faster nowadays?

Also, have a look at our tox pylint invocation. We run it with different arguments for tests. Not sure what default jobs is, but setting to 0 should speed things up a little.

xknx/tox.ini

Lines 31 to 32 in 905b246

pylint --jobs=0 xknx examples
pylint --jobs=0 --disable=protected-access,abstract-class-instantiated test

Maybe this can be configured in pyproject.toml for a global pylint invocation.

@philippwaller
Copy link
Author

philippwaller commented Jan 6, 2025

I initially added it because I noticed inconsistencies in the test results when running tox versus executing pylint directly. At first, I suspected this was due to different virtual environments being used, but I’ve now realized the root cause is different:

It turns out that submodules are only recognized if an __init__.py file is present.

When running:
pylint --disable=protected-access,abstract-class-instantiated test

No issues are reported. However, when specifying a file explicitly:

pylint --disable=protected-access,abstract-class-instantiated test test/dpt_tests/payload_test.py

************* Module payload_test
test/dpt_tests/payload_test.py:76:15: C2801: Unnecessarily calls dunder method __repr__. Use repr built-in function. (unnecessary-dunder-call)
test/dpt_tests/payload_test.py:77:15: C2801: Unnecessarily calls dunder method __repr__. Use repr built-in function. (unnecessary-dunder-call)

pre-commit still reports all errors because it runs pylint on each file individually (actually in batches). However, with the latest commit, performance is ok-ish since it only lints the changed files.

I’ve also added the missing __init__.py files to fix the tox workflow.

@farmio
Copy link
Member

farmio commented Jan 7, 2025

https://pylint.pycqa.org/en/latest/user_guide/installation/pre-commit-integration.html explicitly mentions

We however discourage it as pylint -- due to its speed -- is more suited to a continuous integration job or a git pre-push hook

thus I asked.

Further

If only some of the files are given, pylint might miss a particular value's type and produce inferior inference for the subset. Since pre-commit slices the files given to it in order to parallelize the processing, the result can be degraded. It can also be unexpectedly different when the file set changes because the new slicing can change the inference. Thus the require_serial option should be set to true (...)

So should we require_serial: true?

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