"""
import sys
-from typing import Generic, Iterator, List, Optional, Set, Tuple, TypeVar, Union
+from typing import Final, Generic, Iterator, List, Optional, Set, Tuple, TypeVar, Union
-if sys.version_info >= (3, 8):
- from typing import Final
-else:
- from typing_extensions import Final
if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from mypy_extensions import mypyc_attr
from black.cache import CACHE_DIR
+from black.mode import Mode, Preview
from black.strings import has_triple_quotes
from blib2to3 import pygram
from blib2to3.pgen2 import token
yield from self.visit(child)
-def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa: C901
+def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str: # noqa: C901
"""Return whitespace prefix if needed for the given `leaf`.
`complex_subscript` signals whether the given leaf is part of a subscription
return NO
+ elif Preview.walrus_subscript in mode and (
+ t == token.COLONEQUAL or prev.type == token.COLONEQUAL
+ ):
+ return SPACE
+
elif not complex_subscript:
return NO
def is_stub_suite(node: Node) -> bool:
"""Return True if `node` is a suite with a stub body."""
+
+ # If there is a comment, we want to keep it.
+ if node.prefix.strip():
+ return False
+
if (
len(node.children) != 4
or node.children[0].type != token.NEWLINE
):
return False
+ if node.children[3].prefix.strip():
+ return False
+
return is_stub_body(node.children[2])
child = node.children[0]
return (
- child.type == syms.atom
+ not child.prefix.strip()
+ and child.type == syms.atom
and len(child.children) == 3
and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
)