@@ -65,18 +65,35 @@ def getmsg(f, extra_ns=None, must_pass=False):
6565 pytest .fail ("function didn't raise at all" )
6666
6767
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+
6891class TestAssertionRewrite (object ):
6992
7093 def test_place_initial_imports (self ):
7194 s = """'Doc string'\n other = stuff"""
7295 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 )
8097 for imp in m .body [0 :2 ]:
8198 assert isinstance (imp , ast .Import )
8299 assert imp .lineno == 2
@@ -92,21 +109,15 @@ def test_place_initial_imports(self):
92109 assert isinstance (m .body [3 ], ast .Expr )
93110 s = """'doc string'\n from __future__ import with_statement"""
94111 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 )
99113 assert isinstance (m .body [0 ], ast .ImportFrom )
100114 for imp in m .body [1 :3 ]:
101115 assert isinstance (imp , ast .Import )
102116 assert imp .lineno == 2
103117 assert imp .col_offset == 0
104118 s = """'doc string'\n from __future__ import with_statement\n other"""
105119 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 )
110121 assert isinstance (m .body [0 ], ast .ImportFrom )
111122 for imp in m .body [1 :3 ]:
112123 assert isinstance (imp , ast .Import )
@@ -124,13 +135,8 @@ def test_place_initial_imports(self):
124135 def test_dont_rewrite (self ):
125136 s = """'PYTEST_DONT_REWRITE'\n assert 14"""
126137 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
134140 assert m .body [0 ].msg is None
135141
136142 def test_dont_rewrite_plugin (self , testdir ):
0 commit comments