MCPcopy
hub / github.com/psf/black / _maybe_wrap_cms_in_parens

Function _maybe_wrap_cms_in_parens

src/black/linegen.py:1686–1725  ·  view source on GitHub ↗

When enabled and safe, wrap the multiple context managers in invisible parens. It is only safe when `features` contain Feature.PARENTHESIZED_CONTEXT_MANAGERS.

(
    node: Node, mode: Mode, features: Collection[Feature]
)

Source from the content-addressed store, hash-verified

1684
1685
1686def _maybe_wrap_cms_in_parens(
1687 node: Node, mode: Mode, features: Collection[Feature]
1688) -> None:
1689 """When enabled and safe, wrap the multiple context managers in invisible parens.
1690
1691 It is only safe when `features` contain Feature.PARENTHESIZED_CONTEXT_MANAGERS.
1692 """
1693 if (
1694 Feature.PARENTHESIZED_CONTEXT_MANAGERS not in features
1695 or len(node.children) <= 2
1696 # If it's an atom, it's already wrapped in parens.
1697 or node.children[1].type == syms.atom
1698 ):
1699 return
1700 colon_index: int | None = None
1701 for i in range(2, len(node.children)):
1702 if node.children[i].type == token.COLON:
1703 colon_index = i
1704 break
1705 if colon_index is not None:
1706 lpar = Leaf(token.LPAR, "")
1707 rpar = Leaf(token.RPAR, "")
1708 context_managers = node.children[1:colon_index]
1709 for child in context_managers:
1710 child.remove()
1711 # After wrapping, the with_stmt will look like this:
1712 # with_stmt
1713 # NAME 'with'
1714 # atom
1715 # LPAR ''
1716 # testlist_gexp
1717 # ... <-- context_managers
1718 # /testlist_gexp
1719 # RPAR ''
1720 # /atom
1721 # COLON ':'
1722 new_child = Node(
1723 syms.atom, [lpar, Node(syms.testlist_gexp, context_managers), rpar]
1724 )
1725 node.insert_child(1, new_child)
1726
1727
1728def remove_with_parens(

Callers 1

Calls 4

LeafClass · 0.90
NodeClass · 0.90
removeMethod · 0.80
insert_childMethod · 0.80

Tested by

no test coverage detected