Skip to content

Commit 8d80642

Browse files
committed
Check feature names imported from future
As '*' does not appear in __future__.all_feature_names, this also reports an error on : from __future__ import *
1 parent 9c88c0e commit 8d80642

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

pyflakes/checker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Implement the central Checker class.
55
Also, it models the Bindings and Scopes.
66
"""
7+
import __future__
78
import doctest
89
import os
910
import sys
@@ -1053,6 +1054,9 @@ def IMPORTFROM(self, node):
10531054
name = alias.asname or alias.name
10541055
if node.module == '__future__':
10551056
importation = FutureImportation(name, node, self.scope)
1057+
if alias.name not in __future__.all_feature_names:
1058+
self.report(messages.FutureFeatureNotDefined,
1059+
node, alias.name)
10561060
elif alias.name == '*':
10571061
# Only Python 2, local import * is a SyntaxWarning
10581062
if not PY2 and not isinstance(self.scope, ModuleScope):

pyflakes/messages.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ def __init__(self, filename, loc, names):
124124
self.message_args = ()
125125

126126

127+
class FutureFeatureNotDefined(Message):
128+
"""An undefined __future__ feature name was imported."""
129+
message = 'future feature %s is not defined'
130+
131+
def __init__(self, filename, loc, name):
132+
Message.__init__(self, filename, loc)
133+
self.message_args = (name,)
134+
135+
127136
class UnusedVariable(Message):
128137
"""
129138
Indicates that a variable has been explicitly assigned to but not actually

pyflakes/test/test_imports.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,18 @@ def test_futureImportUsed(self):
788788
assert print_function is not division
789789
''')
790790

791+
def test_futureImportUndefined(self):
792+
"""Importing undefined names from __future__ fails."""
793+
self.flakes('''
794+
from __future__ import print_statement
795+
''', m.FutureFeatureNotDefined)
796+
797+
def test_futureImportStar(self):
798+
"""Importing '*' from __future__ fails."""
799+
self.flakes('''
800+
from __future__ import *
801+
''', m.FutureFeatureNotDefined)
802+
791803

792804
class TestSpecialAll(TestCase):
793805
"""

0 commit comments

Comments
 (0)