Skip to content

Commit 9b268ec

Browse files
authored
Add sys.version_info guard to import-error (#4468)
1 parent 6044930 commit 9b268ec

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

ChangeLog

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ modules are added.
3838

3939
Closes #3389
4040

41+
* Don't emit ``import-error`` if import guarded behind ``if sys.version_info >= (x, x)``
42+
4143

4244
What's New in Pylint 2.8.2?
4345
===========================

pylint/checkers/imports.py

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ def _ignore_import_failure(node, modname, ignored_modules):
135135
if submodule in ignored_modules:
136136
return True
137137

138+
# ignore import failure if guarded by `sys.version_info` test
139+
if isinstance(node.parent, astroid.If) and isinstance(
140+
node.parent.test, astroid.Compare
141+
):
142+
value = node.parent.test.left
143+
if isinstance(value, astroid.Subscript):
144+
value = value.value
145+
if (
146+
isinstance(value, astroid.Attribute)
147+
and value.as_string() == "sys.version_info"
148+
):
149+
return True
150+
138151
return node_ignores_exception(node, ImportError)
139152

140153

tests/functional/i/import_error.py

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@
2626

2727
# pylint: disable=no-name-in-module
2828
from functional.s.syntax_error import toto # [syntax-error]
29+
30+
# Don't emit import-error if guarded behind `sys.version_info`
31+
import sys
32+
33+
if sys.version_info >= (3, 9):
34+
import zoneinfo
35+
36+
if sys.version_info[:2] >= (3, 9):
37+
import zoneinfo

0 commit comments

Comments
 (0)