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

Function is_borrow_friendly_expr

mypyc/irbuild/ast_helpers.py:103–123  ·  view source on GitHub ↗

Can the result of the expression borrowed temporarily? Borrowing means keeping a reference without incrementing the reference count.

(self: IRBuilder, expr: Expression)

Source from the content-addressed store, hash-verified

101
102
103def is_borrow_friendly_expr(self: IRBuilder, expr: Expression) -> bool:
104 """Can the result of the expression borrowed temporarily?
105
106 Borrowing means keeping a reference without incrementing the reference count.
107 """
108 if isinstance(expr, (IntExpr, FloatExpr, StrExpr, BytesExpr)):
109 # Literals are immortal and can always be borrowed
110 return True
111 if (
112 isinstance(expr, (UnaryExpr, OpExpr, NameExpr, MemberExpr))
113 and constant_fold_expr(self, expr) is not None
114 ):
115 # Literal expressions are similar to literals
116 return True
117 if isinstance(expr, NameExpr):
118 if isinstance(expr.node, Var) and expr.kind == LDEF:
119 # Local variable reference can be borrowed
120 return True
121 if isinstance(expr, MemberExpr) and self.is_native_attr_ref(expr):
122 return True
123 return False

Callers 5

transform_op_exprFunction · 0.90
transform_index_exprFunction · 0.90

Calls 3

constant_fold_exprFunction · 0.90
isinstanceFunction · 0.85
is_native_attr_refMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…