@@ -673,12 +673,9 @@ def run(self, mod: ast.Module) -> None:
673
673
if not mod .body :
674
674
# Nothing to do.
675
675
return
676
- # Insert some special imports at the top of the module but after any
677
- # docstrings and __future__ imports.
678
- aliases = [
679
- ast .alias ("builtins" , "@py_builtins" ),
680
- ast .alias ("_pytest.assertion.rewrite" , "@pytest_ar" ),
681
- ]
676
+
677
+ # We'll insert some special imports at the top of the module, but after any
678
+ # docstrings and __future__ imports, so first figure out where that is.
682
679
doc = getattr (mod , "docstring" , None )
683
680
expect_docstring = doc is None
684
681
if doc is not None and self .is_rewrite_disabled (doc ):
@@ -710,10 +707,27 @@ def run(self, mod: ast.Module) -> None:
710
707
lineno = item .decorator_list [0 ].lineno
711
708
else :
712
709
lineno = item .lineno
710
+ # Now actually insert the special imports.
711
+ if sys .version_info >= (3 , 10 ):
712
+ aliases = [
713
+ ast .alias ("builtins" , "@py_builtins" , lineno = lineno , col_offset = 0 ),
714
+ ast .alias (
715
+ "_pytest.assertion.rewrite" ,
716
+ "@pytest_ar" ,
717
+ lineno = lineno ,
718
+ col_offset = 0 ,
719
+ ),
720
+ ]
721
+ else :
722
+ aliases = [
723
+ ast .alias ("builtins" , "@py_builtins" ),
724
+ ast .alias ("_pytest.assertion.rewrite" , "@pytest_ar" ),
725
+ ]
713
726
imports = [
714
727
ast .Import ([alias ], lineno = lineno , col_offset = 0 ) for alias in aliases
715
728
]
716
729
mod .body [pos :pos ] = imports
730
+
717
731
# Collect asserts.
718
732
nodes : List [ast .AST ] = [mod ]
719
733
while nodes :
0 commit comments