]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/pyflakes/test/test_doctests.py
3 from pyflakes
import messages
as m
4 from pyflakes
.checker
import (
10 from pyflakes
.test
.test_other
import Test
as TestOther
11 from pyflakes
.test
.test_imports
import Test
as TestImports
12 from pyflakes
.test
.test_undefined_names
import Test
as TestUndefinedNames
13 from pyflakes
.test
.harness
import TestCase
, skip
20 def doctestify(self
, input):
22 for line
in textwrap
.dedent(input).splitlines():
23 if line
.strip() == '':
25 elif (line
.startswith(' ') or
26 line
.startswith('except:') or
27 line
.startswith('except ') or
28 line
.startswith('finally:') or
29 line
.startswith('else:') or
30 line
.startswith('elif ') or
31 (lines
and lines
[-1].startswith(('>>> @', '... @')))):
32 line
= "... %s" % line
34 line
= ">>> %s" % line
36 doctestificator
= textwrap
.dedent('''\
37 def doctest_something():
42 return doctestificator
% "\n ".join(lines
)
44 def flakes(self
, input, *args
, **kw
):
45 return super().flakes(self
.doctestify(input), *args
, **kw
)
52 def test_scope_class(self
):
53 """Check that a doctest is given a DoctestScope."""
54 checker
= self
.flakes("""
59 >>> d = doctest_stuff()
65 scopes
= checker
.deadScopes
67 scope
for scope
in scopes
if scope
.__class
__ is ModuleScope
]
69 scope
for scope
in scopes
if scope
.__class
__ is DoctestScope
]
71 scope
for scope
in scopes
if scope
.__class
__ is FunctionScope
]
73 self
.assertEqual(len(module_scopes
), 1)
74 self
.assertEqual(len(doctest_scopes
), 1)
76 module_scope
= module_scopes
[0]
77 doctest_scope
= doctest_scopes
[0]
79 self
.assertIsInstance(doctest_scope
, DoctestScope
)
80 self
.assertIsInstance(doctest_scope
, ModuleScope
)
81 self
.assertNotIsInstance(doctest_scope
, FunctionScope
)
82 self
.assertNotIsInstance(module_scope
, DoctestScope
)
84 self
.assertIn('m', module_scope
)
85 self
.assertIn('doctest_stuff', module_scope
)
87 self
.assertIn('d', doctest_scope
)
89 self
.assertEqual(len(function_scopes
), 1)
90 self
.assertIn('f', function_scopes
[0])
92 def test_nested_doctest_ignored(self
):
93 """Check that nested doctests are ignored."""
94 checker
= self
.flakes("""
99 >>> def function_in_doctest():
101 ... >>> ignored_undefined_name
106 >>> function_in_doctest()
112 scopes
= checker
.deadScopes
114 scope
for scope
in scopes
if scope
.__class
__ is ModuleScope
]
116 scope
for scope
in scopes
if scope
.__class
__ is DoctestScope
]
118 scope
for scope
in scopes
if scope
.__class
__ is FunctionScope
]
120 self
.assertEqual(len(module_scopes
), 1)
121 self
.assertEqual(len(doctest_scopes
), 1)
123 module_scope
= module_scopes
[0]
124 doctest_scope
= doctest_scopes
[0]
126 self
.assertIn('m', module_scope
)
127 self
.assertIn('doctest_stuff', module_scope
)
128 self
.assertIn('function_in_doctest', doctest_scope
)
130 self
.assertEqual(len(function_scopes
), 2)
132 self
.assertIn('f', function_scopes
[0])
133 self
.assertIn('df', function_scopes
[1])
135 def test_global_module_scope_pollution(self
):
136 """Check that global in doctest does not pollute module scope."""
137 checker
= self
.flakes("""
140 >>> def function_in_doctest():
146 >>> function_in_doctest()
153 scopes
= checker
.deadScopes
155 scope
for scope
in scopes
if scope
.__class
__ is ModuleScope
]
157 scope
for scope
in scopes
if scope
.__class
__ is DoctestScope
]
159 scope
for scope
in scopes
if scope
.__class
__ is FunctionScope
]
161 self
.assertEqual(len(module_scopes
), 1)
162 self
.assertEqual(len(doctest_scopes
), 1)
164 module_scope
= module_scopes
[0]
165 doctest_scope
= doctest_scopes
[0]
167 self
.assertIn('doctest_stuff', module_scope
)
168 self
.assertIn('function_in_doctest', doctest_scope
)
170 self
.assertEqual(len(function_scopes
), 2)
172 self
.assertIn('f', function_scopes
[0])
173 self
.assertIn('df', function_scopes
[1])
174 self
.assertIn('m', function_scopes
[1])
176 self
.assertNotIn('m', module_scope
)
178 def test_global_undefined(self
):
186 """, m
.UndefinedName
)
188 def test_nested_class(self
):
189 """Doctest within nested class are processed."""
196 def doctest_stuff(self):
201 """, m
.UndefinedName
, m
.UndefinedName
)
203 def test_ignore_nested_function(self
):
204 """Doctest module does not process doctest in nested functions."""
205 # 'syntax error' would cause a SyntaxError if the doctest was processed.
206 # However doctest does not find doctest in nested functions
207 # (https://bugs.python.org/issue1650090). If nested functions were
208 # processed, this use of m should cause UndefinedName, and the
209 # name inner_function should probably exist in the doctest scope.
212 def inner_function():
224 def test_inaccessible_scope_class(self
):
225 """Doctest may not access class scope."""
228 def doctest_stuff(self):
234 """, m
.UndefinedName
)
236 def test_importBeforeDoctest(self
):
247 def test_importBeforeAndInDoctest(self
):
258 ''', m
.RedefinedWhileUnused
)
260 def test_importInDoctestAndAfter(self
):
272 def test_offsetInDoctests(self
):
273 exc
= self
.flakes('''
280 ''', m
.UndefinedName
).messages
[0]
281 self
.assertEqual(exc
.lineno
, 5)
282 self
.assertEqual(exc
.col
, 12)
284 def test_offsetInLambdasInDoctests(self
):
285 exc
= self
.flakes('''
289 >>> lambda: x # line 5
292 ''', m
.UndefinedName
).messages
[0]
293 self
.assertEqual(exc
.lineno
, 5)
294 self
.assertEqual(exc
.col
, 20)
296 def test_offsetAfterDoctests(self
):
297 exc
= self
.flakes('''
306 ''', m
.UndefinedName
).messages
[0]
307 self
.assertEqual(exc
.lineno
, 8)
308 self
.assertEqual(exc
.col
, 0)
310 def test_syntaxErrorInDoctest(self
):
311 exceptions
= self
.flakes(
317 >>> except Exception:
320 m
.DoctestSyntaxError
,
321 m
.DoctestSyntaxError
,
322 m
.DoctestSyntaxError
).messages
324 self
.assertEqual(exc
.lineno
, 4)
326 self
.assertEqual(exc
.col
, 18)
328 self
.assertEqual(exc
.col
, 26)
330 # PyPy error column offset is 0,
331 # for the second and third line of the doctest
332 # i.e. at the beginning of the line
334 self
.assertEqual(exc
.lineno
, 5)
336 self
.assertEqual(exc
.col
, 13)
338 self
.assertEqual(exc
.col
, 16)
340 self
.assertEqual(exc
.lineno
, 6)
341 self
.assertEqual(exc
.col
, 13)
343 def test_indentationErrorInDoctest(self
):
344 exc
= self
.flakes('''
350 ''', m
.DoctestSyntaxError
).messages
[0]
351 self
.assertEqual(exc
.lineno
, 5)
352 self
.assertEqual(exc
.col
, 13)
354 def test_offsetWithMultiLineArgs(self
):
355 (exc1
, exc2
) = self
.flakes(
357 def doctest_stuff(arg1,
365 m
.DoctestSyntaxError
,
366 m
.UndefinedName
).messages
367 self
.assertEqual(exc1
.lineno
, 6)
368 self
.assertEqual(exc1
.col
, 19)
369 self
.assertEqual(exc2
.lineno
, 7)
370 self
.assertEqual(exc2
.col
, 12)
372 def test_doctestCanReferToFunction(self
):
380 def test_doctestCanReferToClass(self
):
392 def test_noOffsetSyntaxErrorInDoctest(self
):
393 exceptions
= self
.flakes(
395 def buildurl(base, *args, **kwargs):
397 >>> buildurl('/blah.php', ('a', '&'), ('b', '=')
398 '/blah.php?a=%26&b=%3D'
399 >>> buildurl('/blah.php', a='&', 'b'='=')
400 '/blah.php?b=%3D&a=%26'
404 m
.DoctestSyntaxError
,
405 m
.DoctestSyntaxError
).messages
407 self
.assertEqual(exc
.lineno
, 4)
409 self
.assertEqual(exc
.lineno
, 6)
411 def test_singleUnderscoreInDoctest(self
):
424 def test_globalUnderscoreInDoctest(self
):
426 from gettext import ugettext as _
435 class TestOther(_DoctestMixin
, TestOther
):
436 """Run TestOther with each test wrapped in a doctest."""
439 class TestImports(_DoctestMixin
, TestImports
):
440 """Run TestImports with each test wrapped in a doctest."""
443 class TestUndefinedNames(_DoctestMixin
, TestUndefinedNames
):
444 """Run TestUndefinedNames with each test wrapped in a doctest."""