11"""Type parser"""
22
3- from typing import List , Tuple , Union , Optional , TypeVar , cast
4-
5- import typing
3+ from typing import List , Tuple , Union , Optional
64
75from mypy .types import (
86 Type , UnboundType , TupleType , ArgumentList , CallableType , StarType ,
9- EllipsisType , AnyType , ArgNameException , ArgKindException
7+ EllipsisType
108)
11-
12- from mypy .sharedparse import ARG_KINDS_BY_CONSTRUCTOR , STAR_ARG_CONSTRUCTORS
13-
149from mypy .lex import Token , Name , StrLit , lex
1510from mypy import nodes
1611
17- T = TypeVar ('T' , bound = Token )
1812
1913none = Token ('' ) # Empty token
2014
@@ -108,80 +102,23 @@ def parse_types(self) -> Type:
108102 type = TupleType (items , None , type .line , implicit = True )
109103 return type
110104
111- def parse_argument_spec (self ) -> Tuple [Type , Optional [str ], int ]:
112- current = self .current_token ()
113- nxt = self .next_token ()
114- # This is a small recreation of a subset of parsing a CallExpr; just
115- # enough to parse what happens in an arugment list.
116- # TODO: Doesn't handle an explicit name of None yet.
117- if isinstance (current , Name ) and nxt is not None and nxt .string == '(' :
118- arg_const = self .expect_type (Name ).string
119- name = None # type: Optional[str]
120- typ = AnyType (implicit = True ) # type: Type
121- try :
122- kind = ARG_KINDS_BY_CONSTRUCTOR [arg_const ]
123- except KeyError :
124- raise self .parse_error ("Unknown argument constructor {}" .format (arg_const ))
125- name , typ = self .parse_arg_args (read_name = arg_const not in STAR_ARG_CONSTRUCTORS )
126- return typ , name , kind
127- else :
128- return self .parse_type (), None , nodes .ARG_POS
129-
130- def parse_arg_args (self , * , read_name : bool ) -> Tuple [Optional [str ], Optional [Type ]]:
131- self .expect ('(' )
132- name = None # type: Optional[str]
133- typ = AnyType (implicit = True ) # type: Type
134- i = 0
135- while self .current_token_str () != ')' :
136- if i > 0 :
137- self .expect (',' )
138- if self .next_token () and self .next_token ().string == '=' :
139- arg_arg_name = self .current_token_str ()
140- if arg_arg_name == 'name' and read_name :
141- self .expect ('name' )
142- self .expect ('=' )
143- if self .current_token_str () == 'None' :
144- self .expect ('None' )
145- else :
146- name = self .expect_type (StrLit ).parsed ()
147- elif arg_arg_name == 'typ' :
148- self .expect ('typ' )
149- self .expect ('=' )
150- typ = self .parse_type ()
151- else :
152- raise self .parse_error (
153- 'Unexpected argument "{}" for argument constructor' .format (arg_arg_name ))
154- elif i == 0 and read_name :
155- if self .current_token_str () == 'None' :
156- self .expect ('None' )
157- else :
158- name = self .expect_type (StrLit ).parsed ()
159- elif i == 0 and not read_name or i == 1 and read_name :
160- typ = self .parse_type ()
161- else :
162- raise self .parse_error ("Unexpected argument for argument constructor" )
163- i += 1
164- self .expect (')' )
165- return name , typ
166-
167105 def parse_argument_list (self ) -> ArgumentList :
168106 """Parse type list [t, ...]."""
169107 lbracket = self .expect ('[' )
170108 commas = [] # type: List[Token]
171109 items = [] # type: List[Type]
172- names = [] # type: List[Optional[str]]
173- kinds = [] # type: List[int]
174110 while self .current_token_str () != ']' :
175- t , name , kind = self .parse_argument_spec ()
111+ t = self .parse_type ()
176112 items .append (t )
177- names .append (name )
178- kinds .append (kind )
179-
180113 if self .current_token_str () != ',' :
181114 break
182115 commas .append (self .skip ())
183116 self .expect (']' )
184- return ArgumentList (items , names , kinds , line = lbracket .line )
117+ return ArgumentList (
118+ items ,
119+ [None ] * len (items ),
120+ [None ] * len (items ),
121+ line = lbracket .line )
185122
186123 def parse_named_type (self ) -> Type :
187124 line = self .current_token ().line
@@ -235,26 +172,21 @@ def expect(self, string: str) -> Token:
235172 else :
236173 raise self .parse_error ()
237174
238- def expect_type (self , typ : typing . Type [ T ] ) -> T :
175+ def expect_type (self , typ : type ) -> Token :
239176 if isinstance (self .current_token (), typ ):
240177 self .ind += 1
241- return cast ( T , self .tok [self .ind - 1 ])
178+ return self .tok [self .ind - 1 ]
242179 else :
243180 raise self .parse_error ()
244181
245182 def current_token (self ) -> Token :
246183 return self .tok [self .ind ]
247184
248- def next_token (self ) -> Optional [Token ]:
249- if self .ind + 1 >= len (self .tok ):
250- return None
251- return self .tok [self .ind + 1 ]
252-
253185 def current_token_str (self ) -> str :
254186 return self .current_token ().string
255187
256- def parse_error (self , message : Optional [ str ] = None ) -> TypeParseError :
257- return TypeParseError (self .tok [self .ind ], self .ind , message = message )
188+ def parse_error (self ) -> TypeParseError :
189+ return TypeParseError (self .tok [self .ind ], self .ind )
258190
259191
260192def parse_str_as_type (typestr : str , line : int ) -> Type :
@@ -279,8 +211,6 @@ def parse_signature(tokens: List[Token]) -> Tuple[CallableType, int]:
279211 i = 0
280212 if tokens [i ].string != '(' :
281213 raise TypeParseError (tokens [i ], i )
282- begin = tokens [i ]
283- begin_idx = i
284214 i += 1
285215 arg_types = [] # type: List[Type]
286216 arg_kinds = [] # type: List[int]
@@ -316,11 +246,8 @@ def parse_signature(tokens: List[Token]) -> Tuple[CallableType, int]:
316246 raise TypeParseError (tokens [i ], i )
317247 i += 1
318248 ret_type , i = parse_type (tokens , i )
319- try :
320- return CallableType (arg_types ,
321- arg_kinds ,
322- [None ] * len (arg_types ),
323- ret_type , None ,
324- is_ellipsis_args = encountered_ellipsis ), i
325- except (ArgKindException , ArgNameException ) as e :
326- raise TypeParseError (begin , begin_idx , e .message )
249+ return CallableType (arg_types ,
250+ arg_kinds ,
251+ [None ] * len (arg_types ),
252+ ret_type , None ,
253+ is_ellipsis_args = encountered_ellipsis ), i
0 commit comments