(#12330) linter - fix regex matching messages

* linter - testing messages

* second part in brackets is optional

* Override message for E0611

* use cleaner approach for linter

* import both errors

* Error if errors imported

* revert changes in any-lite
This commit is contained in:
Javier G. Sogo
2022-08-18 12:04:52 +02:00
committed by GitHub
parent 9e650bcfb1
commit 6009a8ef4e
5 changed files with 97 additions and 7 deletions

View File

@@ -0,0 +1,77 @@
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from astroid import nodes, Const, AssignName
class ImportErrors(BaseChecker):
"""
Import errors from new 'conan' module
"""
__implements__ = IAstroidChecker
name = "conan-import-errors"
msgs = {
"E9008": (
"Import errors from new module: `from conan import errors`. Old import is deprecated in Conan v2.",
"conan-import-errors",
"Import errors from new module: `from conan import errors`. Old import is deprecated in Conan v2.",
),
}
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
basename = node.modname
if basename == 'conans':
names = [name for name, _ in node.names]
if 'errors' in names:
self.add_message("conan-import-errors", node=node)
class ImportErrorsConanException(BaseChecker):
"""
Import errors from new 'conan' module
"""
__implements__ = IAstroidChecker
name = "conan-import-error-conanexception"
msgs = {
"E9009": (
"Import ConanException from new module: `from conan.errors import ConanException`. Old import is deprecated in Conan v2.",
"conan-import-error-conanexception",
"Import ConanException from new module: `from conan.errors import ConanException`. Old import is deprecated in Conan v2.",
),
}
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
basename = node.modname
if basename == 'conans.errors':
names = [name for name, _ in node.names]
if 'ConanException' in names:
self.add_message("conan-import-error-conanexception", node=node)
class ImportErrorsConanInvalidConfiguration(BaseChecker):
"""
Import errors from new 'conan' module
"""
__implements__ = IAstroidChecker
name = "conan-import-error-conaninvalidconfiguration"
msgs = {
"E9010": (
"Import ConanInvalidConfiguration from new module: `from conan.errors import ConanInvalidConfiguration`. Old import is deprecated in Conan v2.",
"conan-import-error-conaninvalidconfiguration",
"Import ConanInvalidConfiguration from new module: `from conan.errors import ConanInvalidConfiguration`. Old import is deprecated in Conan v2.",
),
}
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
basename = node.modname
if basename == 'conans.errors':
names = [name for name, _ in node.names]
if 'ConanInvalidConfiguration' in names:
self.add_message("conan-import-error-conaninvalidconfiguration", node=node)

View File

@@ -7,8 +7,12 @@ Pylint plugin/rules for test_package folder in Conan Center Index
from pylint.lint import PyLinter
from linter.check_import_conanfile import ImportConanFile
from linter.check_no_test_package_name import NoPackageName
from linter.check_import_errors import ImportErrorsConanException, ImportErrorsConanInvalidConfiguration, ImportErrors
def register(linter: PyLinter) -> None:
linter.register_checker(NoPackageName(linter))
linter.register_checker(ImportConanFile(linter))
linter.register_checker(ImportErrors(linter))
linter.register_checker(ImportErrorsConanException(linter))
linter.register_checker(ImportErrorsConanInvalidConfiguration(linter))

View File

@@ -7,8 +7,12 @@ Pylint plugin/rules for conanfiles in Conan Center Index
from pylint.lint import PyLinter
from linter.check_package_name import PackageName
from linter.check_import_conanfile import ImportConanFile
from linter.check_import_errors import ImportErrorsConanException, ImportErrorsConanInvalidConfiguration, ImportErrors
def register(linter: PyLinter) -> None:
linter.register_checker(PackageName(linter))
linter.register_checker(ImportConanFile(linter))
linter.register_checker(ImportErrors(linter))
linter.register_checker(ImportErrorsConanException(linter))
linter.register_checker(ImportErrorsConanInvalidConfiguration(linter))

View File

@@ -5,7 +5,7 @@
"severity": "error",
"pattern": [
{
"regexp": "(\\S+):(\\d+): \\[(F\\d+\\(\\S+\\)),\\s(.+?)\\](.+)",
"regexp": "(\\S+):(\\d+): \\[(F\\d+\\(\\S+\\)),\\s(.+?)?\\](.+)",
"file": 1,
"line": 2,
"message": 5,
@@ -18,7 +18,7 @@
"severity": "error",
"pattern": [
{
"regexp": "(\\S+):(\\d+): \\[(E\\d+\\(\\S+\\)),\\s(.+?)\\](.+)",
"regexp": "(\\S+):(\\d+): \\[(E\\d+\\(\\S+\\)),\\s(.+?)?\\](.+)",
"file": 1,
"line": 2,
"message": 5,
@@ -31,7 +31,7 @@
"severity": "warning",
"pattern": [
{
"regexp": "(\\S+):(\\d+): \\[(W\\d+\\(\\S+\\)),\\s(.+?)\\](.+)",
"regexp": "(\\S+):(\\d+): \\[(W\\d+\\(\\S+\\)),\\s(.+?)?\\](.+)",
"file": 1,
"line": 2,
"message": 5,

View File

@@ -14,6 +14,10 @@ def register(linter: PyLinter):
msge1101.msg += ". Please, check https://github.com/conan-io/conan-center-index/blob/master/docs/v2_linter.md"
linter.msgs_store.register_message(msge1101)
msge0611 = linter.msgs_store._messages_definitions["E0611"]
msge0611.msg += ". Please, check https://github.com/conan-io/conan-center-index/blob/master/docs/v2_linter.md"
linter.msgs_store.register_message(msge0611)
def transform_tools(module):
""" Transform import module """
if 'get' in module.locals:
@@ -26,10 +30,11 @@ def transform_tools(module):
del module.locals['Version']
def transform_errors(module):
if 'ConanInvalidConfiguration' in module.locals:
del module.locals['ConanInvalidConfiguration']
if 'ConanException' in module.locals:
del module.locals['ConanException']
pass
#if 'ConanInvalidConfiguration' in module.locals:
# del module.locals['ConanInvalidConfiguration']
#if 'ConanException' in module.locals:
# del module.locals['ConanException']
astroid.MANAGER.register_transform(