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

Conversation

ZibingZhang
Copy link
Contributor

Overview

Allows a docstring to be defined at the top of the function.

Details

Docstrings (multiline strings) are represented as an ast.Expr(ast.Constant).
When validating that all child statements (minus the last one) in a function definition are of the class ast.Assign, we make an exception if the first one is an ast.Expr(ast.Constant).

References

#104

Copy link
Collaborator

@odashi odashi left a comment

Choose a reason for hiding this comment

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

It looks basically good!

src/latexify/codegen/function_codegen.py Outdated Show resolved Hide resolved
src/latexify/codegen/function_codegen_test.py Outdated Show resolved Hide resolved
src/latexify/codegen/function_codegen_test.py Outdated Show resolved Hide resolved
src/latexify/codegen/function_codegen_test.py Show resolved Hide resolved
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

src/latexify/ast_utils.py Outdated Show resolved Hide resolved
src/latexify/ast_utils.py Outdated Show resolved Hide resolved
src/latexify/ast_utils.py Outdated Show resolved Hide resolved
src/latexify/ast_utils.py Outdated Show resolved Hide resolved
src/latexify/ast_utils_test.py Outdated Show resolved Hide resolved
src/latexify/ast_utils_test.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@odashi odashi left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@odashi odashi merged commit 4f0a208 into google:main Nov 20, 2022
@ZibingZhang ZibingZhang deleted the ignore-docstring branch November 20, 2022 10:34
@odashi odashi added this to the v0.3 milestone Nov 21, 2022
@odashi odashi added the feature label Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants