|
1 | 1 | import textwrap
|
2 | 2 |
|
3 | 3 | from pyflakes import messages as m
|
| 4 | +from pyflakes.checker import ( |
| 5 | + DoctestScope, |
| 6 | + FunctionScope, |
| 7 | + ModuleScope, |
| 8 | +) |
4 | 9 | from pyflakes.test.test_other import Test as TestOther
|
5 | 10 | from pyflakes.test.test_imports import Test as TestImports
|
6 | 11 | from pyflakes.test.test_undefined_names import Test as TestUndefinedNames
|
@@ -42,6 +47,142 @@ class Test(TestCase):
|
42 | 47 |
|
43 | 48 | withDoctest = True
|
44 | 49 |
|
| 50 | + def test_scope_class(self): |
| 51 | + """Check that a doctest is given a DoctestScope.""" |
| 52 | + checker = self.flakes(""" |
| 53 | + m = None |
| 54 | +
|
| 55 | + def doctest_stuff(): |
| 56 | + ''' |
| 57 | + >>> d = doctest_stuff() |
| 58 | + ''' |
| 59 | + f = m |
| 60 | + return f |
| 61 | + """) |
| 62 | + |
| 63 | + scopes = checker.deadScopes |
| 64 | + module_scopes = [ |
| 65 | + scope for scope in scopes if scope.__class__ is ModuleScope] |
| 66 | + doctest_scopes = [ |
| 67 | + scope for scope in scopes if scope.__class__ is DoctestScope] |
| 68 | + function_scopes = [ |
| 69 | + scope for scope in scopes if scope.__class__ is FunctionScope] |
| 70 | + |
| 71 | + self.assertEqual(len(module_scopes), 1) |
| 72 | + self.assertEqual(len(doctest_scopes), 1) |
| 73 | + |
| 74 | + module_scope = module_scopes[0] |
| 75 | + doctest_scope = doctest_scopes[0] |
| 76 | + |
| 77 | + self.assertIsInstance(doctest_scope, DoctestScope) |
| 78 | + self.assertIsInstance(doctest_scope, ModuleScope) |
| 79 | + self.assertNotIsInstance(doctest_scope, FunctionScope) |
| 80 | + self.assertNotIsInstance(module_scope, DoctestScope) |
| 81 | + |
| 82 | + self.assertIn('m', module_scope) |
| 83 | + self.assertIn('doctest_stuff', module_scope) |
| 84 | + |
| 85 | + self.assertIn('d', doctest_scope) |
| 86 | + |
| 87 | + self.assertEqual(len(function_scopes), 1) |
| 88 | + self.assertIn('f', function_scopes[0]) |
| 89 | + |
| 90 | + def test_nested_doctest_ignored(self): |
| 91 | + """Check that nested doctests are ignored.""" |
| 92 | + checker = self.flakes(""" |
| 93 | + m = None |
| 94 | +
|
| 95 | + def doctest_stuff(): |
| 96 | + ''' |
| 97 | + >>> def function_in_doctest(): |
| 98 | + ... \"\"\" |
| 99 | + ... >>> ignored_undefined_name |
| 100 | + ... \"\"\" |
| 101 | + ... df = m |
| 102 | + ... return df |
| 103 | + ... |
| 104 | + >>> function_in_doctest() |
| 105 | + ''' |
| 106 | + f = m |
| 107 | + return f |
| 108 | + """) |
| 109 | + |
| 110 | + scopes = checker.deadScopes |
| 111 | + module_scopes = [ |
| 112 | + scope for scope in scopes if scope.__class__ is ModuleScope] |
| 113 | + doctest_scopes = [ |
| 114 | + scope for scope in scopes if scope.__class__ is DoctestScope] |
| 115 | + function_scopes = [ |
| 116 | + scope for scope in scopes if scope.__class__ is FunctionScope] |
| 117 | + |
| 118 | + self.assertEqual(len(module_scopes), 1) |
| 119 | + self.assertEqual(len(doctest_scopes), 1) |
| 120 | + |
| 121 | + module_scope = module_scopes[0] |
| 122 | + doctest_scope = doctest_scopes[0] |
| 123 | + |
| 124 | + self.assertIn('m', module_scope) |
| 125 | + self.assertIn('doctest_stuff', module_scope) |
| 126 | + self.assertIn('function_in_doctest', doctest_scope) |
| 127 | + |
| 128 | + self.assertEqual(len(function_scopes), 2) |
| 129 | + |
| 130 | + self.assertIn('f', function_scopes[0]) |
| 131 | + self.assertIn('df', function_scopes[1]) |
| 132 | + |
| 133 | + def test_global_module_scope_pollution(self): |
| 134 | + """Check that global in doctest does not pollute module scope.""" |
| 135 | + checker = self.flakes(""" |
| 136 | + def doctest_stuff(): |
| 137 | + ''' |
| 138 | + >>> def function_in_doctest(): |
| 139 | + ... global m |
| 140 | + ... m = 50 |
| 141 | + ... df = 10 |
| 142 | + ... m = df |
| 143 | + ... |
| 144 | + >>> function_in_doctest() |
| 145 | + ''' |
| 146 | + f = 10 |
| 147 | + return f |
| 148 | +
|
| 149 | + """) |
| 150 | + |
| 151 | + scopes = checker.deadScopes |
| 152 | + module_scopes = [ |
| 153 | + scope for scope in scopes if scope.__class__ is ModuleScope] |
| 154 | + doctest_scopes = [ |
| 155 | + scope for scope in scopes if scope.__class__ is DoctestScope] |
| 156 | + function_scopes = [ |
| 157 | + scope for scope in scopes if scope.__class__ is FunctionScope] |
| 158 | + |
| 159 | + self.assertEqual(len(module_scopes), 1) |
| 160 | + self.assertEqual(len(doctest_scopes), 1) |
| 161 | + |
| 162 | + module_scope = module_scopes[0] |
| 163 | + doctest_scope = doctest_scopes[0] |
| 164 | + |
| 165 | + self.assertIn('doctest_stuff', module_scope) |
| 166 | + self.assertIn('function_in_doctest', doctest_scope) |
| 167 | + |
| 168 | + self.assertEqual(len(function_scopes), 2) |
| 169 | + |
| 170 | + self.assertIn('f', function_scopes[0]) |
| 171 | + self.assertIn('df', function_scopes[1]) |
| 172 | + self.assertIn('m', function_scopes[1]) |
| 173 | + |
| 174 | + self.assertNotIn('m', module_scope) |
| 175 | + |
| 176 | + def test_global_undefined(self): |
| 177 | + self.flakes(""" |
| 178 | + global m |
| 179 | +
|
| 180 | + def doctest_stuff(): |
| 181 | + ''' |
| 182 | + >>> m |
| 183 | + ''' |
| 184 | + """, m.UndefinedName) |
| 185 | + |
45 | 186 | def test_importBeforeDoctest(self):
|
46 | 187 | self.flakes("""
|
47 | 188 | import foo
|
|
0 commit comments