8000 20 - Added pre-commit + fixing all black and isort issues · pylint-dev/pylint-plugin-utils@17a6476 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17a6476

Browse files
committed
20 - Added pre-commit + fixing all black and isort issues
- Better coverage configuration
1 parent 3a4ea4e commit 17a6476

14 files changed

+176
-71
lines changed

.coveragerc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
[paths]
2+
source =
3+
pylint_plugin_utils
14

2-
[run]
3-
source=pylint_plugin_utils
5+
[report]
6+
include =
7+
pylint_plugin_utils/*
8+
omit =
< 8000 /td>9+
*/test/*
10+
exclude_lines =
11+
# Re-enable default pragma
12+
pragma: no cover
13+
14+
# Debug-only code
15+
def __repr__
16+
17+
# Type checking code not executed during pytest runs
18+
if TYPE_CHECKING:
19+
@overload

.flake8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[flake8]
2+
ignore =
3+
E203, W503, # Incompatible with black see https://github.com/ambv/black/issues/315
4+
5+
max-line-length=88
6+
max-complexity=39

.gitignore

Expand all lines: .gitignore
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pip-log.txt
2626
.tox
2727
nosetests.xml
2828
.pytest_cache
29+
.benchmarks
30+
htmlcov
2931

3032
# Translations
3133
*.mo
@@ -35,4 +37,4 @@ nosetests.xml
3537
.project
3638
.pydevproject
3739
.pylint-plugin-utils
38-
.idea
40+
.idea

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.1.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- repo: https://github.com/PyCQA/isort
8+
rev: 5.10.1
9+
hooks:
10+
- id: isort
11+
- repo: https://github.com/psf/black
12+
rev: 21.12b0
13+
hooks:
14+
- id: black
15+
args: [--safe, --quiet]
16+
- repo: https://github.com/Pierre-Sassoulas/black-disable-checker/
17+
rev: 1.0.1
18+
hooks:
19+
- id: black-disable-checker
20+
- repo: https://github.com/PyCQA/flake8
21+
rev: 4.0.1
22+
hooks:
23+
- id: flake8
24+
additional_dependencies: [flake8-typing-imports==1.10.1]

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@
1111
Utilities and helpers for writing Pylint plugins. This is not a direct Pylint plugin, but rather a set of tools and functions used by other plugins such as [pylint-django](https://github.com/PyCQA/pylint-django) and [pylint-celery](https://github.com/PyCQA/pylint-celery).
1212

1313
# Testing
14+
Create virtualenv:
15+
```bash
16+
python3.8 -m venv .pylint-plugin-utils
17+
source .pylint-plugin-utils/bin/activate
18+
pip install --upgrade pip setuptools
19+
```
20+
1421
We use [tox](https://tox.readthedocs.io/en/latest/) and [pytest-benchmark](https://pytest-benchmark.readthedocs.io/en/latest/index.html) for running the test suite. You should be able to install it with:
1522
```bash
1623
pip install tox pytest pytest-benchmark
1724
```
1825

1926
To run the test suite for a particular Python version, you can do:
2027
```bash
21-
tox -e py37
28+
tox -e py38
2229
```
2330

2431
To run individual tests with ``tox``, you can do::
2532
```bash
26-
tox -e py37 -- -k name_of_the_test
33+
tox -e py38 -- -k name_of_the_test
2734
```
2835

2936
We use pytest_ for testing ``pylint``, which you can use without using ``tox`` for a faster development cycle.

pylint_plugin_utils/__init__.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55

66

77
def get_class(module_name, kls):
8-
parts = kls.split('.')
8+
parts = kls.split(".")
99
m = __import__(module_name)
10-
for mp in module_name.split('.')[1:]:
10+
for mp in module_name.split(".")[1:]:
1111
m = getattr(m, mp)
1212
klass = getattr(m, parts[0])
1313
return klass
1414

1515

1616
class NoSuchChecker(Exception):
17-
1817
def __init__(self, checker_class):
1918
self.message = "Checker class %s was not found" % checker_class
2019

@@ -32,18 +31,21 @@ def get_checker(linter: PyLinter, checker_class):
3231
def augment_visit(linter: PyLinter, checker_method, augmentation):
3332
"""
3433
Augmenting a visit enables additional errors to be raised (although that case is
35-
better served using a new checker) or to suppress all warnings in certain circumstances.
34+
better served using a new checker) or to suppress all warnings in certain
35+
circumstances.
3636
37-
Augmenting functions should accept a 'chain' function, which runs the checker method
38-
and possibly any other augmentations, and secondly an Astroid node. "chain()" can be
39-
called at any point to trigger the continuation of other checks, or not at all to
40-
prevent any further checking.
37+
Augmenting functions should accept a 'chain' function, which runs the checker
38+
method and possibly any other augmentations, and secondly an Astroid node.
39+
"chain()" can be called at any point to trigger the continuation of other
40+
checks, or not at all to prevent any further checking.
4141
"""
4242

4343
try:
4444
checker = get_checker(linter, checker_method.__self__.__class__)
4545
except AttributeError:
46-
checker = get_checker(linter, get_class(checker_method.__module__, checker_method.__qualname__))
46+
checker = get_checker(
47+
linter, get_class(checker_method.__module__, checker_method.__qualname__)
48+
)
4749

4850
old_method = getattr(checker, checker_method.__name__)
4951
setattr(checker, checker_method.__name__, AugmentFunc(old_method, augmentation))
@@ -68,7 +70,6 @@ def __call__(self):
6870

6971

7072
class Suppress:
71-
7273
def __init__(self, linter):
7374
self._linter = linter
7475
self._suppress = []
@@ -89,13 +90,6 @@ def suppress(self, *symbols):
8990
def __exit__(self, exc_type, exc_val, exc_tb):
9091
self._linter.add_message = self._orig_add_message
9192
for to_append_args, to_append_kwargs in self._messages_to_append:
92-
# Depending on the Pylint version, the add_message API is different.
93-
# Either a single object called 'message' is passed, or the first argument
94-
# is a message symbol.
95-
if hasattr('symbol', to_append_args[0]):
96-
code = to_append_args[0].symbol
97-
else:
98-
code = to_append_args[0]
9993
if to_append_args[0] in self._suppress:
10094
continue
10195
self._linter.add_message(*to_append_args, **to_append_kwargs)
@@ -107,7 +101,9 @@ def suppress_message(linter: PyLinter, checker_method, message_id_or_symbol, tes
107101
returns True. It is useful to prevent one particular message from being raised
108102
in one particular case, while leaving the rest of the messages intact.
109103
"""
110-
augment_visit(linter, checker_method, DoSuppress(linter, message_id_or_symbol, test_func))
104+
augment_visit(
105+
linter, checker_method, DoSuppress(linter, message_id_or_symbol, test_func)
106+
)
111107

112108

113109
class DoSuppress:
@@ -124,40 +120,49 @@ def __call__(self, chain, node):
124120

125121
@property
126122
def symbols(self) -> List:
127-
# At some point, pylint started preferring message symbols to message IDs. However this is not done
128-
# consistently or uniformly - occasionally there are some message IDs with no matching symbols.
129-
# We try to work around this here by suppressing both the ID and the symbol, if we can find it.
123+
# At some point, pylint started preferring message symbols to message IDs.
124+
# However, this is not done consistently or uniformly
125+
# - occasionally there are some message IDs with no matching symbols.
126+
# We try to work around this here by suppressing both the ID and the symbol.
130127
# This also gives us compatability with a broader range of pylint versions.
131128

132-
# Similarly, a commit between version 1.2 and 1.3 changed where the messages are stored - see:
133-
# https://bitbucket.org/logilab/pylint/commits/0b67f42799bed08aebb47babdc9fb0e761efc4ff#chg-reporters/__init__.py
134-
# Therefore here, we try the new attribute name, and fall back to the old version for
135-
# compatability with <=1.2 and >=1.3
129+
# Similarly, between version 1.2 and 1.3 changed where the messages are stored
130+
# - see:
131+
# https://bitbucket.org/logilab/pylint/commits/0b67f42799bed08aebb47babdc9fb0e761efc4ff#chg-reporters/__init__.py
132+
# Therefore here, we try the new attribute name, and fall back to the old
133+
# version for compatability with <=1.2 and >=1.3
136134

137135
try:
138136
pylint_messages = self.get_message_definitions(self.message_id_or_symbol)
139-
the_symbols = [symbol
140-
for pylint_message in pylint_messages
141-
for symbol in (pylint_message.msgid, pylint_message.symbol)
142-
if symbol is not None]
137+
the_symbols = [
138+
symbol
139+
for pylint_message in pylint_messages
140+
for symbol in (pylint_message.msgid, pylint_message.symbol)
141+
if symbol is not None
142+
]
143143
except UnknownMessageError:
144-
# This can happen due to mismatches of pylint versions and plugin expectations of available messages
144+
# This can happen due to mismatches of pylint versions and plugin
145+
# expectations of available messages
145146
the_symbols = [self.message_id_or_symbol]
146147

147148
return the_symbols
148149

149150
def get_message_definitions(self, message_id_or_symbol):
150-
msgs_store = getattr(self.linter, 'msgs_store', self.linter)
151+
msgs_store = getattr(self.linter, "msgs_store", self.linter)
151152

152-
if hasattr(msgs_store, 'check_message_id'):
153+
if hasattr(msgs_store, "check_message_id"):
153154
return [msgs_store.check_message_id(message_id_or_symbol)]
154155
# pylint 2.0 renamed check_message_id to get_message_definition in:
155156
# https://github.com/PyCQA/pylint/commit/5ccbf9eaa54c0c302c9180bdfb745566c16e416d
156-
elif hasattr(msgs_store, 'get_message_definition'):
157+
elif hasattr(msgs_store, "get_message_definition"):
157158
return [msgs_store.get_message_definition(message_id_or_symbol)]
158159
# pylint 2.3.0 renamed get_message_definition to get_message_definitions in:
159160
# https://github.com/PyCQA/pylint/commit/da67a9da682e51844fbc674229ff6619eb9c816a
160-
elif hasattr(msgs_store, 'get_message_definitions'):
161+
elif hasattr(msgs_store, "get_message_definitions"):
161162
return msgs_store.get_message_definitions(message_id_or_symbol)
162163
else:
163-
raise ValueError('pylint.utils.MessagesStore does not have a get_message_definition(s) method')
164+
msg = (
165+
"pylint.utils.MessagesStore does not have a "
166+
"get_message_definition(s) method"
167+
)
168+
raise ValueError(msg)

requirements_test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-r requirements_test_pre_commit.txt
2+
-r requirements_test_min.txt
3+
pre-commit~=2.16
4+
pytest-cov~=3.0

requirements_test_min.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-e .
2+
pytest~=6.2

requirements_test_pre_commit.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Everything in this file should reflect the pre-commit configuration
2+
# in .pre-commit-config.yaml
3+
black==21.12b0
4+
flake8==4.0.1
5+
flake8-typing-imports==1.12.0
6+
isort==5.10.1

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ testpaths = tests
66
python_files = *test_*.py
77

88
[isort]
9-
profile = black
9+
profile = black

0 commit comments

Comments
 (0)
0