From 5e9f97dc8db0eaf081f534741e204379cbae7442 Mon Sep 17 00:00:00 2001 From: "Moises Lopez - https://www.vauxoo.com/" Date: Thu, 16 Jan 2025 14:50:03 -0600 Subject: [PATCH] [FIX] manifest-version-format: Catch ValueError exception for valid-odoo-versions parameter --- src/pylint_odoo/checkers/odoo_addons.py | 16 +++++++++--- src/pylint_odoo/checkers/odoo_base_checker.py | 11 ++++++++ src/pylint_odoo/misc.py | 5 +++- tests/test_main.py | 25 +++++++++++++------ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/pylint_odoo/checkers/odoo_addons.py b/src/pylint_odoo/checkers/odoo_addons.py index e77801e5..9331236a 100644 --- a/src/pylint_odoo/checkers/odoo_addons.py +++ b/src/pylint_odoo/checkers/odoo_addons.py @@ -102,6 +102,7 @@ import os import re import string +import warnings from collections import Counter, defaultdict from astroid import ClassDef, FunctionDef, NodeNG, nodes @@ -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): diff --git a/src/pylint_odoo/checkers/odoo_base_checker.py b/src/pylint_odoo/checkers/odoo_base_checker.py index 2c9158d5..ac420258 100644 --- a/src/pylint_odoo/checkers/odoo_base_checker.py +++ b/src/pylint_odoo/checkers/odoo_base_checker.py @@ -1,5 +1,7 @@ from __future__ import annotations +import warnings + import pylint from pylint.checkers import BaseChecker @@ -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] diff --git a/src/pylint_odoo/misc.py b/src/pylint_odoo/misc.py index c3c5419f..e16ff79b 100644 --- a/src/pylint_odoo/misc.py +++ b/src/pylint_odoo/misc.py @@ -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): diff --git a/tests/test_main.py b/tests/test_main.py index ba618eff..8f644959 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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 @@ -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): @@ -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)