Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1acdfec

Browse files
authored
gh-99341: Cover type ignore nodes when incrementing line numbers (GH-99422)
1 parentbc3a11d commit1acdfec

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

‎Lib/ast.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ def increment_lineno(node, n=1):
237237
location in a file.
238238
"""
239239
forchildinwalk(node):
240+
# TypeIgnore is a special case where lineno is not an attribute
241+
# but rather a field of the node itself.
242+
ifisinstance(child,TypeIgnore):
243+
child.lineno=getattr(child,'lineno',0)+n
244+
continue
245+
240246
if'lineno'inchild._attributes:
241247
child.lineno=getattr(child,'lineno',0)+n
242248
if (

‎Lib/test/test_ast.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,18 @@ def test_increment_lineno(self):
10361036
self.assertEqual(ast.increment_lineno(src).lineno,2)
10371037
self.assertIsNone(ast.increment_lineno(src).end_lineno)
10381038

1039+
deftest_increment_lineno_on_module(self):
1040+
src=ast.parse(dedent("""\
1041+
a = 1
1042+
b = 2 # type: ignore
1043+
c = 3
1044+
d = 4 # type: ignore@tag
1045+
"""),type_comments=True)
1046+
ast.increment_lineno(src,n=5)
1047+
self.assertEqual(src.type_ignores[0].lineno,7)
1048+
self.assertEqual(src.type_ignores[1].lineno,9)
1049+
self.assertEqual(src.type_ignores[1].tag,'@tag')
1050+
10391051
deftest_iter_fields(self):
10401052
node=ast.parse('foo()',mode='eval')
10411053
d=dict(ast.iter_fields(node.body))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix:func:`ast.increment_lineno` to also cover:class:`ast.TypeIgnore` when
2+
changing line numbers.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp