MCPcopy Index your code
hub / github.com/python/mypy / generate_attr_defaults_init

Function generate_attr_defaults_init

mypyc/irbuild/classdef.py:800–833  ·  view source on GitHub ↗

Generate an initialization method for default attr values (from class vars).

(
    builder: IRBuilder, cdef: ClassDef, default_assignments: list[tuple[AssignmentStmt, str]]
)

Source from the content-addressed store, hash-verified

798
799
800def generate_attr_defaults_init(
801 builder: IRBuilder, cdef: ClassDef, default_assignments: list[tuple[AssignmentStmt, str]]
802) -> None:
803 """Generate an initialization method for default attr values (from class vars)."""
804 if not default_assignments:
805 return
806 cls = builder.mapper.type_to_ir[cdef.info]
807 if cls.builtin_base:
808 return
809
810 with builder.enter_method(cls, "__mypyc_defaults_setup", bool_rprimitive):
811 self_var = builder.self()
812 for stmt, origin_module in default_assignments:
813 lvalue = stmt.lvalues[0]
814 assert isinstance(lvalue, NameExpr), lvalue
815 if not stmt.is_final_def and not is_constant(stmt.rvalue):
816 builder.warning("Unsupported default attribute value", stmt.rvalue.line)
817
818 attr_type = cls.attr_type(lvalue.name)
819 # When the default comes from a parent in a different module,
820 # set the globals lookup module so NameExpr references resolve
821 # against the correct module's globals dict.
822 builder.globals_lookup_module = (
823 origin_module if origin_module != builder.module_name else None
824 )
825 try:
826 val = builder.coerce(builder.accept(stmt.rvalue), attr_type, stmt.line)
827 finally:
828 builder.globals_lookup_module = None
829 init = SetAttr(self_var, lvalue.name, val, stmt.rvalue.line)
830 init.mark_as_initializer()
831 builder.add(init)
832
833 builder.add(Return(builder.true()))
834
835
836def check_deletable_declaration(builder: IRBuilder, cl: ClassIR, line: int) -> None:

Callers 1

finalizeMethod · 0.85

Calls 13

mark_as_initializerMethod · 0.95
is_constantFunction · 0.90
SetAttrClass · 0.90
ReturnClass · 0.90
isinstanceFunction · 0.85
enter_methodMethod · 0.45
selfMethod · 0.45
warningMethod · 0.45
attr_typeMethod · 0.45
coerceMethod · 0.45
acceptMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…