MCPcopy
hub / github.com/psf/black / _dict_or_lambda_match

Method _dict_or_lambda_match

src/black/trans.py:2163–2199  ·  view source on GitHub ↗

Returns: string_idx such that @LL[string_idx] is equal to our target (i.e. matched) string, if this line matches the dictionary key assignment statement or lambda expression requirements listed in the 'Requirements' section of this classes' do

(LL: list[Leaf])

Source from the content-addressed store, hash-verified

2161
2162 @staticmethod
2163 def _dict_or_lambda_match(LL: list[Leaf]) -> int | None:
2164 """
2165 Returns:
2166 string_idx such that @LL[string_idx] is equal to our target (i.e.
2167 matched) string, if this line matches the dictionary key assignment
2168 statement or lambda expression requirements listed in the
2169 'Requirements' section of this classes' docstring.
2170 OR
2171 None, otherwise.
2172 """
2173 # If this line is a part of a dictionary key assignment or lambda expression...
2174 parent_types = [parent_type(LL[0]), parent_type(LL[0].parent)]
2175 if syms.dictsetmaker in parent_types or syms.lambdef in parent_types:
2176 is_valid_index = is_valid_index_factory(LL)
2177
2178 for i, leaf in enumerate(LL):
2179 # We MUST find a colon, it can either be dict's or lambda's colon...
2180 if leaf.type == token.COLON and i < len(LL) - 1:
2181 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
2182
2183 # That colon MUST be followed by a string...
2184 if is_valid_index(idx) and LL[idx].type == token.STRING:
2185 string_idx = idx
2186
2187 # Skip the string trailer, if one exists.
2188 string_parser = StringParser()
2189 idx = string_parser.parse(LL, string_idx)
2190
2191 # That string MAY be followed by a comma...
2192 if is_valid_index(idx) and LL[idx].type == token.COMMA:
2193 idx += 1
2194
2195 # But no more leaves are allowed...
2196 if not is_valid_index(idx):
2197 return string_idx
2198
2199 return None
2200
2201 @staticmethod
2202 def _trailing_comma_tuple_match(line: Line) -> int | None:

Callers 1

do_splitter_matchMethod · 0.95

Calls 6

parseMethod · 0.95
parent_typeFunction · 0.90
is_empty_parFunction · 0.90
is_valid_index_factoryFunction · 0.85
is_valid_indexFunction · 0.85
StringParserClass · 0.85

Tested by

no test coverage detected