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

Function _init_fn

Lib/dataclasses.py:670–725  ·  view source on GitHub ↗
(fields, std_fields, kw_only_fields, frozen, has_post_init,
             self_name, func_builder, slots)

Source from the content-addressed store, hash-verified

668
669
670def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
671 self_name, func_builder, slots):
672 # fields contains both real fields and InitVar pseudo-fields.
673
674 # Make sure we don't have fields without defaults following fields
675 # with defaults. This actually would be caught when exec-ing the
676 # function source code, but catching it here gives a better error
677 # message, and future-proofs us in case we build up the function
678 # using ast.
679
680 seen_default = None
681 for f in std_fields:
682 # Only consider the non-kw-only fields in the __init__ call.
683 if f.init:
684 if not (f.default is MISSING and f.default_factory is MISSING):
685 seen_default = f
686 elif seen_default:
687 raise TypeError(f'non-default argument {f.name!r} '
688 f'follows default argument {seen_default.name!r}')
689
690 annotation_fields = [f.name for f in fields if f.init]
691
692 locals = {'__dataclass_HAS_DEFAULT_FACTORY__': _HAS_DEFAULT_FACTORY,
693 '__dataclass_builtins_object__': object}
694
695 body_lines = []
696 for f in fields:
697 line = _field_init(f, frozen, locals, self_name, slots)
698 # line is None means that this field doesn't require
699 # initialization (it's a pseudo-field). Just skip it.
700 if line:
701 body_lines.append(line)
702
703 # Does this class have a post-init function?
704 if has_post_init:
705 params_str = ','.join(f.name for f in fields
706 if f._field_type is _FIELD_INITVAR)
707 body_lines.append(f' {self_name}.{_POST_INIT_NAME}({params_str})')
708
709 # If no body lines, use 'pass'.
710 if not body_lines:
711 body_lines = [' pass']
712
713 _init_params = [_init_param(f) for f in std_fields]
714 if kw_only_fields:
715 # Add the keyword-only args. Because the * can only be added if
716 # there's at least one keyword-only arg, there needs to be a test here
717 # (instead of just concatenating the lists together).
718 _init_params += ['*']
719 _init_params += [_init_param(f) for f in kw_only_fields]
720 func_builder.add_fn('__init__',
721 [self_name] + _init_params,
722 body_lines,
723 locals=locals,
724 return_type=None,
725 annotation_fields=annotation_fields)
726
727

Callers 1

_process_classFunction · 0.85

Calls 5

_field_initFunction · 0.85
_init_paramFunction · 0.85
add_fnMethod · 0.80
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…