diff --git a/ChangeLog b/ChangeLog index b9e279ab9a..82da849a03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -38,6 +38,8 @@ modules are added. Closes #3389 +* Don't emit ``import-error`` if import guarded behind ``if sys.version_info >= (x, x)`` + What's New in Pylint 2.8.2? =========================== diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index 5a8dc385d6..73fc4cbbdf 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -135,6 +135,19 @@ def _ignore_import_failure(node, modname, ignored_modules): if submodule in ignored_modules: return True + # ignore import failure if guarded by `sys.version_info` test + if isinstance(node.parent, astroid.If) and isinstance( + node.parent.test, astroid.Compare + ): + value = node.parent.test.left + if isinstance(value, astroid.Subscript): + value = value.value + if ( + isinstance(value, astroid.Attribute) + and value.as_string() == "sys.version_info" + ): + return True + return node_ignores_exception(node, ImportError) diff --git a/tests/functional/i/import_error.py b/tests/functional/i/import_error.py index 3041ef671f..7eb4d78909 100644 --- a/tests/functional/i/import_error.py +++ b/tests/functional/i/import_error.py @@ -26,3 +26,12 @@ # pylint: disable=no-name-in-module from functional.s.syntax_error import toto # [syntax-error] + +# Don't emit import-error if guarded behind `sys.version_info` +import sys + +if sys.version_info >= (3, 9): + import zoneinfo + +if sys.version_info[:2] >= (3, 9): + import zoneinfo