@@ -146,7 +146,7 @@ def get_argument_name(*name_or_flags) -> str:
146
146
return "help"
147
147
148
148
if len (name_or_flags ) > 1 :
149
- name_or_flags = [ n_or_f for n_or_f in name_or_flags if n_or_f .startswith ("--" )]
149
+ name_or_flags = tuple ( n_or_f for n_or_f in name_or_flags if n_or_f .startswith ("--" ))
150
150
151
151
if len (name_or_flags ) != 1 :
152
152
raise ValueError (f"There should only be a single canonical name for argument { name_or_flags } !" )
@@ -204,39 +204,33 @@ def get_class_column(tokens: Iterable[tokenize.TokenInfo]) -> int:
204
204
205
205
206
206
def source_line_to_tokens (tokens : Iterable [tokenize .TokenInfo ]) -> Dict [int , List [Dict [str , Union [str , int ]]]]:
207
- """
208
- Gets a dictionary mapping from line number to a dictionary of tokens on that line for an object's source code,
209
- given the tokens of the object's source code.
210
- """
207
+ """Extract a map from each line number to list of mappings providing information about each token."""
211
208
line_to_tokens = {}
212
209
for token_type , token , (start_line , start_column ), (end_line , end_column ), line in tokens :
213
- line_to_tokens .setdefault (start_line , []).append ({
214
- 'token_type' : token_type ,
215
- 'token' : token ,
216
- 'start_line' : start_line ,
217
- 'start_column' : start_column ,
218
- 'end_line' : end_line ,
219
- 'end_column' : end_column ,
220
- 'line' : line
221
- })
210
+ line_to_tokens .setdefault (start_line , []).append (
211
+ {
212
+ "token_type" : token_type ,
213
+ "token" : token ,
214
+ "start_line" : start_line ,
215
+ "start_column" : start_column ,
216
+ "end_line" : end_line ,
217
+ "end_column" : end_column ,
218
+ "line" : line ,
219
+ }
220
+ )
222
221
223
222
return line_to_tokens
224
223
225
224
226
225
def get_subsequent_assign_lines (source_cls : str ) -> Set [int ]:
227
- """
228
- For all multiline assign statements, get the line numbers after the first line of the assignment,
229
- given the source code of the object.
230
- """
231
-
226
+ """For all multiline assign statements, get the line numbers after the first line in the assignment."""
232
227
# Parse source code using ast (with an if statement to avoid indentation errors)
233
228
source = f"if True:\n { textwrap .indent (source_cls , ' ' )} "
234
229
body = ast .parse (source ).body [0 ]
235
230
236
231
# Set up warning message
237
232
parse_warning = (
238
- "Could not parse class source code to extract comments. "
239
- "Comments in the help string may be incorrect."
233
+ "Could not parse class source code to extract comments. Comments in the help string may be incorrect."
240
234
)
241
235
242
236
# Check for correct parsing
@@ -322,7 +316,7 @@ def get_class_variables(cls: type) -> Dict[str, Dict[str, str]]:
322
316
token ["token" ] = token ["token" ][num_quote_chars :- num_quote_chars ]
323
317
324
318
# Remove the unicode escape sequences (e.g. "\"")
325
- token ["token" ] = bytes (token ["token" ], encoding = ' ascii' ).decode (' unicode-escape' )
319
+ token ["token" ] = bytes (token ["token" ], encoding = " ascii" ).decode (" unicode-escape" )
326
320
327
321
# Add the token to the comment, stripping whitespace
328
322
variable_to_comment [class_variable ]["comment" ] += sep + token ["token" ].strip ()
@@ -351,7 +345,7 @@ def get_class_variables(cls: type) -> Dict[str, Dict[str, str]]:
351
345
return variable_to_comment
352
346
353
347
354
- def get_literals (literal : Literal , variable : str ) -> Tuple [Callable [[str ], Any ], List [str ]]:
348
+ def get_literals (literal : Literal , variable : str ) -> Tuple [Callable [[str ], Any ], List [type ]]:
355
349
"""Extracts the values from a Literal type and ensures that the values are all primitive types."""
356
350
literals = list (get_args (literal ))
357
351
@@ -476,7 +470,7 @@ def default(self, obj: Any) -> Any:
476
470
477
471
478
472
class UnpicklableObject :
479
- """A class that serves as a placeholder for an object that could not be pickled. """
473
+ """A class that serves as a placeholder for an object that could not be pickled."""
480
474
481
475
def __eq__ (self , other ):
482
476
return isinstance (other , UnpicklableObject )
@@ -508,33 +502,6 @@ def as_python_object(dct: Any) -> Any:
508
502
return dct
509
503
510
504
511
- def fix_py36_copy (func : Callable ) -> Callable :
512
- """Decorator that fixes functions using Python 3.6 deepcopy of ArgumentParsers.
513
-
514
- Based on https://stackoverflow.com/questions/6279305/typeerror-cannot-deepcopy-this-pattern-object
515
- """
516
- if sys .version_info [:2 ] > (3 , 6 ):
517
- return func
518
-
519
- @wraps (func )
520
- def wrapper (* args , ** kwargs ):
521
- re_type = type (re .compile ("" ))
522
- has_prev_val = re_type in copy ._deepcopy_dispatch
523
- prev_val = copy ._deepcopy_dispatch .get (re_type , None )
524
- copy ._deepcopy_dispatch [type (re .compile ("" ))] = lambda r , _ : r
525
-
526
- result = func (* args , ** kwargs )
527
-
528
- if has_prev_val :
529
- copy ._deepcopy_dispatch [re_type ] = prev_val
530
- else :
531
- del copy ._deepcopy_dispatch [re_type ]
532
-
533
- return result
534
-
535
- return wrapper
536
-
537
-
538
505
def enforce_reproducibility (
539
506
saved_reproducibility_data : Optional [Dict [str , str ]], current_reproducibility_data : Dict [str , str ], path : PathLike
540
507
) -> None :
0 commit comments