@@ -185,17 +185,12 @@ def is_positional_arg(*name_or_flags) -> bool:
185
185
return not is_option_arg (* name_or_flags )
186
186
187
187
188
- def _tokenize_source (source : str ) -> Generator [tokenize .TokenInfo , None , None ]:
188
+ def tokenize_source (source : str ) -> Generator [tokenize .TokenInfo , None , None ]:
189
189
"""Returns a generator for the tokens of the object's source code, given the source code."""
190
190
return tokenize .generate_tokens (StringIO (source ).readline )
191
191
192
192
193
- def tokenize_source (obj : object ) -> Generator :
194
- """Returns a generator for the tokens of the object's source code."""
195
- return _tokenize_source (inspect .getsource (obj ))
196
-
197
-
198
- def _get_class_column (tokens : Iterable [tokenize .TokenInfo ]) -> int :
193
+ def get_class_column (tokens : Iterable [tokenize .TokenInfo ]) -> int :
199
194
"""Determines the column number for class variables in a class, given the tokens of the class."""
200
195
first_line = 1
201
196
for token_type , token , (start_line , start_column ), (end_line , end_column ), line in tokens :
@@ -205,14 +200,10 @@ def _get_class_column(tokens: Iterable[tokenize.TokenInfo]) -> int:
205
200
continue
206
201
207
202
return start_column
203
+ raise ValueError ("Could not find any class variables in the class." )
208
204
209
205
210
- def get_class_column (cls : type ) -> int :
211
- """Determines the column number for class variables in a class."""
212
- return _get_class_column (tokenize_source (cls ))
213
-
214
-
215
- def _source_line_to_tokens (tokens : Iterable [tokenize .TokenInfo ]) -> Dict [int , List [Dict [str , Union [str , int ]]]]:
206
+ def source_line_to_tokens (tokens : Iterable [tokenize .TokenInfo ]) -> Dict [int , List [Dict [str , Union [str , int ]]]]:
216
207
"""
217
208
Gets a dictionary mapping from line number to a dictionary of tokens on that line for an object's source code,
218
209
given the tokens of the object's source code.
@@ -232,12 +223,7 @@ def _source_line_to_tokens(tokens: Iterable[tokenize.TokenInfo]) -> Dict[int, Li
232
223
return line_to_tokens
233
224
234
225
235
- def source_line_to_tokens (obj : object ) -> Dict [int , List [Dict [str , Union [str , int ]]]]:
236
- """Gets a dictionary mapping from line number to a dictionary of tokens on that line for an object's source code."""
237
- return _source_line_to_tokens (tokenize_source (obj ))
238
-
239
-
240
- def _get_subsequent_assign_lines (source_cls : str ) -> Set [int ]:
226
+ def get_subsequent_assign_lines (source_cls : str ) -> Set [int ]:
241
227
"""
242
228
For all multiline assign statements, get the line numbers after the first line of the assignment,
243
229
given the source code of the object.
@@ -283,25 +269,22 @@ def _get_subsequent_assign_lines(source_cls: str) -> Set[int]:
283
269
284
270
return assign_lines
285
271
286
- def get_subsequent_assign_lines (cls : type ) -> Set [int ]:
287
- """For all multiline assign statements, get the line numbers after the first line of the assignment."""
288
- return _get_subsequent_assign_lines (inspect .getsource (cls ))
289
272
290
273
def get_class_variables (cls : type ) -> Dict [str , Dict [str , str ]]:
291
274
"""Returns a dictionary mapping class variables to their additional information (currently just comments)."""
292
275
# Get the source code and tokens of the class
293
276
source_cls = inspect .getsource (cls )
294
- tokens = tuple (_tokenize_source (source_cls ))
277
+ tokens = tuple (tokenize_source (source_cls ))
295
278
296
279
# Get mapping from line number to tokens
297
- line_to_tokens = _source_line_to_tokens (tokens )
280
+ line_to_tokens = source_line_to_tokens (tokens )
298
281
299
282
# Get class variable column number
300
- class_variable_column = _get_class_column (tokens )
283
+ class_variable_column = get_class_column (tokens )
301
284
302
285
# For all multiline assign statements, get the line numbers after the first line of the assignment
303
286
# This is used to avoid identifying comments in multiline assign statements
304
- subsequent_assign_lines = _get_subsequent_assign_lines (source_cls )
287
+ subsequent_assign_lines = get_subsequent_assign_lines (source_cls )
305
288
306
289
# Extract class variables
307
290
class_variable = None
0 commit comments