Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed Sep 18, 2023
commit 6fc2efcca929da79a38546d20a501a2d9598fb36
31 changes: 21 additions & 10 deletions Lib/test/test_future_stmt/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import re
import sys

TOP_LEVEL_MSG = 'from __future__ imports must occur at the beginning of the file'

rx = re.compile(r'\((\S+).py, line (\d+)')

def get_error_location(msg):
Expand All @@ -18,25 +20,30 @@ def get_error_location(msg):

class FutureTest(unittest.TestCase):

def check_syntax_error(self, err, basename, lineno, offset=1):
def check_syntax_error(self, err, basename, lineno,
message=TOP_LEVEL_MSG, offset=1):
if basename != '<string>':
basename += '.py'

self.assertIn(f'{basename}, line {lineno}', str(err))
self.assertEqual(f'{message} ({basename}, line {lineno})', str(err))
self.assertEqual(os.path.basename(err.filename), basename)
self.assertEqual(err.lineno, lineno)
self.assertEqual(err.offset, offset)

def assertSyntaxError(self, code, lineno, offset=1, *, parametrize_docstring=True):
def assertSyntaxError(self, code, lineno,
message=TOP_LEVEL_MSG, offset=1,
*,
parametrize_docstring=True):
code = dedent(code)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like code first line is always empty. I suggest using:

Suggested change
code = dedent(code)
code = dedent(code.lstrip())

And adapt lineno -= 2 and other references to lineno below.

For example, for me, test_unknown_future_flag() code has only 3 lines and expect an error on the 4th line, it's strange.

for trim_docstring in ([False, True] if parametrize_docstring else [False]):
with self.subTest(trim_docstring=trim_docstring):
with self.subTest(code=code, trim_docstring=trim_docstring):
if trim_docstring:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last question: instead of copy/paste '''Docstring''' in each test. Would it make sense to add it here instead? I'm waiting for your opinion to decide if we merge the change like that, or you prefer to remove docstrings in tests and add it here instead.

code = os.linesep.join(code.splitlines()[2:])
lineno -= 2
with self.assertRaises(SyntaxError) as cm:
exec(code)
self.check_syntax_error(cm.exception, "<string>", lineno, offset=offset)
self.check_syntax_error(cm.exception, "<string>", lineno,
message, offset=offset)

def test_import_nested_scope_twice(self):
# Import the name nested_scopes twice to trigger SF bug #407394
Expand Down Expand Up @@ -75,7 +82,10 @@ def test_unknown_future_flag(self):
from __future__ import nested_scopes
from __future__ import rested_snopes # typo error here: nested => rested
"""
self.assertSyntaxError(code, 4)
self.assertSyntaxError(
code, 4,
message='future feature rested_snopes is not defined',
)

def test_future_import_not_on_top(self):
code = """
Expand Down Expand Up @@ -129,22 +139,23 @@ def test_future_import_star(self):
'''Docstring'''
from __future__ import *
"""
self.assertSyntaxError(code, 3)
self.assertSyntaxError(code, 3, message='future feature * is not defined')

def test_future_import_braces(self):
Comment thread
vstinner marked this conversation as resolved.
code = """
'''Docstring'''
from __future__ import braces
"""
self.assertSyntaxError(code, 3)
# Congrats, you found an easter egg!
self.assertSyntaxError(code, 3, message='not a chance')

code = """
'''Docstring'''
from __future__ import nested_scopes, braces
"""
self.assertSyntaxError(code, 3)
self.assertSyntaxError(code, 3, message='not a chance')

def test_bad_future_as_module(self):
def test_module_with_future_import_not_on_top(self):
with self.assertRaises(SyntaxError) as cm:
from test.test_future_stmt import badsyntax_future
self.check_syntax_error(cm.exception, "badsyntax_future", 3)
Expand Down