Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/black/nodes.py: 19%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2blib2to3 Node/Leaf transformation-related utility functions.
3"""
5from collections.abc import Iterator
6from typing import Final, Generic, Literal, TypeGuard, TypeVar, Union
8from mypy_extensions import mypyc_attr
10from black.cache import CACHE_DIR
11from black.mode import Mode, Preview
12from black.strings import get_string_prefix, has_triple_quotes
13from blib2to3 import pygram
14from blib2to3.pgen2 import token
15from blib2to3.pytree import NL, Leaf, Node, type_repr
17pygram.initialize(CACHE_DIR)
18syms: Final = pygram.python_symbols
21# types
22T = TypeVar("T")
23LN = Union[Leaf, Node]
24LeafID = int
25NodeType = int
28WHITESPACE: Final = {token.DEDENT, token.INDENT, token.NEWLINE}
29STATEMENT: Final = {
30 syms.if_stmt,
31 syms.while_stmt,
32 syms.for_stmt,
33 syms.try_stmt,
34 syms.except_clause,
35 syms.with_stmt,
36 syms.funcdef,
37 syms.classdef,
38 syms.match_stmt,
39 syms.case_block,
40}
41STANDALONE_COMMENT: Final = 153
42token.tok_name[STANDALONE_COMMENT] = "STANDALONE_COMMENT"
43LOGIC_OPERATORS: Final = {"and", "or"}
44COMPARATORS: Final = {
45 token.LESS,
46 token.GREATER,
47 token.EQEQUAL,
48 token.NOTEQUAL,
49 token.LESSEQUAL,
50 token.GREATEREQUAL,
51}
52MATH_OPERATORS: Final = {
53 token.VBAR,
54 token.CIRCUMFLEX,
55 token.AMPER,
56 token.LEFTSHIFT,
57 token.RIGHTSHIFT,
58 token.PLUS,
59 token.MINUS,
60 token.STAR,
61 token.SLASH,
62 token.DOUBLESLASH,
63 token.PERCENT,
64 token.AT,
65 token.TILDE,
66 token.DOUBLESTAR,
67}
68STARS: Final = {token.STAR, token.DOUBLESTAR}
69VARARGS_SPECIALS: Final = STARS | {token.SLASH}
70VARARGS_PARENTS: Final = {
71 syms.arglist,
72 syms.argument, # double star in arglist
73 syms.trailer, # single argument to call
74 syms.typedargslist,
75 syms.varargslist, # lambdas
76}
77UNPACKING_PARENTS: Final = {
78 syms.atom, # single element of a list or set literal
79 syms.dictsetmaker,
80 syms.listmaker,
81 syms.testlist_gexp,
82 syms.testlist_star_expr,
83 syms.subject_expr,
84 syms.pattern,
85}
86TEST_DESCENDANTS: Final = {
87 syms.test,
88 syms.lambdef,
89 syms.or_test,
90 syms.and_test,
91 syms.not_test,
92 syms.comparison,
93 syms.star_expr,
94 syms.expr,
95 syms.xor_expr,
96 syms.and_expr,
97 syms.shift_expr,
98 syms.arith_expr,
99 syms.trailer,
100 syms.term,
101 syms.power,
102 syms.namedexpr_test,
103}
104TYPED_NAMES: Final = {syms.tname, syms.tname_star}
105ASSIGNMENTS: Final = {
106 "=",
107 "+=",
108 "-=",
109 "*=",
110 "@=",
111 "/=",
112 "%=",
113 "&=",
114 "|=",
115 "^=",
116 "<<=",
117 ">>=",
118 "**=",
119 "//=",
120 ":",
121}
123IMPLICIT_TUPLE: Final = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
124BRACKET: Final = {
125 token.LPAR: token.RPAR,
126 token.LSQB: token.RSQB,
127 token.LBRACE: token.RBRACE,
128}
129OPENING_BRACKETS: Final = set(BRACKET.keys())
130CLOSING_BRACKETS: Final = set(BRACKET.values())
131BRACKETS: Final = OPENING_BRACKETS | CLOSING_BRACKETS
132ALWAYS_NO_SPACE: Final = CLOSING_BRACKETS | {
133 token.COMMA,
134 STANDALONE_COMMENT,
135 token.FSTRING_MIDDLE,
136 token.FSTRING_END,
137 token.TSTRING_MIDDLE,
138 token.TSTRING_END,
139 token.BANG,
140}
142RARROW = 55
145@mypyc_attr(allow_interpreted_subclasses=True)
146class Visitor(Generic[T]):
147 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
149 def visit(self, node: LN) -> Iterator[T]:
150 """Main method to visit `node` and its children.
152 It tries to find a `visit_*()` method for the given `node.type`, like
153 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
154 If no dedicated `visit_*()` method is found, chooses `visit_default()`
155 instead.
157 Then yields objects of type `T` from the selected visitor.
158 """
159 if node.type < 256:
160 name = token.tok_name[node.type]
161 else:
162 name = str(type_repr(node.type))
163 # We explicitly branch on whether a visitor exists (instead of
164 # using self.visit_default as the default arg to getattr) in order
165 # to save needing to create a bound method object and so mypyc can
166 # generate a native call to visit_default.
167 visitf = getattr(self, f"visit_{name}", None)
168 if visitf:
169 yield from visitf(node)
170 else:
171 yield from self.visit_default(node)
173 def visit_default(self, node: LN) -> Iterator[T]:
174 """Default `visit_*()` implementation. Recurses to children of `node`."""
175 if isinstance(node, Node):
176 for child in node.children:
177 yield from self.visit(child)
180def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str: # noqa: C901
181 """Return whitespace prefix if needed for the given `leaf`.
183 `complex_subscript` signals whether the given leaf is part of a subscription
184 which has non-trivial arguments, like arithmetic expressions or function calls.
185 """
186 NO: Final[str] = ""
187 SPACE: Final[str] = " "
188 DOUBLESPACE: Final[str] = " "
189 t = leaf.type
190 p = leaf.parent
191 v = leaf.value
192 if t in ALWAYS_NO_SPACE:
193 return NO
195 if t == token.COMMENT:
196 return DOUBLESPACE
198 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
199 if t == token.COLON and p.type not in {
200 syms.subscript,
201 syms.subscriptlist,
202 syms.sliceop,
203 }:
204 return NO
206 if t == token.LBRACE and p.type in (
207 syms.fstring_replacement_field,
208 syms.tstring_replacement_field,
209 ):
210 return NO
212 prev = leaf.prev_sibling
213 if not prev:
214 prevp = preceding_leaf(p)
215 if not prevp or prevp.type in OPENING_BRACKETS:
216 return NO
218 if t == token.COLON:
219 if prevp.type == token.COLON:
220 return NO
222 elif prevp.type != token.COMMA and not complex_subscript:
223 return NO
225 return SPACE
227 if prevp.type == token.EQUAL:
228 if prevp.parent:
229 if prevp.parent.type in {
230 syms.arglist,
231 syms.argument,
232 syms.parameters,
233 syms.varargslist,
234 }:
235 return NO
237 elif prevp.parent.type == syms.typedargslist:
238 # A bit hacky: if the equal sign has whitespace, it means we
239 # previously found it's a typed argument. So, we're using
240 # that, too.
241 return prevp.prefix
243 elif (
244 prevp.type == token.STAR
245 and parent_type(prevp) == syms.star_expr
246 and parent_type(prevp.parent) in (syms.subscriptlist, syms.tname_star)
247 ):
248 # No space between typevar tuples or unpacking them.
249 return NO
251 elif prevp.type in VARARGS_SPECIALS:
252 if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
253 return NO
255 elif prevp.type == token.COLON:
256 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
257 return SPACE if complex_subscript else NO
259 elif (
260 prevp.parent
261 and prevp.parent.type == syms.factor
262 and prevp.type in MATH_OPERATORS
263 ):
264 return NO
266 elif prevp.type == token.AT and p.parent and p.parent.type == syms.decorator:
267 # no space in decorators
268 return NO
270 elif prev.type in OPENING_BRACKETS:
271 return NO
273 elif prev.type == token.BANG:
274 return NO
276 if p.type in {syms.parameters, syms.arglist}:
277 # untyped function signatures or calls
278 if not prev or prev.type != token.COMMA:
279 return NO
281 elif p.type == syms.varargslist:
282 # lambdas
283 if prev and prev.type != token.COMMA:
284 return NO
286 elif p.type == syms.typedargslist:
287 # typed function signatures
288 if not prev:
289 return NO
291 if t == token.EQUAL:
292 if prev.type not in TYPED_NAMES:
293 return NO
295 elif prev.type == token.EQUAL:
296 # A bit hacky: if the equal sign has whitespace, it means we
297 # previously found it's a typed argument. So, we're using that, too.
298 return prev.prefix
300 elif prev.type != token.COMMA:
301 return NO
303 elif p.type in TYPED_NAMES:
304 # type names
305 if not prev:
306 prevp = preceding_leaf(p)
307 if not prevp or prevp.type != token.COMMA:
308 return NO
310 elif p.type == syms.trailer:
311 # attributes and calls
312 if t == token.LPAR or t == token.RPAR:
313 return NO
315 if not prev:
316 if t == token.DOT or t == token.LSQB:
317 return NO
319 elif prev.type != token.COMMA:
320 return NO
322 elif p.type == syms.argument:
323 # single argument
324 if t == token.EQUAL:
325 return NO
327 if not prev:
328 prevp = preceding_leaf(p)
329 if not prevp or prevp.type == token.LPAR:
330 return NO
332 elif prev.type in {token.EQUAL} | VARARGS_SPECIALS:
333 return NO
335 elif p.type == syms.decorator:
336 # decorators
337 return NO
339 elif p.type == syms.dotted_name:
340 if prev:
341 return NO
343 prevp = preceding_leaf(p)
344 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
345 return NO
347 elif p.type == syms.classdef:
348 if t == token.LPAR:
349 return NO
351 if prev and prev.type == token.LPAR:
352 return NO
354 elif p.type in {syms.subscript, syms.sliceop}:
355 # indexing
356 if not prev:
357 assert p.parent is not None, "subscripts are always parented"
358 if p.parent.type == syms.subscriptlist:
359 return SPACE
361 return NO
363 elif t == token.COLONEQUAL or prev.type == token.COLONEQUAL:
364 return SPACE
366 elif not complex_subscript:
367 return NO
369 elif p.type == syms.atom:
370 if prev and t == token.DOT:
371 # dots, but not the first one.
372 return NO
374 elif p.type == syms.dictsetmaker:
375 # dict unpacking
376 if prev and prev.type == token.DOUBLESTAR:
377 return NO
379 elif p.type in {syms.factor, syms.star_expr}:
380 # unary ops
381 if not prev:
382 prevp = preceding_leaf(p)
383 if not prevp or prevp.type in OPENING_BRACKETS:
384 return NO
386 prevp_parent = prevp.parent
387 assert prevp_parent is not None
388 if prevp.type == token.COLON and prevp_parent.type in {
389 syms.subscript,
390 syms.sliceop,
391 }:
392 return NO
394 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
395 return NO
397 elif t in {token.NAME, token.NUMBER, token.STRING}:
398 return NO
400 elif p.type == syms.import_from:
401 if t == token.DOT:
402 if prev and prev.type == token.DOT:
403 return NO
405 elif t == token.NAME:
406 if v == "import":
407 return SPACE
409 if prev and prev.type == token.DOT:
410 return NO
412 elif p.type == syms.sliceop:
413 return NO
415 elif p.type == syms.except_clause:
416 if t == token.STAR:
417 return NO
419 return SPACE
422def make_simple_prefix(nl_count: int, form_feed: bool, empty_line: str = "\n") -> str:
423 """Generate a normalized prefix string."""
424 if form_feed:
425 return (empty_line * (nl_count - 1)) + "\f" + empty_line
426 return empty_line * nl_count
429def preceding_leaf(node: LN | None) -> Leaf | None:
430 """Return the first leaf that precedes `node`, if any."""
431 while node:
432 res = node.prev_sibling
433 if res:
434 if isinstance(res, Leaf):
435 return res
437 try:
438 return list(res.leaves())[-1]
440 except IndexError:
441 return None
443 node = node.parent
444 return None
447def prev_siblings_are(node: LN | None, tokens: list[NodeType | None]) -> bool:
448 """Return if the `node` and its previous siblings match types against the provided
449 list of tokens; the provided `node`has its type matched against the last element in
450 the list. `None` can be used as the first element to declare that the start of the
451 list is anchored at the start of its parent's children."""
452 if not tokens:
453 return True
454 if tokens[-1] is None:
455 return node is None
456 if not node:
457 return False
458 if node.type != tokens[-1]:
459 return False
460 return prev_siblings_are(node.prev_sibling, tokens[:-1])
463def parent_type(node: LN | None) -> NodeType | None:
464 """
465 Returns:
466 @node.parent.type, if @node is not None and has a parent.
467 OR
468 None, otherwise.
469 """
470 if node is None or node.parent is None:
471 return None
473 return node.parent.type
476def child_towards(ancestor: Node, descendant: LN) -> LN | None:
477 """Return the child of `ancestor` that contains `descendant`."""
478 node: LN | None = descendant
479 while node and node.parent != ancestor:
480 node = node.parent
481 return node
484def replace_child(old_child: LN, new_child: LN) -> None:
485 """
486 Side Effects:
487 * If @old_child.parent is set, replace @old_child with @new_child in
488 @old_child's underlying Node structure.
489 OR
490 * Otherwise, this function does nothing.
491 """
492 parent = old_child.parent
493 if not parent:
494 return
496 child_idx = old_child.remove()
497 if child_idx is not None:
498 parent.insert_child(child_idx, new_child)
501def container_of(leaf: Leaf) -> LN:
502 """Return `leaf` or one of its ancestors that is the topmost container of it.
504 By "container" we mean a node where `leaf` is the very first child.
505 """
506 same_prefix = leaf.prefix
507 container: LN = leaf
508 while container:
509 parent = container.parent
510 if parent is None:
511 break
513 if parent.children[0].prefix != same_prefix:
514 break
516 if parent.type == syms.file_input:
517 break
519 if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS:
520 break
522 container = parent
523 return container
526def first_leaf_of(node: LN) -> Leaf | None:
527 """Returns the first leaf of the node tree."""
528 if isinstance(node, Leaf):
529 return node
530 if node.children:
531 return first_leaf_of(node.children[0])
532 else:
533 return None
536def is_arith_like(node: LN) -> bool:
537 """Whether node is an arithmetic or a binary arithmetic expression"""
538 return node.type in {
539 syms.arith_expr,
540 syms.shift_expr,
541 syms.xor_expr,
542 syms.and_expr,
543 }
546def is_docstring(node: NL) -> bool:
547 if isinstance(node, Leaf):
548 if node.type != token.STRING:
549 return False
551 prefix = get_string_prefix(node.value)
552 if set(prefix).intersection("bBfF"):
553 return False
555 if (
556 node.parent
557 and node.parent.type == syms.simple_stmt
558 and not node.parent.prev_sibling
559 and node.parent.parent
560 and node.parent.parent.type == syms.file_input
561 ):
562 return True
564 if prev_siblings_are(
565 node.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
566 ):
567 return True
569 # Multiline docstring on the same line as the `def`.
570 if prev_siblings_are(node.parent, [syms.parameters, token.COLON, syms.simple_stmt]):
571 # `syms.parameters` is only used in funcdefs and async_funcdefs in the Python
572 # grammar. We're safe to return True without further checks.
573 return True
575 return False
578def is_empty_tuple(node: LN) -> bool:
579 """Return True if `node` holds an empty tuple."""
580 return (
581 node.type == syms.atom
582 and len(node.children) == 2
583 and node.children[0].type == token.LPAR
584 and node.children[1].type == token.RPAR
585 )
588def is_one_tuple(node: LN) -> bool:
589 """Return True if `node` holds a tuple with one element, with or without parens."""
590 if node.type == syms.atom:
591 gexp = unwrap_singleton_parenthesis(node)
592 if gexp is None or gexp.type != syms.testlist_gexp:
593 return False
595 return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
597 return (
598 node.type in IMPLICIT_TUPLE
599 and len(node.children) == 2
600 and node.children[1].type == token.COMMA
601 )
604def is_tuple(node: LN) -> bool:
605 """Return True if `node` holds a tuple."""
606 if node.type != syms.atom:
607 return False
608 gexp = unwrap_singleton_parenthesis(node)
609 if gexp is None or gexp.type != syms.testlist_gexp:
610 return False
612 return True
615def is_tuple_containing_walrus(node: LN) -> bool:
616 """Return True if `node` holds a tuple that contains a walrus operator."""
617 if node.type != syms.atom:
618 return False
619 gexp = unwrap_singleton_parenthesis(node)
620 if gexp is None or gexp.type != syms.testlist_gexp:
621 return False
623 return any(child.type == syms.namedexpr_test for child in gexp.children)
626def is_tuple_containing_star(node: LN) -> bool:
627 """Return True if `node` holds a tuple that contains a star operator."""
628 if node.type != syms.atom:
629 return False
630 gexp = unwrap_singleton_parenthesis(node)
631 if gexp is None or gexp.type != syms.testlist_gexp:
632 return False
634 return any(child.type == syms.star_expr for child in gexp.children)
637def is_generator(node: LN) -> bool:
638 """Return True if `node` holds a generator."""
639 if node.type != syms.atom:
640 return False
641 gexp = unwrap_singleton_parenthesis(node)
642 if gexp is None or gexp.type != syms.testlist_gexp:
643 return False
645 return any(child.type == syms.old_comp_for for child in gexp.children)
648def is_one_sequence_between(
649 opening: Leaf,
650 closing: Leaf,
651 leaves: list[Leaf],
652 brackets: tuple[int, int] = (token.LPAR, token.RPAR),
653) -> bool:
654 """Return True if content between `opening` and `closing` is a one-sequence."""
655 if (opening.type, closing.type) != brackets:
656 return False
658 depth = closing.bracket_depth + 1
659 for _opening_index, leaf in enumerate(leaves):
660 if leaf is opening:
661 break
663 else:
664 raise LookupError("Opening paren not found in `leaves`")
666 commas = 0
667 _opening_index += 1
668 for leaf in leaves[_opening_index:]:
669 if leaf is closing:
670 break
672 bracket_depth = leaf.bracket_depth
673 if bracket_depth == depth and leaf.type == token.COMMA:
674 commas += 1
675 if leaf.parent and leaf.parent.type in {
676 syms.arglist,
677 syms.typedargslist,
678 }:
679 commas += 1
680 break
682 return commas < 2
685def is_walrus_assignment(node: LN) -> bool:
686 """Return True iff `node` is of the shape ( test := test )"""
687 inner = unwrap_singleton_parenthesis(node)
688 return inner is not None and inner.type == syms.namedexpr_test
691def is_simple_decorator_trailer(node: LN, last: bool = False) -> bool:
692 """Return True iff `node` is a trailer valid in a simple decorator"""
693 return node.type == syms.trailer and (
694 (
695 len(node.children) == 2
696 and node.children[0].type == token.DOT
697 and node.children[1].type == token.NAME
698 )
699 # last trailer can be an argument-less parentheses pair
700 or (
701 last
702 and len(node.children) == 2
703 and node.children[0].type == token.LPAR
704 and node.children[1].type == token.RPAR
705 )
706 # last trailer can be arguments
707 or (
708 last
709 and len(node.children) == 3
710 and node.children[0].type == token.LPAR
711 # and node.children[1].type == syms.argument
712 and node.children[2].type == token.RPAR
713 )
714 )
717def is_simple_decorator_expression(node: LN) -> bool:
718 """Return True iff `node` could be a 'dotted name' decorator
720 This function takes the node of the 'namedexpr_test' of the new decorator
721 grammar and test if it would be valid under the old decorator grammar.
723 The old grammar was: decorator: @ dotted_name [arguments] NEWLINE
724 The new grammar is : decorator: @ namedexpr_test NEWLINE
725 """
726 if node.type == token.NAME:
727 return True
728 if node.type == syms.power:
729 if node.children:
730 return (
731 node.children[0].type == token.NAME
732 and all(map(is_simple_decorator_trailer, node.children[1:-1]))
733 and (
734 len(node.children) < 2
735 or is_simple_decorator_trailer(node.children[-1], last=True)
736 )
737 )
738 return False
741def is_yield(node: LN) -> bool:
742 """Return True if `node` holds a `yield` or `yield from` expression."""
743 if node.type == syms.yield_expr:
744 return True
746 if is_name_token(node) and node.value == "yield":
747 return True
749 if node.type != syms.atom:
750 return False
752 if len(node.children) != 3:
753 return False
755 lpar, expr, rpar = node.children
756 if lpar.type == token.LPAR and rpar.type == token.RPAR:
757 return is_yield(expr)
759 return False
762def is_vararg(leaf: Leaf, within: set[NodeType]) -> bool:
763 """Return True if `leaf` is a star or double star in a vararg or kwarg.
765 If `within` includes VARARGS_PARENTS, this applies to function signatures.
766 If `within` includes UNPACKING_PARENTS, it applies to right hand-side
767 extended iterable unpacking (PEP 3132) and additional unpacking
768 generalizations (PEP 448).
769 """
770 if leaf.type not in VARARGS_SPECIALS or not leaf.parent:
771 return False
773 p = leaf.parent
774 if p.type == syms.star_expr:
775 # Star expressions are also used as assignment targets in extended
776 # iterable unpacking (PEP 3132). See what its parent is instead.
777 if not p.parent:
778 return False
780 p = p.parent
782 return p.type in within
785def is_fstring(node: Node) -> bool:
786 """Return True if the node is an f-string"""
787 return node.type == syms.fstring
790def fstring_tstring_to_string(node: Node) -> Leaf:
791 """Converts an fstring or tstring node back to a string node."""
792 string_without_prefix = str(node)[len(node.prefix) :]
793 string_leaf = Leaf(token.STRING, string_without_prefix, prefix=node.prefix)
794 string_leaf.lineno = node.get_lineno() or 0
795 return string_leaf
798def is_multiline_string(node: LN) -> bool:
799 """Return True if `leaf` is a multiline string that actually spans many lines."""
800 if isinstance(node, Node) and is_fstring(node):
801 leaf = fstring_tstring_to_string(node)
802 elif isinstance(node, Leaf):
803 leaf = node
804 else:
805 return False
807 return has_triple_quotes(leaf.value) and "\n" in leaf.value
810def is_parent_function_or_class(node: Node) -> bool:
811 assert node.type in {syms.suite, syms.simple_stmt}
812 assert node.parent is not None
813 # Note this works for suites / simple_stmts in async def as well
814 return node.parent.type in {syms.funcdef, syms.classdef}
817def is_function_or_class(node: Node) -> bool:
818 return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef}
821def is_stub_suite(node: Node) -> bool:
822 """Return True if `node` is a suite with a stub body."""
823 if node.parent is not None and not is_parent_function_or_class(node):
824 return False
826 # If there is a comment, we want to keep it.
827 if node.prefix.strip():
828 return False
830 if (
831 len(node.children) != 4
832 or node.children[0].type != token.NEWLINE
833 or node.children[1].type != token.INDENT
834 or node.children[3].type != token.DEDENT
835 ):
836 return False
838 if node.children[3].prefix.strip():
839 return False
841 return is_stub_body(node.children[2])
844def is_stub_body(node: LN) -> bool:
845 """Return True if `node` is a simple statement containing an ellipsis."""
846 if not isinstance(node, Node) or node.type != syms.simple_stmt:
847 return False
849 if len(node.children) != 2:
850 return False
852 child = node.children[0]
853 return (
854 not child.prefix.strip()
855 and child.type == syms.atom
856 and len(child.children) == 3
857 and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
858 )
861def is_atom_with_invisible_parens(node: LN) -> bool:
862 """Given a `LN`, determines whether it's an atom `node` with invisible
863 parens. Useful in dedupe-ing and normalizing parens.
864 """
865 if isinstance(node, Leaf) or node.type != syms.atom:
866 return False
868 first, last = node.children[0], node.children[-1]
869 return (
870 isinstance(first, Leaf)
871 and first.type == token.LPAR
872 and first.value == ""
873 and isinstance(last, Leaf)
874 and last.type == token.RPAR
875 and last.value == ""
876 )
879def is_empty_par(leaf: Leaf) -> bool:
880 return is_empty_lpar(leaf) or is_empty_rpar(leaf)
883def is_empty_lpar(leaf: Leaf) -> bool:
884 return leaf.type == token.LPAR and leaf.value == ""
887def is_empty_rpar(leaf: Leaf) -> bool:
888 return leaf.type == token.RPAR and leaf.value == ""
891def is_import(leaf: Leaf) -> bool:
892 """Return True if the given leaf starts an import statement."""
893 p = leaf.parent
894 t = leaf.type
895 v = leaf.value
896 return bool(
897 t == token.NAME
898 and (
899 (v == "import" and p and p.type == syms.import_name)
900 or (v == "from" and p and p.type == syms.import_from)
901 )
902 )
905def is_with_or_async_with_stmt(leaf: Leaf) -> bool:
906 """Return True if the given leaf starts a with or async with statement."""
907 return bool(
908 leaf.type == token.NAME
909 and leaf.value == "with"
910 and leaf.parent
911 and leaf.parent.type == syms.with_stmt
912 ) or bool(
913 leaf.type == token.ASYNC
914 and leaf.next_sibling
915 and leaf.next_sibling.type == syms.with_stmt
916 )
919def is_async_stmt_or_funcdef(leaf: Leaf) -> bool:
920 """Return True if the given leaf starts an async def/for/with statement.
922 Note that `async def` can be either an `async_stmt` or `async_funcdef`,
923 the latter is used when it has decorators.
924 """
925 return bool(
926 leaf.type == token.ASYNC
927 and leaf.parent
928 and leaf.parent.type in {syms.async_stmt, syms.async_funcdef}
929 )
932def is_type_comment(leaf: Leaf, mode: Mode) -> bool:
933 """Return True if the given leaf is a type comment. This function should only
934 be used for general type comments (excluding ignore annotations, which should
935 use `is_type_ignore_comment`). Note that general type comments are no longer
936 used in modern version of Python, this function may be deprecated in the future."""
937 t = leaf.type
938 v = leaf.value
939 return t in {token.COMMENT, STANDALONE_COMMENT} and is_type_comment_string(v, mode)
942def is_type_comment_string(value: str, mode: Mode) -> bool:
943 if Preview.standardize_type_comments in mode:
944 is_valid = value.startswith("#") and value[1:].lstrip().startswith("type:")
945 else:
946 is_valid = value.startswith("# type:")
947 return is_valid
950def is_type_ignore_comment(leaf: Leaf, mode: Mode) -> bool:
951 """Return True if the given leaf is a type comment with ignore annotation."""
952 t = leaf.type
953 v = leaf.value
954 return t in {token.COMMENT, STANDALONE_COMMENT} and is_type_ignore_comment_string(
955 v, mode
956 )
959def is_type_ignore_comment_string(value: str, mode: Mode) -> bool:
960 """Return True if the given string match with type comment with
961 ignore annotation."""
962 if Preview.standardize_type_comments in mode:
963 is_valid = is_type_comment_string(value, mode) and value.split(":", 1)[
964 1
965 ].lstrip().startswith("ignore")
966 else:
967 is_valid = value.startswith("# type: ignore")
969 return is_valid
972def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:
973 """Wrap `child` in parentheses.
975 This replaces `child` with an atom holding the parentheses and the old
976 child. That requires moving the prefix.
978 If `visible` is False, the leaves will be valueless (and thus invisible).
979 """
980 lpar = Leaf(token.LPAR, "(" if visible else "")
981 rpar = Leaf(token.RPAR, ")" if visible else "")
982 prefix = child.prefix
983 child.prefix = ""
984 index = child.remove() or 0
985 new_child = Node(syms.atom, [lpar, child, rpar])
986 new_child.prefix = prefix
987 parent.insert_child(index, new_child)
990def unwrap_singleton_parenthesis(node: LN) -> LN | None:
991 """Returns `wrapped` if `node` is of the shape ( wrapped ).
993 Parenthesis can be optional. Returns None otherwise"""
994 if len(node.children) != 3:
995 return None
997 lpar, wrapped, rpar = node.children
998 if not (lpar.type == token.LPAR and rpar.type == token.RPAR):
999 return None
1001 return wrapped
1004def ensure_visible(leaf: Leaf) -> None:
1005 """Make sure parentheses are visible.
1007 They could be invisible as part of some statements (see
1008 :func:`normalize_invisible_parens` and :func:`visit_import_from`).
1009 """
1010 if leaf.type == token.LPAR:
1011 leaf.value = "("
1012 elif leaf.type == token.RPAR:
1013 leaf.value = ")"
1016def is_name_token(nl: NL) -> TypeGuard[Leaf]:
1017 return nl.type == token.NAME
1020def is_lpar_token(nl: NL) -> TypeGuard[Leaf]:
1021 return nl.type == token.LPAR
1024def is_rpar_token(nl: NL) -> TypeGuard[Leaf]:
1025 return nl.type == token.RPAR
1028def is_number_token(nl: NL) -> TypeGuard[Leaf]:
1029 return nl.type == token.NUMBER
1032def get_annotation_type(leaf: Leaf) -> Literal["return", "param", None]:
1033 """Returns the type of annotation this leaf is part of, if any."""
1034 ancestor = leaf.parent
1035 while ancestor is not None:
1036 if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW:
1037 return "return"
1038 if ancestor.parent and ancestor.parent.type == syms.tname:
1039 return "param"
1040 ancestor = ancestor.parent
1041 return None
1044def is_part_of_annotation(leaf: Leaf) -> bool:
1045 """Returns whether this leaf is part of a type annotation."""
1046 assert leaf.parent is not None
1047 return get_annotation_type(leaf) is not None
1050def first_leaf(node: LN) -> Leaf | None:
1051 """Returns the first leaf of the ancestor node."""
1052 if isinstance(node, Leaf):
1053 return node
1054 elif not node.children:
1055 return None
1056 else:
1057 return first_leaf(node.children[0])
1060def last_leaf(node: LN) -> Leaf | None:
1061 """Returns the last leaf of the ancestor node."""
1062 if isinstance(node, Leaf):
1063 return node
1064 elif not node.children:
1065 return None
1066 else:
1067 return last_leaf(node.children[-1])
1070def furthest_ancestor_with_last_leaf(leaf: Leaf) -> LN:
1071 """Returns the furthest ancestor that has this leaf node as the last leaf."""
1072 node: LN = leaf
1073 while node.parent and node.parent.children and node is node.parent.children[-1]:
1074 node = node.parent
1075 return node
1078def has_sibling_with_type(node: LN, type: int) -> bool:
1079 # Check previous siblings
1080 sibling = node.prev_sibling
1081 while sibling is not None:
1082 if sibling.type == type:
1083 return True
1084 sibling = sibling.prev_sibling
1086 # Check next siblings
1087 sibling = node.next_sibling
1088 while sibling is not None:
1089 if sibling.type == type:
1090 return True
1091 sibling = sibling.next_sibling
1093 return False