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

[ADD] renamed-field-parameter: detect deprecated field values (digits_compute, select) (#91) #99

Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions pylint_odoo/checkers/no_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -264,6 +273,19 @@ class NoModuleChecker(BaseChecker):
'help': 'List of attributes deprecated, ' +
'separated by a comma.'
}),
('deprecated_field_parameters', {
'type': 'csv',
'metavar': '<comma separated values>',
'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': '<comma separated values>',
Expand Down Expand Up @@ -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.'):
Expand All @@ -317,13 +362,19 @@ 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 \
not argument_aux.value.startswith(
'_' + argument.arg + '_'):
self.add_message('method-' + argument.arg,
node=argument_aux)
elif (argument.arg in deprecated):
self.add_message(
'renamed-field-parameter', node=node,
args=(argument.arg, deprecated[argument.arg])
)
if isinstance(argument_aux, astroid.CallFunc) and \
isinstance(argument_aux.func, astroid.Name) and \
argument_aux.func.name == '_':
Expand Down
1 change: 1 addition & 0 deletions pylint_odoo/test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'use-vim-comment': 1,
'wrong-tabs-instead-of-spaces': 2,
'xml-syntax-error': 2,
'renamed-field-parameter': 2,
}


Expand Down
12 changes: 12 additions & 0 deletions pylint_odoo/test_repo/broken_module/models/broken_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down