Skip to content

Commit

Permalink
Merge pull request #10811 from czoido/port_deprecated_imports_checker
Browse files Browse the repository at this point in the history
[pylint_plugin] Added deprecated imports checker (#10746)
  • Loading branch information
czoido committed Mar 17, 2022
2 parents 2ace493 + e58a396 commit 084b21f
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions conans/pylint_plugin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Pylint plugin for ConanFile"""
import re

import astroid
from astroid import MANAGER
from pylint.checkers import BaseChecker
from pylint.interfaces import IRawChecker


def register(linter):
"""Declare package as plugin
This function needs to be declared so astroid treats
current file as a plugin.
"""
pass
"""required method to auto register this checker"""
linter.register_checker(ConanDeprecatedImportsChecker(linter))


def transform_conanfile(node):
Expand Down Expand Up @@ -55,3 +55,34 @@ def _python_requires_member():


astroid.register_module_extender(astroid.MANAGER, "conans", _python_requires_member)


class ConanDeprecatedImportsChecker(BaseChecker):
"""
Check "from conans*" imports which disappears in Conan 2.x. Only "from conan*" is valid
"""

__implements__ = IRawChecker

deprecated_imports_pattern = re.compile(r"(from|import)\s+conans[\.|\s].*")
name = "conan_deprecated_imports"
msgs = {
"E9000": (
"Using deprecated imports from 'conans'",
"conan1.x-deprecated-imports",
(
"Use imports from 'conan' instead of 'conans'"
" because 'conan' will be the root package for Conan 2.x"
)
)
}
options = ()

def process_module(self, node):
"""
Processing the module's content that is accessible via node.stream() function
"""
with node.stream() as stream:
for (index, line) in enumerate(stream):
if self.deprecated_imports_pattern.match(line.decode('utf-8')):
self.add_message("conan1.x-deprecated-imports", line=index)

0 comments on commit 084b21f

Please sign in to comment.