Skip to content

Commit

Permalink
Add sys.version_info guard to import-error (#4468)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored May 11, 2021
1 parent 6044930 commit 9b268ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
===========================
Expand Down
13 changes: 13 additions & 0 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
9 changes: 9 additions & 0 deletions tests/functional/i/import_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9b268ec

Please sign in to comment.