From d240ca25ea73dad793750977fb7b7cdfbeadd2eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Langa?= Date: Sat, 9 Jun 2018 15:40:39 -0700 Subject: [PATCH] Make `is_complex_subscript()` ignore list literals This fixes catastrophically quadratic behavior on long lists. --- README.md | 3 +++ black.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 533a1f8..440f42b 100644 --- a/README.md +++ b/README.md @@ -812,6 +812,9 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). * fixed improper formatting of f-strings with quotes inside interpolated expressions (#322) +* fixed unnecessary slowdown when long list literals where found in a file + + ### 18.6b2 * added `--config` (#65) diff --git a/black.py b/black.py index 35daaa9..a405bad 100644 --- a/black.py +++ b/black.py @@ -1256,18 +1256,18 @@ class Line: def is_complex_subscript(self, leaf: Leaf) -> bool: """Return True iff `leaf` is part of a slice with non-trivial exprs.""" - open_lsqb = ( - leaf if leaf.type == token.LSQB else self.bracket_tracker.get_open_lsqb() - ) + open_lsqb = self.bracket_tracker.get_open_lsqb() if open_lsqb is None: return False subscript_start = open_lsqb.next_sibling - if ( - isinstance(subscript_start, Node) - and subscript_start.type == syms.subscriptlist - ): - subscript_start = child_towards(subscript_start, leaf) + + if isinstance(subscript_start, Node): + if subscript_start.type == syms.listmaker: + return False + + if subscript_start.type == syms.subscriptlist: + subscript_start = child_towards(subscript_start, leaf) return subscript_start is not None and any( n.type in TEST_DESCENDANTS for n in subscript_start.pre_order() ) -- 2.39.5