From a90527ebdb071d6ee04fe44c51d0582fa25de6fe Mon Sep 17 00:00:00 2001 From: Tomas Alvarez Date: Mon, 26 Dec 2016 12:13:51 -0400 Subject: [PATCH 1/2] [ADD] renamed-field-parameter: detect deprecated field values (digits_compute, select) (#91) --- pylint_odoo/checkers/no_modules.py | 54 +++++++++++++++++++ pylint_odoo/test/main.py | 1 + .../broken_module/models/broken_model.py | 12 +++++ 3 files changed, 67 insertions(+) diff --git a/pylint_odoo/checkers/no_modules.py b/pylint_odoo/checkers/no_modules.py index 0a105105..c7d6334d 100644 --- a/pylint_odoo/checkers/no_modules.py +++ b/pylint_odoo/checkers/no_modules.py @@ -186,6 +186,11 @@ 'old-api7-method-defined', settings.DESC_DFLT ), + 'W%d11' % settings.BASE_NOMODULE_ID: ( + 'Field parameter "%s" is no longer supported. Use "%s" instead.', + 'renamed-field-parameter', + settings.DESC_DFLT + ), } DFTL_MANIFEST_REQUIRED_KEYS = ['license'] @@ -221,6 +226,10 @@ DFTL_NO_MISSING_RETURN = [ '__init__', 'setUp', 'tearDown', ] +DFTL_DEPRECATED_FIELD_PARAMETERS = [ + # From odoo/odoo 10.0: odoo/odoo/fields.py:29 + 'digits_compute:digits', 'select:index' +] class NoModuleChecker(BaseChecker): @@ -264,6 +273,19 @@ class NoModuleChecker(BaseChecker): 'help': 'List of attributes deprecated, ' + 'separated by a comma.' }), + ('deprecated_field_parameters', { + 'type': 'csv', + 'metavar': '', + 'default': DFTL_DEPRECATED_FIELD_PARAMETERS, + 'help': 'List of deprecated field parameters, separated by a ' + 'comma. If the param was renamed, separate the old and ' + 'new name with a colon. If the param was removed, keep ' + 'the right side of the colon empty. ' + '"deprecated_param:" means that "deprecated_param" was ' + 'deprecated and it doesn\'t have a new alternative. ' + '"deprecated_param:new_param" means that it was ' + 'deprecated and renamed as "new_param". ' + }), ('method_required_super', { 'type': 'csv', 'metavar': '', @@ -306,9 +328,32 @@ class NoModuleChecker(BaseChecker): }), ) + def open(self): + super(NoModuleChecker, self).open() + self.config.deprecated_field_parameters = \ + self.colon_list_to_dict(self.config.deprecated_field_parameters) + + def colon_list_to_dict(self, colon_list): + """Converts a colon list to a dictionary. + + :param colon_list: A list of strings representing keys and values, + separated with a colon. If a key doesn't have a value, keep the + right side of the colon empty. + :type colon_list: list + :returns: A dictionary with the values assigned to corresponding keys. + :rtype: dict + + :Example: + + >>> self.colon_list_to_dict(['colon:list', 'empty_key:']) + {'colon': 'list', 'empty_key': ''} + """ + return dict([item.split(":") for item in colon_list]) + @utils.check_messages('translation-field', 'invalid-commit', 'method-compute', 'method-search', 'method-inverse', 'sql-injection', + 'renamed-field-parameter' ) def visit_call(self, node): if node.as_string().lower().startswith('fields.'): @@ -324,6 +369,15 @@ def visit_call(self, node): '_' + argument.arg + '_'): self.add_message('method-' + argument.arg, node=argument_aux) + elif (argument.arg in + self.config.deprecated_field_parameters): + new_param = self.config.deprecated_field_parameters[ + argument.arg + ] + self.add_message( + 'renamed-field-parameter', node=node, + args=(argument.arg, new_param) + ) if isinstance(argument_aux, astroid.CallFunc) and \ isinstance(argument_aux.func, astroid.Name) and \ argument_aux.func.name == '_': diff --git a/pylint_odoo/test/main.py b/pylint_odoo/test/main.py index e92d237c..52284d2c 100644 --- a/pylint_odoo/test/main.py +++ b/pylint_odoo/test/main.py @@ -54,6 +54,7 @@ 'use-vim-comment': 1, 'wrong-tabs-instead-of-spaces': 2, 'xml-syntax-error': 2, + 'renamed-field-parameter': 2, } diff --git a/pylint_odoo/test_repo/broken_module/models/broken_model.py b/pylint_odoo/test_repo/broken_module/models/broken_model.py index ea275855..bcab36f0 100644 --- a/pylint_odoo/test_repo/broken_module/models/broken_model.py +++ b/pylint_odoo/test_repo/broken_module/models/broken_model.py @@ -46,6 +46,18 @@ class TestModel(models.Model): copy=True, ) + my_ok_field = fields.Float( + digits=(6, 6), # OK: Valid field parameter + index=True, # OK: Valid field parameter + help="My ok field", + ) + + my_ko_field = fields.Float( + digits_compute=lambda cr: (6, 6), # Deprecated field parameter + select=True, # Deprecated field parameter + help="My ko field", + ) + def my_method1(self, variable1): # Shouldn't show error of field-argument-translate self.my_method2(_('hello world')) From a56639f4d5baac601ac9c97ba569c3ef3fc021e3 Mon Sep 17 00:00:00 2001 From: Tomas Alvarez Date: Tue, 27 Dec 2016 13:48:18 +0000 Subject: [PATCH 2/2] [REF] checkers.no_modules: clean syntax --- pylint_odoo/checkers/no_modules.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pylint_odoo/checkers/no_modules.py b/pylint_odoo/checkers/no_modules.py index c7d6334d..bb78a35e 100644 --- a/pylint_odoo/checkers/no_modules.py +++ b/pylint_odoo/checkers/no_modules.py @@ -348,7 +348,7 @@ def colon_list_to_dict(self, colon_list): >>> self.colon_list_to_dict(['colon:list', 'empty_key:']) {'colon': 'list', 'empty_key': ''} """ - return dict([item.split(":") for item in colon_list]) + return dict(item.split(":") for item in colon_list) @utils.check_messages('translation-field', 'invalid-commit', 'method-compute', 'method-search', 'method-inverse', @@ -362,6 +362,7 @@ def visit_call(self, node): argument_aux = argument if isinstance(argument, astroid.Keyword): argument_aux = argument.value + deprecated = self.config.deprecated_field_parameters if argument.arg in ['compute', 'search', 'inverse'] and \ isinstance(argument_aux, astroid.Const) and \ isinstance(argument_aux.value, string_types) and \ @@ -369,14 +370,10 @@ def visit_call(self, node): '_' + argument.arg + '_'): self.add_message('method-' + argument.arg, node=argument_aux) - elif (argument.arg in - self.config.deprecated_field_parameters): - new_param = self.config.deprecated_field_parameters[ - argument.arg - ] + elif (argument.arg in deprecated): self.add_message( 'renamed-field-parameter', node=node, - args=(argument.arg, new_param) + args=(argument.arg, deprecated[argument.arg]) ) if isinstance(argument_aux, astroid.CallFunc) and \ isinstance(argument_aux.func, astroid.Name) and \