|
3 | 3 | import __future__ |
4 | 4 | import ast |
5 | 5 | import unittest |
6 | | -from test.support import force_not_colorized, import_helper |
| 6 | +from test.support import force_not_colorized, import_helper, subTests |
7 | 7 | from test.support.script_helper import spawn_python, kill_python |
8 | 8 | from textwrap import dedent |
9 | 9 | import os |
@@ -87,8 +87,44 @@ def test_unknown_future_flag(self): |
87 | 87 | from __future__ import rested_snopes # typo error here: nested => rested |
88 | 88 | """ |
89 | 89 | self.assertSyntaxError( |
90 | | - code, lineno=2, |
91 | | - message='future feature rested_snopes is not defined', offset=24, |
| 90 | + code, |
| 91 | + lineno=2, |
| 92 | + message=( |
| 93 | + "future feature 'rested_snopes' is not defined. " |
| 94 | + "Did you mean: 'nested_scopes'?" |
| 95 | + ), |
| 96 | + offset=24, |
| 97 | + ) |
| 98 | + |
| 99 | + @subTests("typo, origin", [ |
| 100 | + ("nest_scopes", "nested_scopes"), |
| 101 | + ("gneretors", "generators"), |
| 102 | + ("divicion", "division"), |
| 103 | + ("absolute_imports", "absolute_import"), |
| 104 | + ("print_func", "print_function"), |
| 105 | + ("unicode_literal", "unicode_literals"), |
| 106 | + ("barry_as_bdfl", "barry_as_FLUFL"), |
| 107 | + ("generatorstop", "generator_stop"), |
| 108 | + ("anotations", "annotations"), |
| 109 | + ]) |
| 110 | + def test_typos_in_future_imports(self, typo, origin): |
| 111 | + self.assertSyntaxError( |
| 112 | + f"from __future__ import {typo}", |
| 113 | + lineno=1, |
| 114 | + message=( |
| 115 | + f"future feature '{typo}' is not defined. " |
| 116 | + f"Did you mean: '{origin}'?" |
| 117 | + ), |
| 118 | + offset=24, |
| 119 | + ) |
| 120 | + |
| 121 | + @subTests("name", ["missing_name", "brces", "brace"]) |
| 122 | + def test_no_suggestion_on_missing_name(self, name): |
| 123 | + self.assertSyntaxError( |
| 124 | + f"from __future__ import {name}", |
| 125 | + lineno=1, |
| 126 | + message=f"future feature '{name}' is not defined", |
| 127 | + offset=24, |
92 | 128 | ) |
93 | 129 |
|
94 | 130 | def test_future_import_not_on_top(self): |
@@ -137,7 +173,7 @@ def test_future_import_star(self): |
137 | 173 | code = """ |
138 | 174 | from __future__ import * |
139 | 175 | """ |
140 | | - self.assertSyntaxError(code, message='future feature * is not defined', offset=24) |
| 176 | + self.assertSyntaxError(code, message="future feature '*' is not defined", offset=24) |
141 | 177 |
|
142 | 178 | def test_future_import_braces(self): |
143 | 179 | code = """ |
|
0 commit comments