-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add DocstringRemover * fix test
- Loading branch information
Showing
7 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Transformer to remove all docstrings.""" | ||
|
||
from __future__ import annotations | ||
|
||
import ast | ||
from typing import Union | ||
|
||
from latexify import ast_utils | ||
|
||
|
||
class DocstringRemover(ast.NodeTransformer): | ||
"""NodeTransformer to remove all docstrings. | ||
Docstrings here are detected as Expr nodes with a single string constant. | ||
""" | ||
|
||
def visit_Expr(self, node: ast.Expr) -> Union[ast.Expr, None]: | ||
if ast_utils.is_str(node.value): | ||
return None | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Tests for latexify.transformers.docstring_remover.""" | ||
|
||
import ast | ||
|
||
from latexify import ast_utils, parser, test_utils | ||
from latexify.transformers.docstring_remover import DocstringRemover | ||
|
||
|
||
def test_remove_docstrings() -> None: | ||
def f(): | ||
"""Test docstring.""" | ||
x = 42 | ||
f() # This Expr should not be removed. | ||
"""This string constant should also be removed.""" | ||
return x | ||
|
||
tree = parser.parse_function(f).body[0] | ||
assert isinstance(tree, ast.FunctionDef) | ||
|
||
expected = ast.FunctionDef( | ||
name="f", | ||
body=[ | ||
ast.Assign( | ||
targets=[ast.Name(id="x", ctx=ast.Store())], | ||
value=ast_utils.make_constant(42), | ||
), | ||
ast.Expr(value=ast.Call(func=ast.Name(id="f", ctx=ast.Load()))), | ||
ast.Return(value=ast.Name(id="x", ctx=ast.Load())), | ||
], | ||
) | ||
transformed = DocstringRemover().visit(tree) | ||
test_utils.assert_ast_equal(transformed, expected) |