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

Function maybe_parenthesize

Tools/cases_generator/stack.py:12–24  ·  view source on GitHub ↗

Add parentheses around a string if it contains an operator and is not already parenthesized. An exception is made for '*' which is common and harmless in the context where the symbolic size is used.

(sym: str)

Source from the content-addressed store, hash-verified

10PRINT_STACKS = False
11
12def maybe_parenthesize(sym: str) -> str:
13 """Add parentheses around a string if it contains an operator
14 and is not already parenthesized.
15
16 An exception is made for '*' which is common and harmless
17 in the context where the symbolic size is used.
18 """
19 if sym.startswith("(") and sym.endswith(")"):
20 return sym
21 if re.match(r"^[\s\w*]+$", sym):
22 return sym
23 else:
24 return f"({sym})"
25
26
27def var_size(var: StackItem) -> str:

Callers 1

to_cMethod · 0.85

Calls 3

startswithMethod · 0.45
endswithMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…