Repository navigation
gh-109485: Further improve test_future_stmt tests
#109486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe8d76c
gh-109485: Further improve `test_future_stmt` tests
sobolevn 095f066
Address review
sobolevn 6fc2efc
Address review
sobolevn b56cc8e
Kw `lineno`
sobolevn 35a3a3d
Adjust lineno
sobolevn 06c2747
Add docstring in parametrization
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Address review
- Loading branch information
commit 6fc2efcca929da79a38546d20a501a2d9598fb36
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
|
@@ -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) | ||
| 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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last question: instead of copy/paste |
||
| 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 | ||
|
|
@@ -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 = """ | ||
|
|
@@ -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): | ||
|
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) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
And adapt
lineno -= 2and 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.