summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Vandenberg <jayvdb@gmail.com>2015-11-25 00:20:04 +1100
committerJohn Vandenberg <jayvdb@gmail.com>2015-11-25 00:20:04 +1100
commit8d80642a76e16af6a8756a21b9ef9ec9a4b32108 (patch)
treeb9759b157f2648d62d75ebf5701990443924c8f2
parent9c88c0ec3f4cdb0fb2e69c2bdadc0bdd03da4a55 (diff)
downloadpyflakes-8d80642a76e16af6a8756a21b9ef9ec9a4b32108.tar.gz
Check feature names imported from future
As '*' does not appear in __future__.all_feature_names, this also reports an error on : from __future__ import *
-rw-r--r--pyflakes/checker.py4
-rw-r--r--pyflakes/messages.py9
-rw-r--r--pyflakes/test/test_imports.py12
3 files changed, 25 insertions, 0 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 719f6b4..fe20146 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -4,6 +4,7 @@ Main module.
Implement the central Checker class.
Also, it models the Bindings and Scopes.
"""
+import __future__
import doctest
import os
import sys
@@ -1053,6 +1054,9 @@ class Checker(object):
name = alias.asname or alias.name
if node.module == '__future__':
importation = FutureImportation(name, node, self.scope)
+ if alias.name not in __future__.all_feature_names:
+ self.report(messages.FutureFeatureNotDefined,
+ node, alias.name)
elif alias.name == '*':
# Only Python 2, local import * is a SyntaxWarning
if not PY2 and not isinstance(self.scope, ModuleScope):
diff --git a/pyflakes/messages.py b/pyflakes/messages.py
index e9aef81..05db5bf 100644
--- a/pyflakes/messages.py
+++ b/pyflakes/messages.py
@@ -124,6 +124,15 @@ class LateFutureImport(Message):
self.message_args = ()
+class FutureFeatureNotDefined(Message):
+ """An undefined __future__ feature name was imported."""
+ message = 'future feature %s is not defined'
+
+ def __init__(self, filename, loc, name):
+ Message.__init__(self, filename, loc)
+ self.message_args = (name,)
+
+
class UnusedVariable(Message):
"""
Indicates that a variable has been explicitly assigned to but not actually
diff --git a/pyflakes/test/test_imports.py b/pyflakes/test/test_imports.py
index d6f9205..2101579 100644
--- a/pyflakes/test/test_imports.py
+++ b/pyflakes/test/test_imports.py
@@ -788,6 +788,18 @@ class Test(TestCase):
assert print_function is not division
''')
+ def test_futureImportUndefined(self):
+ """Importing undefined names from __future__ fails."""
+ self.flakes('''
+ from __future__ import print_statement
+ ''', m.FutureFeatureNotDefined)
+
+ def test_futureImportStar(self):
+ """Importing '*' from __future__ fails."""
+ self.flakes('''
+ from __future__ import *
+ ''', m.FutureFeatureNotDefined)
+
class TestSpecialAll(TestCase):
"""