Navigation Menu

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

Fix AST handling of docstrings and __future__ ordering #1673

Merged
merged 2 commits into from Sep 11, 2018

Conversation

brandonwillard
Copy link
Member

This closes #1367, closes #1540

hy/compiler.py Outdated
body += [result.stmts.pop(0)]

body += sorted(compiler.imports_as_stmts(tree) + result.stmts,
key=import_order_key)
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't you just write something like key=lambda x: not (isinstance(x, ast.ImportFrom) and x.module == '__future__')?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, probably. This started from an elaborate scheme to check ast.ImportFrom and multi-import ast.Imports, then I realized only from __future__ import ... statements needed to be handled and cut out parts from there.

"""Confirm that docstrings come before import statements in AST output."""
hy_tree = import_buffer_to_hst('"hello world"\n(print (first [1 2]))')
hy_ast = hy_compile(hy_tree, 'test')
assert hy_ast.body[0].value.s == 'hello world'
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be a better test to add a module docstring to resources/pydemo.hy and check for it in test_hy2py.

Copy link
Member Author

Choose a reason for hiding this comment

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

You fine leaving that test in there, though? I like its "directness".

Copy link
Member

Choose a reason for hiding this comment

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

It uses an internal function (import_buffer_to_hst), and it relies on the structure of Python AST, to test for a bug that's readily visible in user space. So I'd say we should get rid of it if we don't need it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like the tests in test_ast.py are using can_compile instead; I'll make the other tests here use that as well.

Otherwise, I can replace this one with an assertion to test_hy2py that checks m.__doc__.

@brandonwillard brandonwillard force-pushed the fix-docstring-with-imports branch 4 times, most recently from 68ffb3c to 7f52f68 Compare August 28, 2018 00:58
Copy link
Member

@Kodiologist Kodiologist left a comment

Choose a reason for hiding this comment

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

Don't forget NEWS.

@Kodiologist Kodiologist merged commit a2f3a08 into hylang:master Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants