MCPcopy Index your code
hub / github.com/python/cpython / add_fn

Method add_fn

Lib/dataclasses.py:446–476  ·  view source on GitHub ↗
(self, name, args, body, *, locals=None, return_type=MISSING,
               overwrite_error=False, unconditional_add=False, decorator=None,
               annotation_fields=None)

Source from the content-addressed store, hash-verified

444 self.method_annotations = {}
445
446 def add_fn(self, name, args, body, *, locals=None, return_type=MISSING,
447 overwrite_error=False, unconditional_add=False, decorator=None,
448 annotation_fields=None):
449 if locals is not None:
450 self.locals.update(locals)
451
452 # Keep track if this method is allowed to be overwritten if it already
453 # exists in the class. The error is method-specific, so keep it with
454 # the name. We'll use this when we generate all of the functions in
455 # the add_fns_to_class call. overwrite_error is either True, in which
456 # case we'll raise an error, or it's a string, in which case we'll
457 # raise an error and append this string.
458 if overwrite_error:
459 self.overwrite_errors[name] = overwrite_error
460
461 # Should this function always overwrite anything that's already in the
462 # class? The default is to not overwrite a function that already
463 # exists.
464 if unconditional_add:
465 self.unconditional_adds[name] = True
466
467 self.names.append(name)
468
469 if annotation_fields is not None:
470 self.method_annotations[name] = (annotation_fields, return_type)
471
472 args = ','.join(args)
473 body = '\n'.join(body)
474
475 # Compute the text of the entire function, add it to the text we're generating.
476 self.src.append(f'{f' {decorator}\n' if decorator else ''} def {name}({args}):\n{body}')
477
478 def add_fns_to_class(self, cls):
479 # The source to all of the functions we're generating.

Callers 4

_init_fnFunction · 0.80
_frozen_get_del_attrFunction · 0.80
_hash_addFunction · 0.80
_process_classFunction · 0.80

Calls 3

updateMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected