2014-04-15 16:51:46 +00:00
|
|
|
"""Unit tests for the variables checker."""
|
2014-07-26 15:11:14 +00:00
|
|
|
import unittest
|
2015-02-09 13:05:48 +00:00
|
|
|
import sys
|
2014-04-15 16:51:46 +00:00
|
|
|
|
2015-07-13 20:28:24 +00:00
|
|
|
import astroid
|
2014-04-15 16:51:46 +00:00
|
|
|
from astroid import test_utils
|
|
|
|
from pylint.checkers import classes
|
2014-10-28 10:24:18 +00:00
|
|
|
from pylint.testutils import CheckerTestCase, Message, set_config
|
2014-04-15 16:51:46 +00:00
|
|
|
|
|
|
|
class VariablesCheckerTC(CheckerTestCase):
|
|
|
|
|
|
|
|
CHECKER_CLASS = classes.ClassChecker
|
|
|
|
|
|
|
|
def test_bitbucket_issue_164(self):
|
|
|
|
"""Issue 164 report a false negative for access-member-before-definition"""
|
|
|
|
n1, n2 = test_utils.extract_node("""
|
|
|
|
class MyClass1(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.first += 5 #@
|
|
|
|
self.first = 0 #@
|
|
|
|
""")
|
2015-07-13 20:55:31 +00:00
|
|
|
message = Message('access-member-before-definition',
|
|
|
|
node=n1.target, args=('first', n2.lineno))
|
|
|
|
with self.assertAddsMessages(message):
|
2014-04-15 16:51:46 +00:00
|
|
|
self.walk(n1.root())
|
2014-07-26 15:11:14 +00:00
|
|
|
|
2014-10-28 10:24:18 +00:00
|
|
|
@set_config(exclude_protected=('_meta', '_manager'))
|
|
|
|
def test_exclude_protected(self):
|
|
|
|
"""Test that exclude-protected can be used to
|
|
|
|
exclude names from protected-access warning.
|
|
|
|
"""
|
|
|
|
|
2015-07-13 20:28:24 +00:00
|
|
|
node = astroid.parse("""
|
2014-10-28 10:24:18 +00:00
|
|
|
class Protected(object):
|
|
|
|
'''empty'''
|
|
|
|
def __init__(self):
|
|
|
|
self._meta = 42
|
|
|
|
self._manager = 24
|
|
|
|
self._teta = 29
|
|
|
|
OBJ = Protected()
|
|
|
|
OBJ._meta
|
|
|
|
OBJ._manager
|
|
|
|
OBJ._teta
|
|
|
|
""")
|
|
|
|
with self.assertAddsMessages(
|
|
|
|
Message('protected-access',
|
|
|
|
node=node.body[-1].value,
|
|
|
|
args='_teta')):
|
|
|
|
self.walk(node.root())
|
|
|
|
|
2015-02-09 13:05:48 +00:00
|
|
|
@unittest.skipUnless(sys.version_info[0] == 3,
|
|
|
|
"The test works on Python 3.")
|
|
|
|
def test_regression_non_parent_init_called_tracemalloc(self):
|
|
|
|
# This used to raise a non-parent-init-called on Pylint 1.3
|
|
|
|
# See issue https://bitbucket.org/logilab/pylint/issue/308/
|
|
|
|
# for reference.
|
|
|
|
node = test_utils.extract_node("""
|
|
|
|
from tracemalloc import Sequence
|
|
|
|
class _Traces(Sequence):
|
|
|
|
def __init__(self, traces): #@
|
|
|
|
Sequence.__init__(self)
|
|
|
|
""")
|
|
|
|
with self.assertNoMessages():
|
|
|
|
self.checker.visit_function(node)
|
|
|
|
|
2015-02-09 20:39:08 +00:00
|
|
|
def test_super_init_not_called_regression(self):
|
|
|
|
# This should not emit a super-init-not-called
|
|
|
|
# warning. It previously did this, because
|
|
|
|
# ``next(node.infer())`` was used in that checker's
|
|
|
|
# logic and the first inferred node was an YES object,
|
|
|
|
# leading to this false positive.
|
|
|
|
node = test_utils.extract_node("""
|
|
|
|
import ctypes
|
|
|
|
|
|
|
|
class Foo(ctypes.BigEndianStructure):
|
|
|
|
def __init__(self): #@
|
|
|
|
ctypes.BigEndianStructure.__init__(self)
|
|
|
|
""")
|
|
|
|
with self.assertNoMessages():
|
|
|
|
self.checker.visit_function(node)
|
|
|
|
|
2014-10-28 10:24:18 +00:00
|
|
|
|
2014-07-26 15:11:14 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|