Method: pylint.checkers.strings.StringConstantChecker.check_for_consistent_string_delimiters
Calls: 2, Exceptions: 0, Paths: 1Back
Path 1: 2 calls (1.0)
list (2)
1def check_for_consistent_string_delimiters(
2 self, tokens: Iterable[tokenize.TokenInfo]
3 ) -> None:
4 """Adds a message for each string using inconsistent quote delimiters.
5
6 Quote delimiters are used inconsistently if " and ' are mixed in a module's
7 shortstrings without having done so to avoid escaping an internal quote
8 character.
9
10 Args:
11 tokens: The tokens to be checked against for consistent usage.
12 """
13 string_delimiters: Counter[str] = collections.Counter()
14
15 # First, figure out which quote character predominates in the module
16 for tok_type, token, _, _, _ in tokens:
17 if tok_type == tokenize.STRING and _is_quote_delimiter_chosen_freely(token):
18 string_delimiters[_get_quote_delimiter(token)] += 1
19
20 if len(string_delimiters) > 1:
21 # Ties are broken arbitrarily
22 most_common_delimiter = string_delimiters.most_common(1)[0][0]
23 for tok_type, token, start, _, _ in tokens:
24 if tok_type != tokenize.STRING:
25 continue
26 quote_delimiter = _get_quote_delimiter(token)
27 if (
28 _is_quote_delimiter_chosen_freely(token)
29 and quote_delimiter != most_common_delimiter
30 ):
31 self.add_message(
32 "inconsistent-quotes", line=start[0], args=(quote_delimiter,)
33 )