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

Function maybe_make_parens_invisible_in_atom

src/black/linegen.py:1803–1913  ·  view source on GitHub ↗

If it's safe, make the parens in the atom `node` invisible, recursively. Additionally, remove repeated, adjacent invisible parens from the atom `node` as they are redundant. Returns whether the node should itself be wrapped in invisible parentheses.

(
    node: LN,
    parent: LN,
    mode: Mode,
    features: Collection[Feature],
    remove_brackets_around_comma: bool = False,
    allow_star_expr: bool = False,
)

Source from the content-addressed store, hash-verified

1801
1802
1803def maybe_make_parens_invisible_in_atom(
1804 node: LN,
1805 parent: LN,
1806 mode: Mode,
1807 features: Collection[Feature],
1808 remove_brackets_around_comma: bool = False,
1809 allow_star_expr: bool = False,
1810) -> bool:
1811 """If it's safe, make the parens in the atom `node` invisible, recursively.
1812 Additionally, remove repeated, adjacent invisible parens from the atom `node`
1813 as they are redundant.
1814
1815 Returns whether the node should itself be wrapped in invisible parentheses.
1816 """
1817 if (
1818 node.type not in (syms.atom, syms.expr)
1819 or is_empty_tuple(node)
1820 or is_one_tuple(node)
1821 or (is_tuple(node) and parent.type == syms.asexpr_test)
1822 or (
1823 is_tuple(node)
1824 and parent.type == syms.with_stmt
1825 and has_sibling_with_type(node, token.COMMA)
1826 )
1827 or (is_yield(node) and parent.type != syms.expr_stmt)
1828 or (
1829 # This condition tries to prevent removing non-optional brackets
1830 # around a tuple, however, can be a bit overzealous so we provide
1831 # and option to skip this check for `for` and `with` statements.
1832 not remove_brackets_around_comma
1833 and max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
1834 # Remove parentheses around multiple exception types in except and
1835 # except* without as. See PEP 758 for details.
1836 and not (
1837 Feature.UNPARENTHESIZED_EXCEPT_TYPES in features
1838 # is a tuple
1839 and is_tuple(node)
1840 # has a parent node
1841 and node.parent is not None
1842 # parent is an except clause
1843 and node.parent.type == syms.except_clause
1844 # is not immediately followed by as clause
1845 and not (
1846 node.next_sibling is not None
1847 and is_name_token(node.next_sibling)
1848 and node.next_sibling.value == "as"
1849 )
1850 )
1851 )
1852 or is_tuple_containing_walrus(node)
1853 or (not allow_star_expr and is_tuple_containing_star(node))
1854 or is_generator(node)
1855 ):
1856 return False
1857
1858 if is_walrus_assignment(node):
1859 if parent.type in [
1860 syms.annassign,

Callers 7

visit_dictsetmakerMethod · 0.85
visit_funcdefMethod · 0.85
visit_tnameMethod · 0.85
visit_atomMethod · 0.85
remove_await_parensFunction · 0.85
remove_with_parensFunction · 0.85

Calls 15

is_empty_tupleFunction · 0.90
is_one_tupleFunction · 0.90
is_tupleFunction · 0.90
has_sibling_with_typeFunction · 0.90
is_yieldFunction · 0.90
is_name_tokenFunction · 0.90
is_tuple_containing_starFunction · 0.90
is_generatorFunction · 0.90
is_walrus_assignmentFunction · 0.90
is_lpar_tokenFunction · 0.90

Tested by

no test coverage detected