@@ -158,6 +158,18 @@ def __init__(self, name, source):
158
158
self .fullName = name
159
159
160
160
161
+ class FutureImportation (Importation ):
162
+ """
163
+ A binding created by a from `__future__` import statement.
164
+
165
+ `__future__` imports are implicitly used.
166
+ """
167
+
168
+ def __init__ (self , name , source , scope ):
169
+ super (FutureImportation , self ).__init__ (name , source )
170
+ self .used = (scope , source )
171
+
172
+
161
173
class Argument (Binding ):
162
174
"""
163
175
Represents binding a name as an argument.
@@ -255,6 +267,7 @@ class GeneratorScope(Scope):
255
267
256
268
class ModuleScope (Scope ):
257
269
"""Scope for a module."""
270
+ _futures_allowed = True
258
271
259
272
260
273
class DoctestScope (ModuleScope ):
@@ -310,7 +323,6 @@ def __init__(self, tree, filename='(none)', builtins=None,
310
323
self .withDoctest = withDoctest
311
324
self .scopeStack = [ModuleScope ()]
312
325
self .exceptHandlers = [()]
313
- self .futuresAllowed = True
314
326
self .root = tree
315
327
self .handleChildren (tree )
316
328
self .runDeferred (self ._deferredFunctions )
@@ -356,6 +368,20 @@ def _in_doctest(self):
356
368
return (len (self .scopeStack ) >= 2 and
357
369
isinstance (self .scopeStack [1 ], DoctestScope ))
358
370
371
+ @property
372
+ def futuresAllowed (self ):
373
+ if not all (isinstance (scope , ModuleScope )
374
+ for scope in self .scopeStack ):
375
+ return False
376
+
377
+ return self .scope ._futures_allowed
378
+
379
+ @futuresAllowed .setter
380
+ def futuresAllowed (self , value ):
381
+ assert value is False
382
+ if isinstance (self .scope , ModuleScope ):
383
+ self .scope ._futures_allowed = False
384
+
359
385
@property
360
386
def scope (self ):
361
387
return self .scopeStack [- 1 ]
@@ -1025,7 +1051,9 @@ def IMPORTFROM(self, node):
1025
1051
1026
1052
for alias in node .names :
1027
1053
name = alias .asname or alias .name
1028
- if alias .name == '*' :
1054
+ if node .module == '__future__' :
1055
+ importation = FutureImportation (name , node , self .scope )
1056
+ elif alias .name == '*' :
1029
1057
# Only Python 2, local import * is a SyntaxWarning
1030
1058
if not PY2 and not isinstance (self .scope , ModuleScope ):
1031
1059
self .report (messages .ImportStarNotPermitted ,
@@ -1037,8 +1065,6 @@ def IMPORTFROM(self, node):
1037
1065
importation = StarImportation (node .module , node )
1038
1066
else :
1039
1067
importation = Importation (name , node )
1040
- if node .module == '__future__' :
1041
- importation .used = (self .scope , node )
1042
1068
self .addBinding (node , importation )
1043
1069
1044
1070
def TRY (self , node ):
0 commit comments