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

[pylint_plugin] Added deprecated imports checker (#10746) #10811

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)