From b84ed3d16ea83e554381b2e9d97660d42d5ede1b Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Tue, 29 Nov 2016 12:54:52 -0800 Subject: [PATCH] Add a Python 3 warning about accessing sys.maxint (#1180) --- CONTRIBUTORS.txt | 1 + ChangeLog | 3 +++ doc/whatsnew/2.0.rst | 16 ++++++++++++++++ pylint/checkers/python3.py | 12 +++++++++--- pylint/test/unittest_checker_python3.py | 18 ++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 9074450c2b..58dc72c6db 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -112,6 +112,7 @@ Order doesn't matter (not that much, at least ;) * Roy Williams (Lyft): added check for implementing __eq__ without implementing __hash__, Added Python 3 check for accessing Exception.message, Added Python 3 check for calling encode/decode with invalid codecs. + Added Python 3 check for accessing sys.maxint * Erik Eriksson - Added overlapping-except error check. diff --git a/ChangeLog b/ChangeLog index 993e53d1fe..e1592a7cd2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -200,6 +200,9 @@ Release date: tba Closes #1166 + * Added a new Python 3 check for accessing 'sys.maxint' which was removed in Python 3 in favor + of 'sys.maxsize' + What's new in Pylint 1.6.3? =========================== diff --git a/doc/whatsnew/2.0.rst b/doc/whatsnew/2.0.rst index ab3e32a444..6b15de22e9 100644 --- a/doc/whatsnew/2.0.rst +++ b/doc/whatsnew/2.0.rst @@ -293,6 +293,22 @@ New checkers except (ConnectionError, IOError, OSError, socket.error): pass +* A new Python 3 checker was added to warn about accessing ``sys.maxint``. This attribute was + removed in Python 3 in favor of ``sys.maxsize``. + + .. code-block:: python + + import sys + print(sys.maxint) + + Instead of using ``sys.maxint``, use ``sys.maxsize`` + + .. code-block:: python + + import sys + print(sys.maxsize) + + Other Changes ============= diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py index a19bec14b0..0eeaa4e9e6 100644 --- a/pylint/checkers/python3.py +++ b/pylint/checkers/python3.py @@ -367,6 +367,10 @@ class Python3Checker(checkers.BaseChecker): 'Used when using str.encode or str.decode with a non-text encoding. Use ' 'codecs module to handle arbitrary codecs.', {'maxversion': (3, 0)}), + 'W1647': ('sys.maxint removed in Python 3', + 'sys-max-int', + 'Used when accessing sys.maxint. Use sys.maxsize instead.', + {'maxversion': (3, 0)}), } _bad_builtins = frozenset([ @@ -591,11 +595,13 @@ def visit_attribute(self, node): """ Look for accessing message on exceptions. """ try: for infered in node.expr.infer(): - if not isinstance(infered, astroid.Instance): - continue - if utils.inherit_from_std_ex(infered): + if (isinstance(infered, astroid.Instance) and + utils.inherit_from_std_ex(infered)): if node.attrname == 'message': self.add_message('exception-message-attribute', node=node) + if isinstance(infered, astroid.Module) and infered.name == 'sys': + if node.attrname == 'maxint': + self.add_message('sys-max-int', node=node) except astroid.InferenceError: return diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py index 789536f173..d99e54c227 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -491,6 +491,24 @@ def test_using_cmp_argument(self): with self.assertAddsMessages(message): self.checker.visit_call(node) + @python2_only + def test_sys_maxint(self): + node = astroid.extract_node(''' + import sys + sys.maxint #@ + ''') + message = testutils.Message('sys-max-int', node=node) + with self.assertAddsMessages(message): + self.checker.visit_attribute(node) + + @python2_only + def test_object_maxint(self): + node = astroid.extract_node(''' + sys = object() + sys.maxint #@ + ''') + with self.assertNoMessages(): + self.checker.visit_attribute(node) @python2_only class Python3TokenCheckerTest(testutils.CheckerTestCase):