forked from Vauxoo/pylint-odoo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes Vauxoo#177 This new check looks for python files (modules) which are not referenced anywhere in the codebase, making them useless.
- Loading branch information
Showing
15 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from . import odoo_addons | ||
from . import vim_comment | ||
from . import custom_logging | ||
from . import custom_logging, import_checker, odoo_addons, vim_comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import re | ||
|
||
from pylint.checkers.utils import only_required_for_messages | ||
from pylint.lint import PyLinter | ||
|
||
from .odoo_base_checker import OdooBaseChecker | ||
|
||
ODOO_MSGS = { | ||
# C->convention R->refactor W->warning E->error F->fatal | ||
"E8401": ( | ||
"This python module is not imported and has no effect", | ||
"unused-module", | ||
"", | ||
), | ||
} | ||
|
||
UNUSED_MODULE_EXCLUDE = re.compile("migrations/|__init__.py$|__manifest__.py$|__openerp__.py$") | ||
|
||
|
||
class ImportChecker(OdooBaseChecker): | ||
msgs = ODOO_MSGS | ||
name = "odoolint" | ||
|
||
def __init__(self, linter: PyLinter): | ||
self.imports = set() | ||
self.modules = set() | ||
|
||
super().__init__(linter) | ||
|
||
@staticmethod | ||
def get_package(node) -> str: | ||
if "." in node.parent.name: | ||
return node.parent.name[: node.parent.name.rfind(".")] | ||
|
||
return node.parent.name | ||
|
||
@only_required_for_messages("unused-module") | ||
def visit_module(self, node) -> None: | ||
if UNUSED_MODULE_EXCLUDE.search(node.file): | ||
return | ||
|
||
self.modules.add(node) | ||
|
||
@only_required_for_messages("unused-module") | ||
def visit_importfrom(self, node) -> None: | ||
if node.modname: | ||
self.imports.add(f"{self.get_package(node)}.{node.modname}") | ||
return | ||
|
||
for name in node.names: | ||
self.imports.add(f"{node.parent.name}.{name[0]}") | ||
|
||
@only_required_for_messages("unused-module") | ||
def close(self) -> None: | ||
for module in self.modules: | ||
if module.name not in self.imports: | ||
self.add_message("unused-module", node=module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
'name': 'Test Import Messages', | ||
'license': 'AGPL-3', | ||
'author': 'Vauxoo, Odoo Community Association (OCA)', | ||
'version': '11.0.0.0.0', | ||
'depends': [ | ||
'base', | ||
], | ||
'data': [], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import res_partner |
6 changes: 6 additions & 0 deletions
6
testing/resources/test_repo/unused_module/models/fail_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from odoo.models import BaseModel | ||
|
||
class FailModel(BaseModel): | ||
_name = "fail.model" | ||
|
||
not_imported = fields.Boolean(default=True) |
8 changes: 8 additions & 0 deletions
8
testing/resources/test_repo/unused_module/models/res_partner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from odoo import fields | ||
from odoo.models import BaseModel | ||
|
||
|
||
class ResPartner(BaseModel): | ||
_name = "res.partner" | ||
|
||
random_field = fields.Char() |
1 change: 1 addition & 0 deletions
1
testing/resources/test_repo/unused_module/models/unused_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
RANDOM_STRING = "HELLO" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import pass_wizard |
7 changes: 7 additions & 0 deletions
7
testing/resources/test_repo/unused_module/wizard/fail_wizard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from odoo import fields, models | ||
|
||
|
||
class FailWizard(models.AbstractModel): | ||
_name = "fail.wizard" | ||
|
||
name = fields.Char() |
13 changes: 13 additions & 0 deletions
13
testing/resources/test_repo/unused_module/wizard/pass_wizard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from odoo.models import AbstractModel | ||
from odoo import fields | ||
|
||
from .utils import func | ||
|
||
|
||
class PassWizard(AbstractModel): | ||
_name = "pass.wizard" | ||
|
||
name = fields.Char(required=True) | ||
|
||
def foo(self): | ||
return func(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(*_args): | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters