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

[FIX] manifest-version-format: Catch ValueError exception for valid-odoo-versions parameter #520

Merged
merged 1 commit into from
Jan 16, 2025
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
16 changes: 13 additions & 3 deletions src/pylint_odoo/checkers/odoo_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import os
import re
import string
import warnings
from collections import Counter, defaultdict

from astroid import ClassDef, FunctionDef, NodeNG, nodes
Expand Down Expand Up @@ -623,10 +624,19 @@ def open(self):
deprecated_model_methods = ast.literal_eval(self.linter.config.deprecated_odoo_model_methods)
else:
deprecated_model_methods = DFTL_DEPRECATED_ODOO_MODEL_METHODS

max_valid_version = float(sorted(self.linter.config.valid_odoo_versions, key=float)[-1])
odoo_versions = [misc.version_parse(odoo_version) for odoo_version in self.linter.config.valid_odoo_versions]
if () in odoo_versions:
# Empty value means odoo_version value could not be adapted
warnings.warn(
f"Invalid manifest versions format {self.linter.config.valid_odoo_versions}. "
"It was not possible to supress checks based on particular odoo version",
UserWarning,
stacklevel=2,
)
return
max_valid_version = max(odoo_versions)
for version, checks in deprecated_model_methods.items():
if float(version) <= max_valid_version:
if misc.version_parse(version) <= max_valid_version:
self._deprecated_odoo_methods.update(checks)

def colon_list_to_dict(self, colon_list):
Expand Down
11 changes: 11 additions & 0 deletions src/pylint_odoo/checkers/odoo_base_checker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import warnings

import pylint
from pylint.checkers import BaseChecker

Expand Down Expand Up @@ -30,6 +32,15 @@ def is_odoo_message_enabled(self, msgid):
if msg_symbol is None:
return True
odoo_version = valid_odoo_versions[0]
odoo_version_tuple = misc.version_parse(odoo_version)
if not odoo_version_tuple:
warnings.warn(
f"Invalid manifest versions format {odoo_version}. "
"It was not possible to supress checks based on particular odoo version",
UserWarning,
stacklevel=2,
)
return True
required_odoo_versions = self.checks_maxmin_odoo_version.get(msg_symbol) or {}
odoo_minversion = required_odoo_versions.get("odoo_minversion") or misc.DFTL_VALID_ODOO_VERSIONS[0]
odoo_maxversion = required_odoo_versions.get("odoo_maxversion") or misc.DFTL_VALID_ODOO_VERSIONS[-1]
Expand Down
5 changes: 4 additions & 1 deletion src/pylint_odoo/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class StringParseError(TypeError):


def version_parse(version_str):
return tuple(map(int, version_str.split(".")))
try:
return tuple(map(int, version_str.split(".")))
except (ValueError, TypeError):
return tuple()


def get_plugin_msgs(pylint_run_res):
Expand Down
25 changes: 18 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re
import sys
import unittest
import warnings
from collections import Counter, defaultdict
from glob import glob
from io import StringIO
Expand Down Expand Up @@ -127,13 +126,11 @@ def run_pylint(self, paths, extra_params: list | None = None, verbose=False, rcf
@staticmethod
def _run_pylint(args, out, reporter):
with pytest.raises(SystemExit) as ctx_mgr:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if sys.gettrace() is None: # No pdb enabled
with _patch_streams(out):
Run(args, reporter=reporter)
else: # pdb enabled
if sys.gettrace() is None: # No pdb enabled
with _patch_streams(out):
Run(args, reporter=reporter)
else: # pdb enabled
Run(args, reporter=reporter)
return int(ctx_mgr.value.code)

def test_10_path_dont_exist(self):
Expand Down Expand Up @@ -464,6 +461,20 @@ def test_180_jobs(self):
for line, count in lines_counter.items():
self.assertLessEqual(count, 2, "%s duplicated more than 2 times. Line %s" % (key, line))

def test_format_version_value_error(self):
"""Test --valid-odoo-versions to force a value error exception"""
extra_params = [
"--valid-odoo-versions=8.0saas",
"--disable=all",
"--enable=manifest-version-format",
]
with self.assertWarns(UserWarning) as warn:
pylint_res = self.run_pylint(self.paths_modules, extra_params, verbose=True)
real_errors = pylint_res.linter.stats.by_msg
expected_errors = {"manifest-version-format": 6}
self.assertDictEqual(real_errors, expected_errors)
self.assertIn("Invalid manifest versions format ['8.0saas']", str(warn.warning))

@staticmethod
def re_replace(sub_start, sub_end, substitution, content):
re_sub = re.compile(rf"^{re.escape(sub_start)}$.*^{re.escape(sub_end)}$", re.M | re.S)
Expand Down
Loading