Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow docstring in function definition #126

Merged
merged 7 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/integration_tests/regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,23 @@ def solve(a, b):
r"a - b \mathclose{}\right) - a b"
)
_check_function(solve, latex)


def test_docstring_allowed() -> None:
def solve(x):
"""The identity function."""
return x

latex = r"\mathrm{solve}(x) = x"
_check_function(solve, latex)


def test_multiple_constants_allowed() -> None:
def solve(x):
"""The identity function."""
123
True
return x

latex = r"\mathrm{solve}(x) = x"
_check_function(solve, latex)
25 changes: 25 additions & 0 deletions src/latexify/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ def make_constant(value: Any) -> ast.expr:
raise ValueError(f"Unsupported type to generate Constant: {type(value).__name__}")


def is_constant(node: ast.AST) -> bool:
"""Checks if the node is a constant.

Args:
node: The node to examine.

Returns:
True if the node is a constant, False otherwise.
"""
if sys.version_info.minor < 8:
if isinstance(
node,
(ast.Bytes, ast.Constant, ast.Ellipsis, ast.NameConstant, ast.Num, ast.Str),
):
return True
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved
else:
if isinstance(node, ast.Constant):
return True
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(node, ast.Expr):
return is_constant(node.value)

ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved
return False
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved


def extract_int_or_none(node: ast.expr) -> int | None:
"""Extracts int constant from the given Constant node.

Expand Down
31 changes: 31 additions & 0 deletions src/latexify/ast_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ def test_make_constant_invalid() -> None:
ast_utils.make_constant(object())


@test_utils.require_at_most(7)
@pytest.mark.parametrize(
"value,expected",
[
(ast.Bytes(s=b"foo"), True),
(ast.Constant("bar"), True),
(ast.Ellipsis(), True),
(ast.NameConstant(value=None), True),
(ast.Num(n=123), True),
(ast.Str(s="baz"), True),
(ast.Expr(value=ast.Num(456)), True),
(ast.Global("qux"), False),
],
)
def test_is_constant_legacy(value: Any, expected: ast.Constant) -> None:
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved
assert ast_utils.is_constant(value) is expected


@test_utils.require_at_least(8)
@pytest.mark.parametrize(
"value,expected",
[
(ast.Constant("foo"), True),
(ast.Expr(value=ast.Constant(123)), True),
(ast.Global("bar"), False),
],
)
def test_is_constant(value: Any, expected: ast.Constant) -> None:
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved
assert ast_utils.is_constant(value) is expected


def test_extract_int_or_none() -> None:
assert ast_utils.extract_int_or_none(ast_utils.make_constant(-123)) == -123
assert ast_utils.extract_int_or_none(ast_utils.make_constant(0)) == 0
Expand Down
5 changes: 4 additions & 1 deletion src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
from typing import Any

from latexify import analyzers, constants, exceptions, math_symbols
from latexify import analyzers, ast_utils, constants, exceptions, math_symbols

# Precedences of operators for BoolOp, BinOp, UnaryOp, and Compare nodes.
# Note that this value affects only the appearance of surrounding parentheses for each
Expand Down Expand Up @@ -253,6 +253,9 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> str:

# Assignment statements (if any): x = ...
for child in node.body[:-1]:
if ast_utils.is_constant(child):
continue

if not isinstance(child, ast.Assign):
raise exceptions.LatexifyNotSupportedError(
"Codegen supports only Assign nodes in multiline functions, "
Expand Down
42 changes: 41 additions & 1 deletion src/latexify/codegen/function_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from latexify import exceptions, test_utils
from latexify import ast_utils, exceptions, test_utils
from latexify.codegen import FunctionCodegen, function_codegen


Expand Down Expand Up @@ -40,6 +40,46 @@ def test_visit_functiondef_use_signature() -> None:
assert FunctionCodegen(use_signature=True).visit(tree) == latex_with_flag


def test_visit_functiondef_ignore_docstring() -> None:
ZibingZhang marked this conversation as resolved.
Show resolved Hide resolved
tree = ast.FunctionDef(
name="f",
args=ast.arguments(
args=[ast.arg(arg="x")],
kwonlyargs=[],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on removing these empty fields from the tests? Would make it easier to read imo / remove some bloat

Copy link
Collaborator

@odashi odashi Nov 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using ast.parse as in some other tests?

tree = ast.parse(textwrap.dedent("""\
    def f(x):
        '''docstring'''
        return x
    """)).body[0]
assert isinstance(tree, ast.FunctionDef)

Copy link
Contributor Author

@ZibingZhang ZibingZhang Nov 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def test() -> None:
    tree = ast.parse(
        """
def f(x):
    '''docstring'''
    return x"""
    )

I've gone with this format. Unfortunately due to the nature of multiline strings it seems like the def can't have any preceding indentation, which makes it a bit awkward to read, but still better than before.

There can't be any space between the last non-whitespace character and the closing """.

I decided to opt against the starting \ because it doesn't actually add anything in terms of the test, and I felt that it's better to keep it simple for readability.

edit: could probably add this function into test_utils if it's commonly used, but since it's only two cases that can probably get held off

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably add this function into test_utils if it's commonly used

I think this is not recommended according to the DAMP principle. It is somewhat desirable to write self-contained tests:
https://abseil.io/resources/swe-book/html/ch12.html#tests_and_code_sharing_dampcomma_not_dr

kw_defaults=[],
defaults=[],
),
body=[
ast.Expr(ast_utils.make_constant("docstring")),
ast.Return(value=ast.Name(id="x", ctx=ast.Load())),
],
decorator_list=[],
)
latex = r"\mathrm{f}(x) = x"
assert FunctionCodegen().visit(tree) == latex


def test_visit_functiondef_ignore_multiple_constants() -> None:
tree = ast.FunctionDef(
name="f",
args=ast.arguments(
args=[ast.arg(arg="x")],
kwonlyargs=[],
kw_defaults=[],
defaults=[],
),
body=[
ast.Expr(ast_utils.make_constant("docstring" "")),
ast.Expr(ast_utils.make_constant(3)),
ast.Expr(ast_utils.make_constant(True)),
ast.Return(value=ast.Name(id="x", ctx=ast.Load())),
],
decorator_list=[],
)
latex = r"\mathrm{f}(x) = x"
assert FunctionCodegen().visit(tree) == latex


@pytest.mark.parametrize(
"code,latex",
[
Expand Down