@@ -65,33 +65,33 @@ def getmsg(f, extra_ns=None, must_pass=False):
65
65
pytest .fail ("function didn't raise at all" )
66
66
67
67
68
- def python_version_has_docstring_in_module_node ( ):
68
+ def adjust_body_for_new_docstring_in_module_node ( m ):
69
69
"""Module docstrings in 3.8 are part of Module node.
70
70
This was briefly in 3.7 as well but got reverted in beta 5.
71
71
72
+ It's not in the body so we remove it so the following body items have
73
+ the same indexes on all Python versions:
74
+
72
75
TODO:
73
76
74
77
We have a complicated sys.version_info if in here to ease testing on
75
78
various Python 3.7 versions, but we should remove the 3.7 check after
76
79
3.7 is released as stable to make this check more straightforward.
77
80
"""
78
- return (
79
- sys .version_info < (3 , 8 ) or (3 , 7 ) <= sys .version_info <= (3 , 7 , 0 , "beta" , 4 )
80
- )
81
+ if sys .version_info < (3 , 8 ) or (3 , 7 ) <= sys .version_info <= (3 , 7 , 0 , "beta" , 4 ):
82
+ assert len (m .body ) > 1
83
+ assert isinstance (m .body [0 ], ast .Expr )
84
+ assert isinstance (m .body [0 ].value , ast .Str )
85
+ del m .body [0 ]
86
+ return m
81
87
82
88
83
89
class TestAssertionRewrite (object ):
84
90
85
91
def test_place_initial_imports (self ):
86
92
s = """'Doc string'\n other = stuff"""
87
93
m = rewrite (s )
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 ():
92
- assert isinstance (m .body [0 ], ast .Expr )
93
- assert isinstance (m .body [0 ].value , ast .Str )
94
- del m .body [0 ]
94
+ m = adjust_body_for_new_docstring_in_module_node (m )
95
95
for imp in m .body [0 :2 ]:
96
96
assert isinstance (imp , ast .Import )
97
97
assert imp .lineno == 2
@@ -107,21 +107,15 @@ def test_place_initial_imports(self):
107
107
assert isinstance (m .body [3 ], ast .Expr )
108
108
s = """'doc string'\n from __future__ import with_statement"""
109
109
m = rewrite (s )
110
- if python_version_has_docstring_in_module_node ():
111
- assert isinstance (m .body [0 ], ast .Expr )
112
- assert isinstance (m .body [0 ].value , ast .Str )
113
- del m .body [0 ]
110
+ m = adjust_body_for_new_docstring_in_module_node (m )
114
111
assert isinstance (m .body [0 ], ast .ImportFrom )
115
112
for imp in m .body [1 :3 ]:
116
113
assert isinstance (imp , ast .Import )
117
114
assert imp .lineno == 2
118
115
assert imp .col_offset == 0
119
116
s = """'doc string'\n from __future__ import with_statement\n other"""
120
117
m = rewrite (s )
121
- if python_version_has_docstring_in_module_node ():
122
- assert isinstance (m .body [0 ], ast .Expr )
123
- assert isinstance (m .body [0 ].value , ast .Str )
124
- del m .body [0 ]
118
+ m = adjust_body_for_new_docstring_in_module_node (m )
125
119
assert isinstance (m .body [0 ], ast .ImportFrom )
126
120
for imp in m .body [1 :3 ]:
127
121
assert isinstance (imp , ast .Import )
@@ -139,13 +133,8 @@ def test_place_initial_imports(self):
139
133
def test_dont_rewrite (self ):
140
134
s = """'PYTEST_DONT_REWRITE'\n assert 14"""
141
135
m = rewrite (s )
142
- if python_version_has_docstring_in_module_node ():
143
- assert len (m .body ) == 2
144
- assert isinstance (m .body [0 ], ast .Expr )
145
- assert isinstance (m .body [0 ].value , ast .Str )
146
- del m .body [0 ]
147
- else :
148
- assert len (m .body ) == 1
136
+ m = adjust_body_for_new_docstring_in_module_node (m )
137
+ assert len (m .body ) == 1
149
138
assert m .body [0 ].msg is None
150
139
151
140
def test_dont_rewrite_plugin (self , testdir ):
0 commit comments