Skip to content

Module docstrings in 3.7 are not part of Module node anymore #3531

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

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog/3530.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix if in tests to support 3.7.0b5, where a docstring handling in AST got reverted.
50 changes: 28 additions & 22 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,35 @@ def getmsg(f, extra_ns=None, must_pass=False):
pytest.fail("function didn't raise at all")


def adjust_body_for_new_docstring_in_module_node(m):
"""Module docstrings in 3.8 are part of Module node.
This was briefly in 3.7 as well but got reverted in beta 5.

It's not in the body so we remove it so the following body items have
the same indexes on all Python versions:

TODO:

We have a complicated sys.version_info if in here to ease testing on
various Python 3.7 versions, but we should remove the 3.7 check after
3.7 is released as stable to make this check more straightforward.
"""
if (
sys.version_info < (3, 8)
and not ((3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4))
):
assert len(m.body) > 1
assert isinstance(m.body[0], ast.Expr)
assert isinstance(m.body[0].value, ast.Str)
del m.body[0]


class TestAssertionRewrite(object):

def test_place_initial_imports(self):
s = """'Doc string'\nother = stuff"""
m = rewrite(s)
# Module docstrings in 3.7 are part of Module node, it's not in the body
# so we remove it so the following body items have the same indexes on
# all Python versions
if sys.version_info < (3, 7):
assert isinstance(m.body[0], ast.Expr)
assert isinstance(m.body[0].value, ast.Str)
del m.body[0]
adjust_body_for_new_docstring_in_module_node(m)
for imp in m.body[0:2]:
assert isinstance(imp, ast.Import)
assert imp.lineno == 2
Expand All @@ -92,21 +109,15 @@ def test_place_initial_imports(self):
assert isinstance(m.body[3], ast.Expr)
s = """'doc string'\nfrom __future__ import with_statement"""
m = rewrite(s)
if sys.version_info < (3, 7):
assert isinstance(m.body[0], ast.Expr)
assert isinstance(m.body[0].value, ast.Str)
del m.body[0]
adjust_body_for_new_docstring_in_module_node(m)
assert isinstance(m.body[0], ast.ImportFrom)
for imp in m.body[1:3]:
assert isinstance(imp, ast.Import)
assert imp.lineno == 2
assert imp.col_offset == 0
s = """'doc string'\nfrom __future__ import with_statement\nother"""
m = rewrite(s)
if sys.version_info < (3, 7):
assert isinstance(m.body[0], ast.Expr)
assert isinstance(m.body[0].value, ast.Str)
del m.body[0]
adjust_body_for_new_docstring_in_module_node(m)
assert isinstance(m.body[0], ast.ImportFrom)
for imp in m.body[1:3]:
assert isinstance(imp, ast.Import)
Expand All @@ -124,13 +135,8 @@ def test_place_initial_imports(self):
def test_dont_rewrite(self):
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
m = rewrite(s)
if sys.version_info < (3, 7):
assert len(m.body) == 2
assert isinstance(m.body[0], ast.Expr)
assert isinstance(m.body[0].value, ast.Str)
del m.body[0]
else:
assert len(m.body) == 1
adjust_body_for_new_docstring_in_module_node(m)
assert len(m.body) == 1
assert m.body[0].msg is None

def test_dont_rewrite_plugin(self, testdir):
Expand Down