Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_checker_classes.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2014 Google, Inc.
  3. # Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  4. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  5. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  6. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  8. """Unit tests for the variables checker."""
  9. import sys
  10. import pytest
  11. import astroid
  12. from pylint.checkers import classes
  13. from pylint.testutils import CheckerTestCase, Message, set_config
  14. class TestVariablesChecker(CheckerTestCase):
  15. CHECKER_CLASS = classes.ClassChecker
  16. def test_bitbucket_issue_164(self):
  17. """Issue 164 report a false negative for access-member-before-definition"""
  18. n1, n2 = astroid.extract_node("""
  19. class MyClass1(object):
  20. def __init__(self):
  21. self.first += 5 #@
  22. self.first = 0 #@
  23. """)
  24. message = Message('access-member-before-definition',
  25. node=n1.target, args=('first', n2.lineno))
  26. with self.assertAddsMessages(message):
  27. self.walk(n1.root())
  28. @set_config(exclude_protected=('_meta', '_manager'))
  29. def test_exclude_protected(self):
  30. """Test that exclude-protected can be used to
  31. exclude names from protected-access warning.
  32. """
  33. node = astroid.parse("""
  34. class Protected(object):
  35. '''empty'''
  36. def __init__(self):
  37. self._meta = 42
  38. self._manager = 24
  39. self._teta = 29
  40. OBJ = Protected()
  41. OBJ._meta
  42. OBJ._manager
  43. OBJ._teta
  44. """)
  45. with self.assertAddsMessages(
  46. Message('protected-access',
  47. node=node.body[-1].value,
  48. args='_teta')):
  49. self.walk(node.root())
  50. @pytest.mark.skipif(sys.version_info[0] != 3,
  51. reason="The test works on Python 3.")
  52. def test_regression_non_parent_init_called_tracemalloc(self):
  53. # This used to raise a non-parent-init-called on Pylint 1.3
  54. # See issue https://bitbucket.org/logilab/pylint/issue/308/
  55. # for reference.
  56. node = astroid.extract_node("""
  57. from tracemalloc import Sequence
  58. class _Traces(Sequence):
  59. def __init__(self, traces): #@
  60. Sequence.__init__(self)
  61. """)
  62. with self.assertNoMessages():
  63. self.checker.visit_functiondef(node)
  64. def test_super_init_not_called_regression(self):
  65. # This should not emit a super-init-not-called
  66. # warning. It previously did this, because
  67. # ``next(node.infer())`` was used in that checker's
  68. # logic and the first inferred node was an YES object,
  69. # leading to this false positive.
  70. node = astroid.extract_node("""
  71. import ctypes
  72. class Foo(ctypes.BigEndianStructure):
  73. def __init__(self): #@
  74. ctypes.BigEndianStructure.__init__(self)
  75. """)
  76. with self.assertNoMessages():
  77. self.checker.visit_functiondef(node)