@@ -70,6 +70,7 @@ def __init__(self,
7070 def visit_unbound_type (self , t : UnboundType ) -> Type :
7171 sym = self .lookup (t .name , t )
7272 if sym is not None :
73+ fullname = sym .node .fullname ()
7374 if sym .kind == TVAR :
7475 if len (t .args ) > 0 :
7576 self .fail ('Type variable "{}" used with arguments' .format (
@@ -81,18 +82,24 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
8182 values = cast (TypeVarExpr , sym .node ).values
8283 return TypeVar (t .name , sym .tvar_id , values , self .builtin_type ('builtins.object' ),
8384 t .line , rep )
84- elif sym . node . fullname () == 'builtins.None' :
85+ elif fullname == 'builtins.None' :
8586 return Void ()
86- elif sym . node . fullname () == 'typing.Any' :
87+ elif fullname == 'typing.Any' :
8788 return AnyType ()
88- elif sym . node . fullname () == 'typing.Tuple' :
89+ elif fullname == 'typing.Tuple' :
8990 return TupleType (self .anal_array (t .args ),
9091 self .builtin_type ('builtins.tuple' ))
91- elif sym . node . fullname () == 'typing.Union' :
92+ elif fullname == 'typing.Union' :
9293 items = self .anal_array (t .args )
9394 items = [item for item in items if not isinstance (item , Void )]
9495 return UnionType .make_union (items )
95- elif sym .node .fullname () == 'typing.Callable' :
96+ elif fullname == 'typing.Optional' :
97+ if len (t .args ) != 1 :
98+ self .fail ('Optional[...] must have exactly one type argument' , t )
99+ items = self .anal_array (t .args )
100+ # Currently Optional[t] is just an alias for t.
101+ return items [0 ]
102+ elif fullname == 'typing.Callable' :
96103 return self .analyze_function_type (t )
97104 elif sym .kind == TYPE_ALIAS :
98105 # TODO: Generic type aliases.
@@ -109,7 +116,6 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
109116 Instance (info , [], t .line ),
110117 t .line , t .repr )
111118 else :
112-
113119 # Analyze arguments and construct Instance type. The
114120 # number of type arguments and their values are
115121 # checked only later, since we do not always know the
0 commit comments