MCPcopy Create free account
hub / github.com/python/mypy / add_property_methods_for_attribute_if_needed

Function add_property_methods_for_attribute_if_needed

mypyc/irbuild/prepare.py:601–629  ·  view source on GitHub ↗

Add getter and/or setter for attribute if defined as property in a base class. Only add declarations. The body IR will be synthesized later during irbuild.

(
    info: TypeInfo,
    ir: ClassIR,
    attr_name: str,
    attr_rtype: RType,
    module_name: str,
    mapper: Mapper,
)

Source from the content-addressed store, hash-verified

599
600
601def add_property_methods_for_attribute_if_needed(
602 info: TypeInfo,
603 ir: ClassIR,
604 attr_name: str,
605 attr_rtype: RType,
606 module_name: str,
607 mapper: Mapper,
608) -> None:
609 """Add getter and/or setter for attribute if defined as property in a base class.
610
611 Only add declarations. The body IR will be synthesized later during irbuild.
612 """
613 for base in info.mro[1:]:
614 if base in mapper.type_to_ir:
615 base_ir = mapper.type_to_ir[base]
616 n = base.names.get(attr_name)
617 if n is None:
618 continue
619 node = n.node
620 if isinstance(node, Decorator) and node.name not in ir.method_decls:
621 # Defined as a read-only property in base class/trait
622 add_getter_declaration(ir, attr_name, attr_rtype, module_name)
623 elif isinstance(node, OverloadedFuncDef) and is_valid_multipart_property_def(node):
624 # Defined as a read-write property in base class/trait
625 add_getter_declaration(ir, attr_name, attr_rtype, module_name)
626 add_setter_declaration(ir, attr_name, attr_rtype, module_name)
627 elif base_ir.is_trait and attr_rtype.error_overlap:
628 add_getter_declaration(ir, attr_name, attr_rtype, module_name)
629 add_setter_declaration(ir, attr_name, attr_rtype, module_name)
630
631
632def add_getter_declaration(

Callers 1

Calls 5

isinstanceFunction · 0.85
add_getter_declarationFunction · 0.85
add_setter_declarationFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…