@@ -2874,9 +2874,10 @@ def check_warning(self, code, errtext, filename="<testcase>", mode="exec"):
28742874
28752875 def check_no_warning (self , code , filename = "<testcase>" , mode = "exec" ):
28762876 """Check that compiling code does not raise any warnings."""
2877+ import warnings
28772878 with warnings .catch_warnings (record = True ) as caught :
28782879 warnings .simplefilter ("always" )
2879- compile (source , filename , mode )
2880+ compile (code , filename , mode )
28802881 self .assertEqual (caught , [])
28812882
28822883 def test_return_in_finally (self ):
@@ -2949,73 +2950,74 @@ def test_break_and_continue_in_finally(self):
29492950 """ )
29502951 self .check_warning (source , f"'{ kw } ' in a 'finally' block" )
29512952
2952- def test_from_lazy_imports (self ):
2953- # gh-150459
2954- self .check_warning (
2955- "from . lazy import x" ,
2956- "did you mean 'lazy from . import'?" ,
2957- )
2958- self .check_warning (
2959- "from . lazy import x as y" ,
2960- "did you mean 'lazy from . import'?" ,
2961- )
2962- self .check_warning (
2963- "from . lazy import *" ,
2964- "did you mean 'lazy from . import'?" ,
2965- )
2966- self .check_warning (
2967- "from .. lazy import x" ,
2968- "did you mean 'lazy from .. import'?" ,
2969- )
2970- self .check_warning (
2971- "from ... lazy import x" ,
2972- "did you mean 'lazy from ... import'?" ,
2973- )
2974- self .check_warning (
2975- "from .... lazy import x" ,
2976- "did you mean 'lazy from .... import'?" ,
2977- )
2978- self .check_warning (
2979- "from . \\ \n lazy import x" ,
2980- "did you mean 'lazy from . import'?" ,
2981- )
2982- self .check_warning (
2983- "from .\\ \n lazy import x" ,
2984- "did you mean 'lazy from . import'?" ,
2985- )
2986- self .check_warning (
2987- "from .\t lazy import x" ,
2988- "did you mean 'lazy from . import'?" ,
2989- )
2990-
2991- def test_not_from_lazy_imports (self ):
2992- self .check_no_warning ("from .lazy import x" )
2993- self .check_no_warning ("from .lazy import *" )
2994- self .check_no_warning ("from ..lazy import x" )
2995- self .check_no_warning ("from ...lazy import x" )
2996- self .check_no_warning ("from .lazy.sub import x" )
2997- self .check_no_warning ("from ..lazy.sub import x" )
2998- self .check_no_warning ("from ...lazy.sub import x" )
2999- self .check_no_warning ("from . lazier import x" )
3000- self .check_no_warning ("from . lazy_module import x" )
3001- self .check_no_warning ("from . lazy.sub import x" )
3002- self .check_no_warning ("from . sub.lazy import x" )
3003- self .check_no_warning ("from lazy import x" )
3004- self .check_no_warning ("from lazy.sub import x" )
3005- self .check_no_warning ("lazy from . lazy import x" )
3006- self .check_no_warning ("from . import lazy" )
3007-
3008- def test_from_lazy_imports_as_error (self ):
3009- with warnings .catch_warnings ():
3010- warnings .simplefilter ("error" , SyntaxWarning )
3011- with self .assertRaisesRegex (
3012- SyntaxError ,
3013- re .escape ("did you mean 'lazy from . import'?" ),
3014- ) as cm :
3015- compile ("from . lazy import x" , "<test>" , "exec" )
3016- self .assertEqual (cm .exception .lineno , 1 )
3017- self .assertEqual (cm .exception .offset , 8 )
3018- self .assertEqual (cm .exception .end_offset , 12 )
2953+ def test_from_lazy_imports (self ):
2954+ # gh-150459
2955+ self .check_warning (
2956+ "from . lazy import x" ,
2957+ "did you mean 'lazy from . import'?" ,
2958+ )
2959+ self .check_warning (
2960+ "from . lazy import x as y" ,
2961+ "did you mean 'lazy from . import'?" ,
2962+ )
2963+ self .check_warning (
2964+ "from . lazy import *" ,
2965+ "did you mean 'lazy from . import'?" ,
2966+ )
2967+ self .check_warning (
2968+ "from .. lazy import x" ,
2969+ "did you mean 'lazy from .. import'?" ,
2970+ )
2971+ self .check_warning (
2972+ "from ... lazy import x" ,
2973+ "did you mean 'lazy from ... import'?" ,
2974+ )
2975+ self .check_warning (
2976+ "from .... lazy import x" ,
2977+ "did you mean 'lazy from .... import'?" ,
2978+ )
2979+ self .check_warning (
2980+ "from . \\ \n lazy import x" ,
2981+ "did you mean 'lazy from . import'?" ,
2982+ )
2983+ self .check_warning (
2984+ "from .\\ \n lazy import x" ,
2985+ "did you mean 'lazy from . import'?" ,
2986+ )
2987+ self .check_warning (
2988+ "from .\t lazy import x" ,
2989+ "did you mean 'lazy from . import'?" ,
2990+ )
2991+
2992+ def test_not_from_lazy_imports (self ):
2993+ self .check_no_warning ("from .lazy import x" )
2994+ self .check_no_warning ("from .lazy import *" )
2995+ self .check_no_warning ("from ..lazy import x" )
2996+ self .check_no_warning ("from ...lazy import x" )
2997+ self .check_no_warning ("from .lazy.sub import x" )
2998+ self .check_no_warning ("from ..lazy.sub import x" )
2999+ self .check_no_warning ("from ...lazy.sub import x" )
3000+ self .check_no_warning ("from . lazier import x" )
3001+ self .check_no_warning ("from . lazy_module import x" )
3002+ self .check_no_warning ("from . lazy.sub import x" )
3003+ self .check_no_warning ("from . sub.lazy import x" )
3004+ self .check_no_warning ("from lazy import x" )
3005+ self .check_no_warning ("from lazy.sub import x" )
3006+ self .check_no_warning ("lazy from . lazy import x" )
3007+ self .check_no_warning ("from . import lazy" )
3008+
3009+ def test_from_lazy_imports_as_error (self ):
3010+ import warnings
3011+ with warnings .catch_warnings ():
3012+ warnings .simplefilter ("error" , SyntaxWarning )
3013+ with self .assertRaisesRegex (
3014+ SyntaxError ,
3015+ re .escape ("did you mean 'lazy from . import'?" ),
3016+ ) as cm :
3017+ compile ("from . lazy import x" , "<test>" , "exec" )
3018+ self .assertEqual (cm .exception .lineno , 1 )
3019+ self .assertEqual (cm .exception .offset , 8 )
3020+ self .assertEqual (cm .exception .end_offset , 12 )
30193021
30203022
30213023class SyntaxErrorTestCase (unittest .TestCase ):
0 commit comments