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

Commit99e852c

Browse files
gh-99341: Cover type ignore nodes when incrementing line numbers (GH-99422)
(cherry picked from commit1acdfec)Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
1 parent37dbbb2 commit99e852c

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
@@ -236,6 +236,12 @@ def increment_lineno(node, n=1):
236236
location in a file.
237237
"""
238238
forchildinwalk(node):
239+
# TypeIgnore is a special case where lineno is not an attribute
240+
# but rather a field of the node itself.
241+
ifisinstance(child,TypeIgnore):
242+
child.lineno=getattr(child,'lineno',0)+n
243+
continue
244+
239245
if'lineno'inchild._attributes:
240246
child.lineno=getattr(child,'lineno',0)+n
241247
if (

‎Lib/test/test_ast.py‎

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

917+
deftest_increment_lineno_on_module(self):
918+
src=ast.parse(dedent("""\
919+
a = 1
920+
b = 2 # type: ignore
921+
c = 3
922+
d = 4 # type: ignore@tag
923+
"""),type_comments=True)
924+
ast.increment_lineno(src,n=5)
925+
self.assertEqual(src.type_ignores[0].lineno,7)
926+
self.assertEqual(src.type_ignores[1].lineno,9)
927+
self.assertEqual(src.type_ignores[1].tag,'@tag')
928+
917929
deftest_iter_fields(self):
918930
node=ast.parse('foo()',mode='eval')
919931
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