Skip to content

Commit

Permalink
Add a Python 3 warning about accessing sys.maxint (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowillia committed Nov 29, 2016
1 parent 1b73f45 commit b84ed3d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -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?
===========================
Expand Down
16 changes: 16 additions & 0 deletions doc/whatsnew/2.0.rst
Expand Up @@ -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
=============

Expand Down
12 changes: 9 additions & 3 deletions pylint/checkers/python3.py
Expand Up @@ -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([
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions pylint/test/unittest_checker_python3.py
Expand Up @@ -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):
Expand Down

0 comments on commit b84ed3d

Please sign in to comment.