Skip to content

Commit e49a9ba

Browse files
committed
Module docstrings in 3.7 are not part of Module node anymore
Fixes pytest-dev#3530
1 parent d609b63 commit e49a9ba

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

changelog/3530.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix if in tests to support 3.7.0b5, where a docstring handling in AST got reverted.

testing/test_assertrewrite.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,30 @@ def getmsg(f, extra_ns=None, must_pass=False):
6565
pytest.fail("function didn't raise at all")
6666

6767

68+
def python_version_has_docstring_in_module_node():
69+
"""Module docstrings in 3.8 are part of Module node.
70+
This was briefly in 3.7 as well but got reverted in beta 5.
71+
72+
TODO:
73+
74+
We have a complicated sys.version_info if in here to ease testing on
75+
various Python 3.7 versions, but we should remove the 3.7 check after
76+
3.7 is released as stable to make this check more straightforward.
77+
"""
78+
return (
79+
sys.version_info < (3, 8) or (3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4)
80+
)
81+
82+
6883
class TestAssertionRewrite(object):
6984

7085
def test_place_initial_imports(self):
7186
s = """'Doc string'\nother = stuff"""
7287
m = rewrite(s)
73-
# Module docstrings in 3.7 are part of Module node, it's not in the body
74-
# so we remove it so the following body items have the same indexes on
75-
# all Python versions
76-
if sys.version_info < (3, 7):
88+
# Module docstrings in some new Python versions are part of Module node
89+
# It's not in the body so we remove it so the following body items have
90+
# the same indexes on all Python versions:
91+
if python_version_has_docstring_in_module_node():
7792
assert isinstance(m.body[0], ast.Expr)
7893
assert isinstance(m.body[0].value, ast.Str)
7994
del m.body[0]
@@ -92,7 +107,7 @@ def test_place_initial_imports(self):
92107
assert isinstance(m.body[3], ast.Expr)
93108
s = """'doc string'\nfrom __future__ import with_statement"""
94109
m = rewrite(s)
95-
if sys.version_info < (3, 7):
110+
if python_version_has_docstring_in_module_node():
96111
assert isinstance(m.body[0], ast.Expr)
97112
assert isinstance(m.body[0].value, ast.Str)
98113
del m.body[0]
@@ -103,7 +118,7 @@ def test_place_initial_imports(self):
103118
assert imp.col_offset == 0
104119
s = """'doc string'\nfrom __future__ import with_statement\nother"""
105120
m = rewrite(s)
106-
if sys.version_info < (3, 7):
121+
if python_version_has_docstring_in_module_node():
107122
assert isinstance(m.body[0], ast.Expr)
108123
assert isinstance(m.body[0].value, ast.Str)
109124
del m.body[0]
@@ -124,7 +139,7 @@ def test_place_initial_imports(self):
124139
def test_dont_rewrite(self):
125140
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
126141
m = rewrite(s)
127-
if sys.version_info < (3, 7):
142+
if python_version_has_docstring_in_module_node():
128143
assert len(m.body) == 2
129144
assert isinstance(m.body[0], ast.Expr)
130145
assert isinstance(m.body[0].value, ast.Str)

0 commit comments

Comments
 (0)