File tree Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change
1
+ Understand `six.PY2` and `six.PY3` for conditional imports.
2
+
3
+ Closes #3501
Original file line number Diff line number Diff line change @@ -1831,7 +1831,11 @@ def is_sys_guard(node: nodes.If) -> bool:
1831
1831
and value .as_string () == "sys.version_info"
1832
1832
):
1833
1833
return True
1834
-
1834
+ elif isinstance (node .test , nodes .Attribute ) and node .test .as_string () in {
1835
+ "six.PY2" ,
1836
+ "six.PY3" ,
1837
+ }:
1838
+ return True
1835
1839
return False
1836
1840
1837
1841
Original file line number Diff line number Diff line change @@ -400,9 +400,19 @@ def test_if_sys_guard() -> None:
400
400
401
401
if sys.some_other_function > (3, 8): #@
402
402
pass
403
+
404
+ import six
405
+ if six.PY2: #@
406
+ pass
407
+
408
+ if six.PY3: #@
409
+ pass
410
+
411
+ if six.something_else: #@
412
+ pass
403
413
"""
404
414
)
405
- assert isinstance (code , list ) and len (code ) == 3
415
+ assert isinstance (code , list ) and len (code ) == 6
406
416
407
417
assert isinstance (code [0 ], nodes .If )
408
418
assert utils .is_sys_guard (code [0 ]) is True
@@ -412,6 +422,14 @@ def test_if_sys_guard() -> None:
412
422
assert isinstance (code [2 ], nodes .If )
413
423
assert utils .is_sys_guard (code [2 ]) is False
414
424
425
+ assert isinstance (code [3 ], nodes .If )
426
+ assert utils .is_sys_guard (code [3 ]) is True
427
+ assert isinstance (code [4 ], nodes .If )
428
+ assert utils .is_sys_guard (code [4 ]) is True
429
+
430
+ assert isinstance (code [5 ], nodes .If )
431
+ assert utils .is_sys_guard (code [5 ]) is False
432
+
415
433
416
434
def test_if_typing_guard () -> None :
417
435
code = astroid .extract_node (
You can’t perform that action at this time.
0 commit comments