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,
)
| 1801 | |
| 1802 | |
| 1803 | def 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, |
no test coverage detected