Skip to content

Commit

Permalink
Add compatibility to pylint 2.12 and more
Browse files Browse the repository at this point in the history
  • Loading branch information
louisjdmartin committed Apr 19, 2022
1 parent f476bbd commit 35be209
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.egg-info/
dist/
dist/
__pycache__
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ cnes-pylint-extension checks the following metrics :
- Version 2.0 - compatible pylint 1.6
- Version 3.0 - compatible pylint 1.7.4 and 1.9.1
- Version 4.0 - compatible pylint 2.1.1
- Version 5.0 - compatible pylint 2.5.0
- Version 5.0 - compatible pylint >=2.5.0,<2.12.0
- Version 6.0 - compatible pylint >=2.12,<3.0.0
- **warning**: At 6.0.0 release, latest pylint was 2.13.5. If you encounter issue with pylint>2.13.5 and <3.0.0 please open an issue.

# To use these checkers:

Expand All @@ -37,7 +39,7 @@ cnes-pylint-extension checks the following metrics :

### Install Pylint

`pip install pylint==2.5.0`
`pip install pylint==2.13.5`

### Install CNES Pylint extension checkers

Expand Down
29 changes: 18 additions & 11 deletions checkers/cnes_checker/cnes_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ class CommentMetricsChecker(BaseTokenChecker):
"""

__implements__ = (ITokenChecker, IAstroidChecker)

# Theses values are hardcoded in pylint (and have changed in pylint 2.12)
# We can't get them directly from the pylint lib :(
LINE_TYPE_DOCSTRING = 'docstring'
LINE_TYPE_COMMENT = 'comment'
LINE_TYPE_CODE = 'code'
LINE_TYPE_EMPTY = 'empty'
LINE_TYPES = [LINE_TYPE_DOCSTRING, LINE_TYPE_CODE, LINE_TYPE_COMMENT, LINE_TYPE_EMPTY]

name = 'commentmetrics'
msgs = {'R5201': ('Too few comments (%s/%s %%)',
'too-few-comments',
Expand Down Expand Up @@ -210,8 +219,7 @@ def __init__(self, linter):

def _reset(self):
self._stats = {}
self._global_stats = dict.fromkeys(['docstring_lines', 'comment_lines',
'code_lines', 'empty_lines'], 0)
self._global_stats = dict.fromkeys(self.LINE_TYPES, 0)

def process_tokens(self, tokens):
"""update stats"""
Expand All @@ -235,8 +243,7 @@ def visit_functiondef(self, node):
nb_lines = node.tolineno - node.fromlineno
if nb_lines <= self.config.min_func_size_to_check_comments:
return
func_stats = dict.fromkeys(['docstring_lines', 'comment_lines',
'code_lines', 'empty_lines'],
func_stats = dict.fromkeys(self.LINE_TYPES,
0)
for line in sorted(self._stats):
if line > node.tolineno:
Expand All @@ -247,22 +254,22 @@ def visit_functiondef(self, node):
func_stats[l_type] += (min(node.tolineno,
line + lines_number - 1)
- max(node.fromlineno, line) + 1)
if func_stats['code_lines'] <= 0:
if func_stats[self.LINE_TYPE_CODE] <= 0:
return
ratio = ((func_stats['comment_lines'] + func_stats['docstring_lines'])
/ float(func_stats['code_lines']) * 100)
ratio = ((func_stats[self.LINE_TYPE_COMMENT] + func_stats[self.LINE_TYPE_DOCSTRING])
/ float(func_stats[self.LINE_TYPE_CODE]) * 100)
if ratio < self.config.min_func_comments_ratio:
self.add_message('too-few-comments', node=node,
args=('%.2f' % ratio,
self.config.min_func_comments_ratio))

@check_messages('too-few-comments')
def visit_module(self, node):
if self._global_stats['code_lines'] <= 0:
if self._global_stats[self.LINE_TYPE_CODE] <= 0:
return
ratio = ((self._global_stats['comment_lines'] +
self._global_stats['docstring_lines']) /
float(self._global_stats['code_lines']) * 100)
ratio = ((self._global_stats[self.LINE_TYPE_COMMENT] +
self._global_stats[self.LINE_TYPE_DOCSTRING]) /
float(self._global_stats[self.LINE_TYPE_CODE]) * 100)
if ratio < self.config.min_module_comments_ratio:
self.add_message('too-few-comments', node=node,
args=('%.2f' % ratio,
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

setuptools.setup(
name="cnes-pylint-extension",
version="5.0.0",
version="6.0.0",
author="CNES CatLab",
description="A PyLint plugin to add coding CNES specific checks",
description="A PyLint plugin to add CNES specific checks",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/cnescatlab/cnes-pylint-extension",
Expand All @@ -22,7 +22,7 @@
python_requires='>=3.6',
install_requires=[
"pylint-plugin-utils==0.7",
"pylint==2.5.0"
"pylint>=2.12.0,<3.0.0"
],
project_urls={
'Bug Reports': 'https://github.com/cnescatlab/cnes-pylint-extension/issues'
Expand Down

0 comments on commit 35be209

Please sign in to comment.