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

Function gen_glue_ne_method

mypyc/irbuild/classdef.py:863–902  ·  view source on GitHub ↗

Generate a "__ne__" method from a "__eq__" method.

(builder: IRBuilder, cls: ClassIR, line: int)

Source from the content-addressed store, hash-verified

861
862
863def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None:
864 """Generate a "__ne__" method from a "__eq__" method."""
865 func_ir = cls.get_method("__eq__")
866 assert func_ir
867 eq_sig = func_ir.decl.sig
868 strict_typing = builder.options.strict_dunders_typing
869 with builder.enter_method(cls, "__ne__", eq_sig.ret_type):
870 rhs_type = eq_sig.args[0].type if strict_typing else object_rprimitive
871 rhs_arg = builder.add_argument("rhs", rhs_type)
872 eqval = builder.add(MethodCall(builder.self(), "__eq__", [rhs_arg], line))
873
874 can_return_not_implemented = is_subtype(not_implemented_op.type, eq_sig.ret_type)
875 return_bool = is_subtype(eq_sig.ret_type, bool_rprimitive)
876
877 if not strict_typing or can_return_not_implemented:
878 # If __eq__ returns NotImplemented, then __ne__ should also
879 not_implemented_block, regular_block = BasicBlock(), BasicBlock()
880 not_implemented = builder.add(
881 LoadAddress(not_implemented_op.type, not_implemented_op.src, line)
882 )
883 builder.add(
884 Branch(
885 builder.translate_is_op(eqval, not_implemented, "is", line),
886 not_implemented_block,
887 regular_block,
888 Branch.BOOL,
889 )
890 )
891 builder.activate_block(regular_block)
892 rettype = bool_rprimitive if return_bool and strict_typing else object_rprimitive
893 retval = builder.coerce(
894 builder.builder.unary_not(eqval, line, likely_bool=True), rettype, line
895 )
896 builder.add(Return(retval))
897 builder.activate_block(not_implemented_block)
898 builder.add(Return(not_implemented))
899 else:
900 rettype = bool_rprimitive if return_bool and strict_typing else object_rprimitive
901 retval = builder.coerce(builder.unary_op(eqval, "not", line), rettype, line)
902 builder.add(Return(retval))
903
904
905def load_non_ext_class(

Callers 1

create_ne_from_eqFunction · 0.85

Calls 15

MethodCallClass · 0.90
is_subtypeFunction · 0.90
BasicBlockClass · 0.90
LoadAddressClass · 0.90
BranchClass · 0.90
ReturnClass · 0.90
add_argumentMethod · 0.80
unary_notMethod · 0.80
get_methodMethod · 0.45
enter_methodMethod · 0.45
addMethod · 0.45
selfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…