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

[IMP] pylint_odoo: black, isort, prettier #397

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ select = C,E,F,W,B,B9
# E203: whitespace before ':' (black behaviour)
# E501: flake8 line length (covered by bugbear B950)
# W503: line break before binary operator (black behaviour)
ignore = E203,E501,W503
# W504: line break after binary operator (black behaviour)
# C901: too complex is enabled from pylint
ignore = E203,E501,W503,W504,C901
per-file-ignores=
__init__.py:F401
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exclude: |
# Library files can have extraneous formatting (even minimized)
/static/(src/)?lib/|
# test_repo is expected to break lints
test_repo/|
testing/resources/|
# Repos using Sphinx to generate docs don't need prettying
^docs/_templates/.*\.html$|
# You don't usually want a bot to modify your legal texts
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- id: pylint_odoo
name: Check for Odoo modules using pylint
entry: pylint
language: python
types: [python]
require_serial: true
- id: pylint_odoo
name: Check for Odoo modules using pylint
entry: pylint
language: python
types: [python]
require_serial: true
13 changes: 13 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ disable=c-extension-no-member,
missing-class-docstring,
missing-function-docstring,
missing-module-docstring,
protected-access,
suppressed-message,
too-few-public-methods,
too-many-ancestors,
Expand All @@ -36,6 +37,15 @@ disable=c-extension-no-member,
too-many-return-statements,
too-many-star-expressions,
too-many-statements,
unused-argument,
# TODO: Re-enable the following one
attribute-defined-outside-init,
consider-using-f-string,
implicit-str-concat,
too-complex,
no-member,
abstract-method,
arguments-differ,

[REPORTS]
msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}
Expand All @@ -54,3 +64,6 @@ ignore-docstrings=yes
# but that in some circumstances it may be appropriate to relax the restriction
# and permit modules with a complexity as high as 15
max-complexity=15

[BASIC]
good-names=maxDiff
4 changes: 0 additions & 4 deletions install.sh

This file was deleted.

2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ filterwarnings =
# You can add exclusions, some examples:
# ignore:'pylint_odoo' defines default_app_config:PendingDeprecationWarning::
# ignore:The {{% if:::
# ignore:Coverage disabled via --no-cov switch!
# ignore:Coverage disabled via --no-cov switch!
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
from os.path import basename, dirname, join, splitext
import re
from setuptools import find_packages, setup
from os.path import dirname, join

from setuptools import setup

try:
from pbr import git
except ImportError:
git = None


def generate_changelog():
fname = "ChangeLog"
if not git:
changelog_str = '# ChangeLog was not generated. You need to install "pbr"'
with open(fname, "w", encoding="UTF-8") as fchg:
fchg.write(changelog_str)
return changelog_str
# pylint: disable=protected-access
changelog = git._iter_log_oneline()
changelog = git._iter_changelog(changelog)
git.write_git_changelog(changelog=changelog)
# git.generate_authors()
return read(fname)


def generate_dependencies():
return read("requirements.txt").splitlines()

Expand Down
47 changes: 26 additions & 21 deletions src/pylint_odoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__version__ = "8.0.0"

from . import checkers
from . augmentations.main import apply_augmentations
from .augmentations.main import apply_augmentations


def register(linter):
Expand All @@ -25,11 +25,10 @@ def get_all_messages():

def messages2md():
all_msgs = get_all_messages()
md_msgs = 'Code | Description | short name\n--- | --- | ---'
for msg_code, (title, name_key, description) in \
sorted(all_msgs.items()):
md_msgs += "\n{0} | {1} | {2}".format(msg_code, title, name_key)
md_msgs += '\n'
md_msgs = "Code | Description | short name\n--- | --- | ---"
for msg_code, (title, name_key, _description) in sorted(all_msgs.items()):
md_msgs += "\n{} | {} | {}".format(msg_code, title, name_key)
md_msgs += "\n"
return md_msgs


Expand All @@ -49,26 +48,32 @@ def messages2rst():
lines = []
max_col_sizes = [len(item) for item in title_list]
for msg_code, msg_items in sorted(all_msgs.items()):
title, name_key, description = msg_items[0:3]
line = [item.replace('`', '``')
for item in [msg_code, title, name_key]]
title, name_key, _description = msg_items[0:3]
line = [item.replace("`", "``") for item in [msg_code, title, name_key]]
# TODO: consider using enumerate :)
# pylint: disable=consider-using-enumerate
for index in range(len(max_col_sizes)):
if len(line[index]) > max_col_sizes[index]:
max_col_sizes[index] = len(line[index])
lines.append(line)

def rst_spaces(max_col_sizes, line=None, sep='|', fill=' '):
def rst_spaces(max_col_sizes, line=None, sep="|", fill=" "):
if line is None:
line = [''] * len(max_col_sizes)
return ''.join(
[sep + fill + line[index] +
fill * (max_col_sizes[index] - len(line[index]) + 1)
for index in range(len(max_col_sizes))]) + sep + '\n'
rst_msgs = rst_spaces(max_col_sizes, sep='+', fill='-')
line = [""] * len(max_col_sizes)
return (
"".join(
[
sep + fill + line[index] + fill * (max_col_sizes[index] - len(line[index]) + 1)
for index in range(len(max_col_sizes))
]
)
+ sep
+ "\n"
)

rst_msgs = rst_spaces(max_col_sizes, sep="+", fill="-")
rst_msgs += rst_spaces(max_col_sizes, title_list)
rst_msgs += rst_spaces(max_col_sizes, sep='+', fill='=')
rst_msgs += rst_spaces(max_col_sizes, sep='+', fill='-').join(
[rst_spaces(max_col_sizes, item) for item in lines])
rst_msgs += rst_spaces(max_col_sizes, sep='+', fill='-')
rst_msgs = rst_msgs
rst_msgs += rst_spaces(max_col_sizes, sep="+", fill="=")
rst_msgs += rst_spaces(max_col_sizes, sep="+", fill="-").join([rst_spaces(max_col_sizes, item) for item in lines])
rst_msgs += rst_spaces(max_col_sizes, sep="+", fill="-")
return rst_msgs
22 changes: 8 additions & 14 deletions src/pylint_odoo/augmentations/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import os

from astroid import FunctionDef, Module
from pylint.checkers.base import BasicChecker, NameChecker
from pylint.checkers.imports import ImportsChecker
from pylint.checkers.variables import VariablesChecker
from pylint_plugin_utils import suppress_message

Expand Down Expand Up @@ -35,14 +33,12 @@ def migrate(cr, version):
"""

# get 'migrations' from 'module/migrations/x.y.z/pre-migration.py'
if os.path.basename(os.path.dirname(os.path.dirname(
node.root().file))) != 'migrations':
if os.path.basename(os.path.dirname(os.path.dirname(node.root().file))) != "migrations":
return False

# pre-migration.py
if (isinstance(node, Module) and '-' in node.name or
# def migrate(cr, version):
isinstance(node, FunctionDef) and node.name == 'migrate'):
# def migrate(cr, version):
if isinstance(node, Module) and "-" in node.name or isinstance(node, FunctionDef) and node.name == "migrate":
return True
return False

Expand All @@ -52,12 +48,10 @@ def apply_augmentations(linter):

# W0104 - pointless-statement
# manifest file have a valid pointless-statement dict
discard = hasattr(BasicChecker, 'visit_discard') and \
BasicChecker.visit_discard or BasicChecker.visit_expr
suppress_message(linter, discard, 'W0104', is_manifest_file)
discard = BasicChecker.visit_discard if hasattr(BasicChecker, "visit_discard") else BasicChecker.visit_expr
suppress_message(linter, discard, "W0104", is_manifest_file)

# C0103 - invalid-name and W0613 - unused-argument for migrations/
suppress_message(linter, NameChecker.visit_module, 'C0103', is_migration_path)
suppress_message(linter, NameChecker.visit_functiondef, 'C0103', is_migration_path)
suppress_message(linter, VariablesChecker.leave_functiondef, 'W0613',
is_migration_path)
suppress_message(linter, NameChecker.visit_module, "C0103", is_migration_path)
suppress_message(linter, NameChecker.visit_functiondef, "C0103", is_migration_path)
suppress_message(linter, VariablesChecker.leave_functiondef, "W0613", is_migration_path)
1 change: 1 addition & 0 deletions src/pylint_odoo/checkers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint:disable=redefined-builtin
from . import modules_odoo
from . import no_modules
from . import format
21 changes: 5 additions & 16 deletions src/pylint_odoo/checkers/format.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@

import os
import tokenize
from sys import platform

from .. import settings
from ..misc import PylintOdooTokenChecker

ODOO_MSGS = {
# C->convention R->refactor W->warning E->error F->fatal

'W%d02' % settings.BASE_FORMAT_ID: (
'Use of vim comment',
'use-vim-comment',
settings.DESC_DFLT
),
"W%d02"
% settings.BASE_FORMAT_ID: ("Use of vim comment", "use-vim-comment", settings.DESC_DFLT),
}

MAGIC_COMMENT_CODING = 1
Expand All @@ -29,15 +22,11 @@ class FormatChecker(PylintOdooTokenChecker):
msgs = ODOO_MSGS

def is_vim_comment(self, comment):
return True if comment.strip('# ').lower().startswith('vim:') \
else False
return comment.strip("# ").lower().startswith("vim:")

def process_tokens(self, tokens):
tokens_identified = {}
for idx, (tok_type, token_content,
start_line_col, end_line_col,
line_content) in enumerate(tokens):
for tok_type, token_content, start_line_col, _end_line_col, _line_content in tokens:
if tokenize.COMMENT == tok_type:
line_num = start_line_col[0]
if self.is_vim_comment(token_content):
self.add_message('use-vim-comment', line=line_num)
self.add_message("use-vim-comment", line=line_num)
Loading