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_stdlib.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (c) 2015-2017 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2015 Cezar <celnazli@bitdefender.com>
  3. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  4. # Copyright (c) 2017 Martin <MartinBasti@users.noreply.github.com>
  5. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  7. import contextlib
  8. import astroid
  9. from pylint.checkers import stdlib
  10. from pylint.testutils import CheckerTestCase, Message
  11. from pylint.interfaces import UNDEFINED
  12. @contextlib.contextmanager
  13. def _add_transform(manager, node, transform, predicate=None):
  14. manager.register_transform(node, transform, predicate)
  15. try:
  16. yield
  17. finally:
  18. manager.unregister_transform(node, transform, predicate)
  19. class TestStdlibChecker(CheckerTestCase):
  20. CHECKER_CLASS = stdlib.StdlibChecker
  21. def test_deprecated_no_qname_on_unexpected_nodes(self):
  22. # Test that we don't crash on nodes which don't have
  23. # a qname method. While this test might seem weird since
  24. # it uses a transform, it's actually testing a crash that
  25. # happened in production, but there was no way to retrieve
  26. # the code for which this occurred (how an AssignAttr
  27. # got to be the result of a function inference
  28. # beats me..)
  29. def infer_func(node, context=None):
  30. new_node = astroid.AssignAttr()
  31. new_node.parent = node
  32. yield new_node
  33. manager = astroid.MANAGER
  34. transform = astroid.inference_tip(infer_func)
  35. with _add_transform(manager, astroid.Name, transform):
  36. node = astroid.extract_node('''
  37. call_something()
  38. ''')
  39. with self.assertNoMessages():
  40. self.checker.visit_call(node)
  41. def test_copy_environ(self):
  42. # shallow copy of os.environ should be reported
  43. node = astroid.extract_node("""
  44. import copy, os
  45. copy.copy(os.environ)
  46. """)
  47. with self.assertAddsMessages(
  48. Message(
  49. msg_id='shallow-copy-environ', node=node, confidence=UNDEFINED)
  50. ):
  51. self.checker.visit_call(node)
  52. def test_copy_environ_hidden(self):
  53. # shallow copy of os.environ should be reported
  54. # hide function names to be sure that checker is not just matching text
  55. node = astroid.extract_node("""
  56. from copy import copy as test_cp
  57. import os as o
  58. test_cp(o.environ)
  59. """)
  60. with self.assertAddsMessages(
  61. Message(
  62. msg_id='shallow-copy-environ', node=node, confidence=UNDEFINED)
  63. ):
  64. self.checker.visit_call(node)
  65. def test_copy_dict(self):
  66. # copy of dict is OK
  67. node = astroid.extract_node("""
  68. import copy
  69. test_dict = {}
  70. copy.copy(test_dict)
  71. """)
  72. with self.assertNoMessages():
  73. self.checker.visit_call(node)
  74. def test_copy_uninferable(self):
  75. # copy of uninferable object should not raise exception, nor make
  76. # the checker crash
  77. node = astroid.extract_node("""
  78. import copy
  79. from missing_library import MissingObject
  80. copy.copy(MissingObject)
  81. """)
  82. with self.assertNoMessages():
  83. self.checker.visit_call(node)
  84. def test_deepcopy_environ(self):
  85. # deepcopy of os.environ is OK
  86. node = astroid.extract_node("""
  87. import copy, os
  88. copy.deepcopy(os.environ)
  89. """)
  90. with self.assertNoMessages():
  91. self.checker.visit_call(node)