@@ -70,7 +70,6 @@ def analyze_type_alias(node: Expression,
70
70
plugin : Plugin ,
71
71
options : Options ,
72
72
is_typeshed_stub : bool ,
73
- allow_new_syntax : bool = False ,
74
73
allow_placeholder : bool = False ,
75
74
in_dynamic_func : bool = False ,
76
75
global_scope : bool = True ) -> Optional [Tuple [Type , Set [str ]]]:
@@ -81,12 +80,12 @@ def analyze_type_alias(node: Expression,
81
80
Return None otherwise. 'node' must have been semantically analyzed.
82
81
"""
83
82
try :
84
- type = expr_to_unanalyzed_type (node , options , allow_new_syntax )
83
+ type = expr_to_unanalyzed_type (node , options , api . is_stub_file )
85
84
except TypeTranslationError :
86
85
api .fail ('Invalid type alias: expression is not a valid type' , node )
87
86
return None
88
87
analyzer = TypeAnalyser (api , tvar_scope , plugin , options , is_typeshed_stub ,
89
- allow_new_syntax = allow_new_syntax , defining_alias = True ,
88
+ defining_alias = True ,
90
89
allow_placeholder = allow_placeholder )
91
90
analyzer .in_dynamic_func = in_dynamic_func
92
91
analyzer .global_scope = global_scope
@@ -127,7 +126,6 @@ def __init__(self,
127
126
is_typeshed_stub : bool , * ,
128
127
defining_alias : bool = False ,
129
128
allow_tuple_literal : bool = False ,
130
- allow_new_syntax : bool = False ,
131
129
allow_unbound_tvars : bool = False ,
132
130
allow_placeholder : bool = False ,
133
131
allow_required : bool = False ,
@@ -144,8 +142,11 @@ def __init__(self,
144
142
# Positive if we are analyzing arguments of another (outer) type
145
143
self .nesting_level = 0
146
144
# Should we allow new type syntax when targeting older Python versions
147
- # like 'list[int]' or 'X | Y' (allowed in stubs)?
148
- self .allow_new_syntax = allow_new_syntax
145
+ # like 'list[int]' or 'X | Y' (allowed in stubs and with `__future__` import)?
146
+ self .always_allow_new_syntax = (
147
+ self .api .is_stub_file
148
+ or self .api .is_future_flag_set ('annotations' )
149
+ )
149
150
# Should we accept unbound type variables (always OK in aliases)?
150
151
self .allow_unbound_tvars = allow_unbound_tvars or defining_alias
151
152
# If false, record incomplete ref if we generate PlaceholderType.
@@ -202,9 +203,8 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
202
203
if hook is not None :
203
204
return hook (AnalyzeTypeContext (t , t , self ))
204
205
if (fullname in get_nongen_builtins (self .options .python_version )
205
- and t .args and
206
- not self .allow_new_syntax and
207
- not self .api .is_future_flag_set ("annotations" )):
206
+ and t .args
207
+ and not self .always_allow_new_syntax ):
208
208
self .fail (no_subscript_builtin_alias (fullname ,
209
209
propose_alt = not self .defining_alias ), t )
210
210
tvar_def = self .tvar_scope .get_binding (sym )
@@ -291,9 +291,8 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
291
291
" in a variable annotation" , t )
292
292
return AnyType (TypeOfAny .from_error )
293
293
elif (fullname == 'typing.Tuple' or
294
- (fullname == 'builtins.tuple' and (self .options .python_version >= (3 , 9 ) or
295
- self .api .is_future_flag_set ('annotations' ) or
296
- self .allow_new_syntax ))):
294
+ (fullname == 'builtins.tuple'
295
+ and (self .always_allow_new_syntax or self .options .python_version >= (3 , 9 )))):
297
296
# Tuple is special because it is involved in builtin import cycle
298
297
# and may be not ready when used.
299
298
sym = self .api .lookup_fully_qualified_or_none ('builtins.tuple' )
@@ -326,8 +325,8 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
326
325
elif fullname == 'typing.Callable' :
327
326
return self .analyze_callable_type (t )
328
327
elif (fullname == 'typing.Type' or
329
- (fullname == 'builtins.type' and ( self . options . python_version >= ( 3 , 9 ) or
330
- self .api . is_future_flag_set ( 'annotations' )))):
328
+ (fullname == 'builtins.type'
329
+ and ( self . always_allow_new_syntax or self .options . python_version >= ( 3 , 9 )))):
331
330
if len (t .args ) == 0 :
332
331
if fullname == 'typing.Type' :
333
332
any_type = self .get_omitted_any (t )
@@ -704,9 +703,8 @@ def visit_star_type(self, t: StarType) -> Type:
704
703
def visit_union_type (self , t : UnionType ) -> Type :
705
704
if (t .uses_pep604_syntax is True
706
705
and t .is_evaluated is True
707
- and self .api .is_stub_file is False
708
- and self .options .python_version < (3 , 10 )
709
- and self .api .is_future_flag_set ('annotations' ) is False ):
706
+ and not self .always_allow_new_syntax
707
+ and not self .options .python_version >= (3 , 10 )):
710
708
self .fail ("X | Y syntax for unions requires Python 3.10" , t )
711
709
return UnionType (self .anal_array (t .items ), t .line )
712
710
0 commit comments