diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-05-11 21:51:01 -0400 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-05-11 21:51:01 -0400 |
commit | 6146ce636a7117a78a3e8f746762f477f603dae4 (patch) | |
tree | 2133f90c07aa07ee8150d0aa466ca6e5eafb426c | |
parent | f495279daed8a101e52c22963892c257a4d83662 (diff) | |
download | pylint-git-6146ce636a7117a78a3e8f746762f477f603dae4.tar.gz |
Emit a warning when .xreadlines() is accessed
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | pylint/checkers/python3.py | 18 | ||||
-rw-r--r-- | pylint/test/unittest_checker_python3.py | 8 |
3 files changed, 22 insertions, 5 deletions
@@ -8,6 +8,7 @@ What's New in Pylint 1.9.0? Release date: + * Added `xreadlines-attribute`, emitted when the `xreadlines()` attribute is accessed. * The Python 3 porting mode can now run with Python 3 as well. diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py index 3baac4ae2..b9e1e299e 100644 --- a/pylint/checkers/python3.py +++ b/pylint/checkers/python3.py @@ -405,6 +405,10 @@ class Python3Checker(checkers.BaseChecker): 'deprecated-urllib-function', 'Used when accessing a field on urllib module that has been ' 'removed or moved in Python 3.',), + 'W1659': ('Accessing a removed xreadlines attribute', + 'xreadlines-attribute', + 'Used when accessing the xreadlines() function on a file stream, ' + 'removed in Python 3.',), } _bad_builtins = frozenset([ @@ -823,18 +827,22 @@ class Python3Checker(checkers.BaseChecker): def visit_delattr(self, node): self.visit_attribute(node) - @utils.check_messages('exception-message-attribute') + @utils.check_messages('exception-message-attribute', 'xreadlines-attribute') def visit_attribute(self, node): - """Look for accessing message on exceptions. """ - message = 'message' + """Look for removed attributes""" + if node.attrname == 'xreadlines': + self.add_message('xreadlines-attribute', node=node) + return + + exception_message = 'message' try: for inferred in node.expr.infer(): if (isinstance(inferred, astroid.Instance) and utils.inherit_from_std_ex(inferred)): - if node.attrname == message: + if node.attrname == exception_message: # Exceptions with .message clearly defined are an exception - if message in inferred.instance_attrs: + if exception_message in inferred.instance_attrs: continue self.add_message('exception-message-attribute', node=node) if isinstance(inferred, astroid.Module): diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py index 374500f3e..c5800f493 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -494,6 +494,14 @@ class TestPython3Checker(testutils.CheckerTestCase): with self.assertAddsMessages(message): self.checker.visit_raise(node) + def test_xreadlines_attribute(self): + node = astroid.extract_node(""" + f.xreadlines #@ + """) + message = testutils.Message('xreadlines-attribute', node=node) + with self.assertAddsMessages(message): + self.checker.visit_attribute(node) + def test_exception_message_attribute(self): node = astroid.extract_node(""" try: |