summaryrefslogtreecommitdiff
path: root/pylint/test/unittest_checker_python3.py
diff options
context:
space:
mode:
authorRoy Williams <roy.williams.iii@gmail.com>2017-01-03 02:16:22 -0500
committerClaudiu Popa <pcmanticore@gmail.com>2017-01-03 09:16:22 +0200
commitc002c48a9b8d98b43262e045243af2a80cb8a1f3 (patch)
tree648c2ecfb39a5d257be13063078e15ecdcf588c1 /pylint/test/unittest_checker_python3.py
parente2f34ebe51d0b9d08ff7a03117b0149434446997 (diff)
downloadpylint-git-c002c48a9b8d98b43262e045243af2a80cb8a1f3.tar.gz
Respect checks for Python 2 (#1196)
Frequently 2and3 code will gate some Python 3 specific code with something like: ```python if six.PY2: # something python 2 only ``` This PR will respect those branches. One thing I wasn't able to do was handle `else` branches here. e.g.: ```python if six.PY3: # something python 3 only else: # something python 2 only ```
Diffstat (limited to 'pylint/test/unittest_checker_python3.py')
-rw-r--r--pylint/test/unittest_checker_python3.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py
index 95eeb7d6a..325d046a2 100644
--- a/pylint/test/unittest_checker_python3.py
+++ b/pylint/test/unittest_checker_python3.py
@@ -714,6 +714,69 @@ class TestPython3Checker(testutils.CheckerTestCase):
with self.assertNoMessages():
self.checker.visit_call(node)
+ def test_non_py2_conditional(self):
+ code = '''
+ from __future__ import absolute_import
+ import sys
+ x = {}
+ if sys.maxsize:
+ x.iterkeys() #@
+ '''
+ node = astroid.extract_node(code)
+ module = node.parent.parent
+ message = testutils.Message('dict-iter-method', node=node)
+ with self.assertAddsMessages(message):
+ self.walk(module)
+
+ def test_six_conditional(self):
+ code = '''
+ from __future__ import absolute_import
+ import six
+ x = {}
+ if six.PY2:
+ x.iterkeys()
+ '''
+ module = astroid.parse(code)
+ with self.assertNoMessages():
+ self.walk(module)
+
+ @python2_only
+ def test_versioninfo_conditional(self):
+ code = '''
+ from __future__ import absolute_import
+ import sys
+ x = {}
+ if sys.version_info[0] == 2:
+ x.iterkeys()
+ '''
+ module = astroid.parse(code)
+ with self.assertNoMessages():
+ self.walk(module)
+
+ @python2_only
+ def test_versioninfo_tuple_conditional(self):
+ code = '''
+ from __future__ import absolute_import
+ import sys
+ x = {}
+ if sys.version_info == (2, 7):
+ x.iterkeys()
+ '''
+ module = astroid.parse(code)
+ with self.assertNoMessages():
+ self.walk(module)
+
+ @python2_only
+ def test_six_ifexp_conditional(self):
+ code = '''
+ from __future__ import absolute_import
+ import six
+ import string
+ string.translate if six.PY2 else None
+ '''
+ module = astroid.parse(code)
+ with self.assertNoMessages():
+ self.walk(module)
@python2_only
class TestPython3TokenChecker(testutils.CheckerTestCase):