from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)]):
self.pos_annotations = pos_annotations
def visit_Annotation(self, node: "Annotation") -> Optional[bool]:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append((pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ == '__main__':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = False
print_line = line + "\n"
for item in reversed(pos_annotations):
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
if pos_start_line < line_no < pos_end_line:
skip = True
break
elif pos_start_line == line_no == pos_end_line:
print_line = print_line[0:pos_start_column] + print_line[pos_end_column:]
continue
elif pos_start_line == line_no:
print_line = print_line[0:pos_start_column]
breakcontinue
elif pos_end_line == line_no:
print_line = print_line[pos_end_column:]
breakcontinue
if skip:
return
print(print_line, end='')
line_no = 1
for line in tree.code.splitlines():
print_no_annotation(pos_annotations, line_no, line)
line_no += 1
from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)]):
self.pos_annotations = pos_annotations
def visit_Annotation(self, node: "Annotation") -> Optional[bool]:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append((pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ == '__main__':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = False
print_line = line + "\n"
for item in reversed(pos_annotations):
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
if pos_start_line < line_no < pos_end_line:
skip = True
break
elif pos_start_line == line_no == pos_end_line:
print_line = print_line[0:pos_start_column] + print_line[pos_end_column:]
continue
elif pos_start_line == line_no:
print_line = print_line[0:pos_start_column]
break
elif pos_end_line == line_no:
print_line = print_line[pos_end_column:]
break
if skip:
return
print(print_line, end='')
line_no = 1
for line in tree.code.splitlines():
print_no_annotation(pos_annotations, line_no, line)
line_no += 1
from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)]):
self.pos_annotations = pos_annotations
def visit_Annotation(self, node: "Annotation") -> Optional[bool]:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append((pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ == '__main__':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = False
print_line = line + "\n"
for item in reversed(pos_annotations):
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
if pos_start_line < line_no < pos_end_line:
skip = True
break
elif pos_start_line == line_no == pos_end_line:
print_line = print_line[0:pos_start_column] + print_line[pos_end_column:]
continue
elif pos_start_line == line_no:
print_line = print_line[0:pos_start_column]
continue
elif pos_end_line == line_no:
print_line = print_line[pos_end_column:]
continue
if skip:
return
print(print_line, end='')
line_no = 1
for line in tree.code.splitlines():
print_no_annotation(pos_annotations, line_no, line)
line_no += 1
#from coding:typing utf-8import Optional
defimport hoge()libcst ->as str:cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class printPrinter('Hello World'cst.CSTVisitor)
: # コメントType Hintsの開始行桁、終了行桁を取得するVisitor
#fuga1 = 'Guido'
#fuga2: str METADATA_DEPENDENCIES = 'Guido'(cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
fuga3: str |\
pos_annotations: list[(int, int, int, int)] = [] None |\
# Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
intdef =__init__(self, 'Guido'
piyopos_annotations: list[int] = [1(int, 9int, 9int, 1int)]):
self.pos_annotations = pos_annotations
def foovisit_Annotation(hoge: strself, fuganode: int"Annotation") -> Optional[bool]:
print pos_start = self.get_metadata(hogecst.metadata.WhitespaceInclusivePositionProvider, fuganode).start
pos_end = self.get_metadata(cst.metadata.
foo("hhh"WhitespaceInclusivePositionProvider, 123node).end
対象ファイルの例
# coding: utf-8
def hoge() -> str:
printself.pos_annotations.append('Hello(pos_start.line, World'pos_start.column, pos_end.line, pos_end.column))
# コメント
#fuga1if __name__ == 'Guido'
#fuga2__main__':
str = 'Guido'
fuga3: strfile_path |\= "対象ファイル.py"
tree = cst.parse_module(open(file_path, Noneencoding="utf-8").read()) |\
# 対象ファイルのCSTツリー
intwrapper = 'Guido'cst.metadata.MetadataWrapper(tree)
piyo: list[int] = pos_annotations: list[1(int, 9int, 9int, 1int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def fooprint_no_annotation(hogepos_annotations: strlist[(int, fugaint, int, int)], line_no: int, line: int):
print(hoge, fuga)
skip ...
= False
foo( print_line = line + "hhh;\n", 123)
実行結果
# coding: utf-8
def hoge for item in reversed(pos_annotations):
print('Hello World')
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
# コメント if pos_start_line < line_no < pos_end_line:
#fuga1 skip = 'Guido'True
#fuga2 break
elif pos_start_line == line_no == pos_end_line:
str print_line = 'Guido'print_line[0:pos_start_column] + print_line[pos_end_column:]
fuga3 continue
elif pos_start_line == 'Guido'line_no:
piyo print_line = print_line[1,0:pos_start_column]
9, 9, 1 break
elif pos_end_line == line_no:
print_line = print_line[pos_end_column:]
break
def foo(hoge, fuga) if skip:
return
print(hogeprint_line, fugaend='')
.line_no = 1
for line in tree.code.
splitlines():
foo print_no_annotation("hhh"pos_annotations, 123line_no, line)
line_no += 1
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
def foo(hoge: str, fuga: int):
print(hoge, fuga)
...
foo("hhh", 123)
対象ファイルの例
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
def foo(hoge: str, fuga: int):
print(hoge, fuga)
...
foo("hhh", 123)
実行結果
# coding: utf-8
def hoge():
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3 = 'Guido'
piyo = [1, 9, 9, 1]
def foo(hoge, fuga):
print(hoge, fuga)
...
foo("hhh", 123)
from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)]):
self.pos_annotations = pos_annotations
def visit_Annotation(self, node: "Annotation") -> Optional[bool]:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append((pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ == '__main__':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = False
print_line = line + "\n"
for item in reversed(pos_annotations):
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
if pos_start_line < line_no < pos_end_line:
skip = True
break
elif pos_start_line == line_no == pos_end_line:
print_line = print_line[0:pos_start_column] + print_line[pos_end_column:]
continue
elif pos_start_line == line_no:
print_line = print_line[0:pos_start_column]
break
elif pos_end_line == line_no:
print_line = print_line[pos_end_column:]
break
if skip:
return
print(print_line, end='')
line_no = 1
for line in tree.code.splitlines():
print_no_annotation(pos_annotations, line_no, line)
line_no += 1
コード例 ※修正版
同一行に複数のType Hints
があるとき、2つ目以降のType Hints
が除去されないバグに対応しました。主な修正点は以下です。
- 修正した行を対象としてさらに
Type Hints
を削除する。 Type Hints
を削除すると桁位置が変わるので、Type Hintsの開始行桁、終了行桁を逆順に処理する。
from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)])coding:
self.pos_annotations = pos_annotations
utf-8
def visit_Annotationhoge(self, node: "Annotation") -> Optional[bool]str:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append(print(pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ =='Hello '__main__World':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = Falseコメント
print_line#fuga1 = line + '\n'Guido'
for item in pos_annotations#fuga2:
pos_start_line, pos_start_column, pos_end_line, pos_end_columnstr = item'Guido'
if pos_start_line < line_no < pos_end_linefuga3:
skip =str True|\
None break|\
elif pos_start_line == line_noint == pos_end_line:'Guido'
print_line = line[0piyo:pos_start_column] + line[pos_end_column:list[int] + "\n"
break
elif pos_start_line == line_no:
[1, print_line9, =9, line[0:pos_start_column1]
break
elif pos_end_line ==def line_nofoo(hoge:
print_line =str, line[pos_end_columnfuga:] + "\n"
break
if skipint):
return
print(print_linehoge, end=''fuga)
line_no = 1
for line in tree.code.splitlines():.
print_no_annotation
foo(pos_annotations, line_no"hhh", line)
line_no += 123)
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
def foo(hoge: str, fuga: int):
print(hoge, fuga)
...
foo("hhh", 123)
# coding: utf-8
def hoge():
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3 = 'Guido'
piyo = [1, 9, 9, 1]
def foo(hoge, fuga):
print(hoge, fuga)
...
foo("hhh", 123)
コード例
from typing import Optional
import libcst as cst
from libcst.metadata import WhitespaceInclusivePositionProvider
class Printer(cst.CSTVisitor): # Type Hintsの開始行桁、終了行桁を取得するVisitor
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider, WhitespaceInclusivePositionProvider)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
def __init__(self, pos_annotations: list[(int, int, int, int)]):
self.pos_annotations = pos_annotations
def visit_Annotation(self, node: "Annotation") -> Optional[bool]:
pos_start = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).start
pos_end = self.get_metadata(cst.metadata.WhitespaceInclusivePositionProvider, node).end
self.pos_annotations.append((pos_start.line, pos_start.column, pos_end.line, pos_end.column))
if __name__ == '__main__':
file_path = "対象ファイル.py"
tree = cst.parse_module(open(file_path, encoding="utf-8").read()) # 対象ファイルのCSTツリー
wrapper = cst.metadata.MetadataWrapper(tree)
pos_annotations: list[(int, int, int, int)] = [] # Type Hintsの開始行、開始行桁、終了行、終了桁のリスト
result = wrapper.visit(Printer(pos_annotations))
def print_no_annotation(pos_annotations: list[(int, int, int, int)], line_no: int, line: int):
skip = False
print_line = line + '\n'
for item in pos_annotations:
pos_start_line, pos_start_column, pos_end_line, pos_end_column = item
if pos_start_line < line_no < pos_end_line:
skip = True
break
elif pos_start_line == line_no == pos_end_line:
print_line = line[0:pos_start_column] + line[pos_end_column:] + "\n"
break
elif pos_start_line == line_no:
print_line = line[0:pos_start_column]
break
elif pos_end_line == line_no:
print_line = line[pos_end_column:] + "\n"
break
if skip:
return
print(print_line, end='')
line_no = 1
for line in tree.code.splitlines():
print_no_annotation(pos_annotations, line_no, line)
line_no += 1
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
# coding: utf-8
def hoge():
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3 = 'Guido'
piyo = [1, 9, 9, 1]
コード例 ※修正版
同一行に複数のType Hints
があるとき、2つ目以降のType Hints
が除去されないバグに対応しました。主な修正点は以下です。
- 修正した行を対象としてさらに
Type Hints
を削除する。 Type Hints
を削除すると桁位置が変わるので、Type Hintsの開始行桁、終了行桁を逆順に処理する。
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
def foo(hoge: str, fuga: int):
print(hoge, fuga)
...
foo("hhh", 123)
# coding: utf-8
def hoge() -> str:
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3: str |\
None |\
int = 'Guido'
piyo: list[int] = [1, 9, 9, 1]
def foo(hoge: str, fuga: int):
print(hoge, fuga)
...
foo("hhh", 123)
# coding: utf-8
def hoge():
print('Hello World')
# コメント
#fuga1 = 'Guido'
#fuga2: str = 'Guido'
fuga3 = 'Guido'
piyo = [1, 9, 9, 1]
def foo(hoge, fuga):
print(hoge, fuga)
...
foo("hhh", 123)
読み込んでいます
lang-py