test_code_segment.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from pyflakes import messages as m
  2. from pyflakes.checker import (FunctionScope, ClassScope, ModuleScope,
  3. Argument, FunctionDefinition, Assignment)
  4. from pyflakes.test.harness import TestCase
  5. class TestCodeSegments(TestCase):
  6. """
  7. Tests for segments of a module
  8. """
  9. def test_function_segment(self):
  10. self.flakes('''
  11. def foo():
  12. def bar():
  13. pass
  14. ''', is_segment=True)
  15. self.flakes('''
  16. def foo():
  17. def bar():
  18. x = 0
  19. ''', m.UnusedVariable, is_segment=True)
  20. def test_class_segment(self):
  21. self.flakes('''
  22. class Foo:
  23. class Bar:
  24. pass
  25. ''', is_segment=True)
  26. self.flakes('''
  27. class Foo:
  28. def bar():
  29. x = 0
  30. ''', m.UnusedVariable, is_segment=True)
  31. def test_scope_class(self):
  32. checker = self.flakes('''
  33. class Foo:
  34. x = 0
  35. def bar(a, b=1, *d, **e):
  36. pass
  37. ''', is_segment=True)
  38. scopes = checker.deadScopes
  39. module_scopes = [
  40. scope for scope in scopes if scope.__class__ is ModuleScope]
  41. class_scopes = [
  42. scope for scope in scopes if scope.__class__ is ClassScope]
  43. function_scopes = [
  44. scope for scope in scopes if scope.__class__ is FunctionScope]
  45. # Ensure module scope is not present because we are analysing
  46. # the inner contents of Foo
  47. self.assertEqual(len(module_scopes), 0)
  48. self.assertEqual(len(class_scopes), 1)
  49. self.assertEqual(len(function_scopes), 1)
  50. class_scope = class_scopes[0]
  51. function_scope = function_scopes[0]
  52. self.assertIsInstance(class_scope, ClassScope)
  53. self.assertIsInstance(function_scope, FunctionScope)
  54. self.assertIn('x', class_scope)
  55. self.assertIn('bar', class_scope)
  56. self.assertIn('a', function_scope)
  57. self.assertIn('b', function_scope)
  58. self.assertIn('d', function_scope)
  59. self.assertIn('e', function_scope)
  60. self.assertIsInstance(class_scope['bar'], FunctionDefinition)
  61. self.assertIsInstance(class_scope['x'], Assignment)
  62. self.assertIsInstance(function_scope['a'], Argument)
  63. self.assertIsInstance(function_scope['b'], Argument)
  64. self.assertIsInstance(function_scope['d'], Argument)
  65. self.assertIsInstance(function_scope['e'], Argument)
  66. def test_scope_function(self):
  67. checker = self.flakes('''
  68. def foo(a, b=1, *d, **e):
  69. def bar(f, g=1, *h, **i):
  70. pass
  71. ''', is_segment=True)
  72. scopes = checker.deadScopes
  73. module_scopes = [
  74. scope for scope in scopes if scope.__class__ is ModuleScope]
  75. function_scopes = [
  76. scope for scope in scopes if scope.__class__ is FunctionScope]
  77. # Ensure module scope is not present because we are analysing
  78. # the inner contents of foo
  79. self.assertEqual(len(module_scopes), 0)
  80. self.assertEqual(len(function_scopes), 2)
  81. function_scope_foo = function_scopes[1]
  82. function_scope_bar = function_scopes[0]
  83. self.assertIsInstance(function_scope_foo, FunctionScope)
  84. self.assertIsInstance(function_scope_bar, FunctionScope)
  85. self.assertIn('a', function_scope_foo)
  86. self.assertIn('b', function_scope_foo)
  87. self.assertIn('d', function_scope_foo)
  88. self.assertIn('e', function_scope_foo)
  89. self.assertIn('bar', function_scope_foo)
  90. self.assertIn('f', function_scope_bar)
  91. self.assertIn('g', function_scope_bar)
  92. self.assertIn('h', function_scope_bar)
  93. self.assertIn('i', function_scope_bar)
  94. self.assertIsInstance(function_scope_foo['bar'], FunctionDefinition)
  95. self.assertIsInstance(function_scope_foo['a'], Argument)
  96. self.assertIsInstance(function_scope_foo['b'], Argument)
  97. self.assertIsInstance(function_scope_foo['d'], Argument)
  98. self.assertIsInstance(function_scope_foo['e'], Argument)
  99. self.assertIsInstance(function_scope_bar['f'], Argument)
  100. self.assertIsInstance(function_scope_bar['g'], Argument)
  101. self.assertIsInstance(function_scope_bar['h'], Argument)
  102. self.assertIsInstance(function_scope_bar['i'], Argument)
  103. def test_scope_async_function(self):
  104. self.flakes('async def foo(): pass', is_segment=True)