For non-multiline strings, return True if `line` is no longer than `line_length`. For multiline strings, looks at the context around `line` to determine if it should be inlined or split up. Uses the provided `line_str` rendering, if any, otherwise computes a new one.
(line: Line, *, mode: Mode, line_str: str = "")
| 1240 | |
| 1241 | |
| 1242 | def is_line_short_enough(line: Line, *, mode: Mode, line_str: str = "") -> bool: |
| 1243 | """For non-multiline strings, return True if `line` is no longer than `line_length`. |
| 1244 | For multiline strings, looks at the context around `line` to determine |
| 1245 | if it should be inlined or split up. |
| 1246 | Uses the provided `line_str` rendering, if any, otherwise computes a new one. |
| 1247 | """ |
| 1248 | if not line_str: |
| 1249 | line_str = line_to_string(line) |
| 1250 | |
| 1251 | if line.contains_standalone_comments(): |
| 1252 | return False |
| 1253 | if "\n" not in line_str: |
| 1254 | # No multiline strings (MLS) present |
| 1255 | return str_width(line_str) <= mode.line_length |
| 1256 | |
| 1257 | first, *_, last = line_str.split("\n") |
| 1258 | if str_width(first) > mode.line_length or str_width(last) > mode.line_length: |
| 1259 | return False |
| 1260 | |
| 1261 | # Traverse the AST to examine the context of the multiline string (MLS), |
| 1262 | # tracking aspects such as depth and comma existence, |
| 1263 | # to determine whether to split the MLS or keep it together. |
| 1264 | # Depth (which is based on the existing bracket_depth concept) |
| 1265 | # is needed to determine nesting level of the MLS. |
| 1266 | # Includes special case for trailing commas. |
| 1267 | commas: list[int] = [] # tracks number of commas per depth level |
| 1268 | multiline_string: Leaf | None = None |
| 1269 | # store the leaves that contain parts of the MLS |
| 1270 | multiline_string_contexts: list[LN] = [] |
| 1271 | |
| 1272 | max_level_to_update: int | float = math.inf # track the depth of the MLS |
| 1273 | for i, leaf in enumerate(line.leaves): |
| 1274 | if max_level_to_update == math.inf: |
| 1275 | had_comma: int | None = None |
| 1276 | if leaf.bracket_depth + 1 > len(commas): |
| 1277 | commas.append(0) |
| 1278 | elif leaf.bracket_depth + 1 < len(commas): |
| 1279 | had_comma = commas.pop() |
| 1280 | if ( |
| 1281 | had_comma is not None |
| 1282 | and multiline_string is not None |
| 1283 | and multiline_string.bracket_depth == leaf.bracket_depth + 1 |
| 1284 | ): |
| 1285 | # Have left the level with the MLS, stop tracking commas |
| 1286 | max_level_to_update = leaf.bracket_depth |
| 1287 | if had_comma > 0: |
| 1288 | # MLS was in parens with at least one comma - force split |
| 1289 | return False |
| 1290 | |
| 1291 | if leaf.bracket_depth <= max_level_to_update and leaf.type == token.COMMA: |
| 1292 | # Inside brackets, ignore trailing comma |
| 1293 | # directly after MLS/MLS-containing expression |
| 1294 | ignore_ctxs: list[LN | None] = [None] |
| 1295 | ignore_ctxs += multiline_string_contexts |
| 1296 | if (line.inside_brackets or leaf.bracket_depth > 0) and ( |
| 1297 | i != len(line.leaves) - 1 or leaf.prev_sibling not in ignore_ctxs |
| 1298 | ): |
| 1299 | commas[leaf.bracket_depth] += 1 |
no test coverage detected