@@ -65,18 +65,35 @@ 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 adjust_body_for_new_docstring_in_module_node (m ):
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
+ 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
+
75
+ TODO:
76
+
77
+ We have a complicated sys.version_info if in here to ease testing on
78
+ various Python 3.7 versions, but we should remove the 3.7 check after
79
+ 3.7 is released as stable to make this check more straightforward.
80
+ """
81
+ if (
82
+ sys .version_info < (3 , 8 )
83
+ and not ((3 , 7 ) <= sys .version_info <= (3 , 7 , 0 , "beta" , 4 ))
84
+ ):
85
+ assert len (m .body ) > 1
86
+ assert isinstance (m .body [0 ], ast .Expr )
87
+ assert isinstance (m .body [0 ].value , ast .Str )
88
+ del m .body [0 ]
89
+
90
+
68
91
class TestAssertionRewrite (object ):
69
92
70
93
def test_place_initial_imports (self ):
71
94
s = """'Doc string'\n other = stuff"""
72
95
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 ):
77
- assert isinstance (m .body [0 ], ast .Expr )
78
- assert isinstance (m .body [0 ].value , ast .Str )
79
- del m .body [0 ]
96
+ adjust_body_for_new_docstring_in_module_node (m )
80
97
for imp in m .body [0 :2 ]:
81
98
assert isinstance (imp , ast .Import )
82
99
assert imp .lineno == 2
@@ -92,21 +109,15 @@ def test_place_initial_imports(self):
92
109
assert isinstance (m .body [3 ], ast .Expr )
93
110
s = """'doc string'\n from __future__ import with_statement"""
94
111
m = rewrite (s )
95
- if sys .version_info < (3 , 7 ):
96
- assert isinstance (m .body [0 ], ast .Expr )
97
- assert isinstance (m .body [0 ].value , ast .Str )
98
- del m .body [0 ]
112
+ adjust_body_for_new_docstring_in_module_node (m )
99
113
assert isinstance (m .body [0 ], ast .ImportFrom )
100
114
for imp in m .body [1 :3 ]:
101
115
assert isinstance (imp , ast .Import )
102
116
assert imp .lineno == 2
103
117
assert imp .col_offset == 0
104
118
s = """'doc string'\n from __future__ import with_statement\n other"""
105
119
m = rewrite (s )
106
- if sys .version_info < (3 , 7 ):
107
- assert isinstance (m .body [0 ], ast .Expr )
108
- assert isinstance (m .body [0 ].value , ast .Str )
109
- del m .body [0 ]
120
+ adjust_body_for_new_docstring_in_module_node (m )
110
121
assert isinstance (m .body [0 ], ast .ImportFrom )
111
122
for imp in m .body [1 :3 ]:
112
123
assert isinstance (imp , ast .Import )
@@ -124,13 +135,8 @@ def test_place_initial_imports(self):
124
135
def test_dont_rewrite (self ):
125
136
s = """'PYTEST_DONT_REWRITE'\n assert 14"""
126
137
m = rewrite (s )
127
- if sys .version_info < (3 , 7 ):
128
- assert len (m .body ) == 2
129
- assert isinstance (m .body [0 ], ast .Expr )
130
- assert isinstance (m .body [0 ].value , ast .Str )
131
- del m .body [0 ]
132
- else :
133
- assert len (m .body ) == 1
138
+ adjust_body_for_new_docstring_in_module_node (m )
139
+ assert len (m .body ) == 1
134
140
assert m .body [0 ].msg is None
135
141
136
142
def test_dont_rewrite_plugin (self , testdir ):
0 commit comments