diff options
-rw-r--r-- | CONTRIBUTORS.txt | 1 | ||||
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | doc/whatsnew/2.0.rst | 16 | ||||
-rw-r--r-- | pylint/checkers/python3.py | 12 | ||||
-rw-r--r-- | pylint/test/unittest_checker_python3.py | 18 |
5 files changed, 47 insertions, 3 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 9074450c2..58dc72c6d 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. @@ -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 ab3e32a44..6b15de22e 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 a19bec14b..0eeaa4e9e 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 @@ class Python3Checker(checkers.BaseChecker): """ 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 789536f17..d99e54c22 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -491,6 +491,24 @@ class Python3CheckerTest(testutils.CheckerTestCase): 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): |