Skip to content

Commit

Permalink
Respect py-version for inconsistent-quotes inside f-strings (#9152)…
Browse files Browse the repository at this point in the history
… (#9155)

* [inconsistent-quotes] Emit for f-strings for 3.12 only

* Add docs for inconsistent-quotes with f-strings

Despite ``sys.version`` check, pylint raises the warning
with Python 3.11

(cherry picked from commit 1c0bc70)

Co-authored-by: theirix <[email protected]>
  • Loading branch information
github-actions[bot] and theirix authored Oct 16, 2023
1 parent a9d9dc3 commit a77f0c1
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9113.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Emit ``inconsistent-quotes`` for f-strings with 3.12 interpreter only if targeting pre-3.12 versions.

Closes #9113
15 changes: 15 additions & 0 deletions pylint/checkers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import collections
import re
import sys
import tokenize
from collections import Counter
from collections.abc import Iterable, Sequence
Expand Down Expand Up @@ -834,8 +835,22 @@ def check_for_consistent_string_delimiters(
"""
string_delimiters: Counter[str] = collections.Counter()

inside_fstring = False # whether token is inside f-string (since 3.12)
target_py312 = self.linter.config.py_version >= (3, 12)

# First, figure out which quote character predominates in the module
for tok_type, token, _, _, _ in tokens:
if sys.version_info[:2] >= (3, 12):
# pylint: disable=no-member,useless-suppression
if tok_type == tokenize.FSTRING_START:
inside_fstring = True
elif tok_type == tokenize.FSTRING_END:
inside_fstring = False

if inside_fstring and not target_py312:
# skip analysis of f-string contents
continue

if tok_type == tokenize.STRING and _is_quote_delimiter_chosen_freely(token):
string_delimiters[_get_quote_delimiter(token)] += 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pylint: disable=missing-module-docstring

dictionary = {'0': 0}
f_string = f'{dictionary["0"]}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[STRING]
check-quote-consistency=yes

[testoptions]
max_pyver=3.12
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pylint: disable=missing-module-docstring

dictionary = {'0': 0}
# quotes are inconsistent when targetting Python 3.12 (use single quotes)
f_string = f'{dictionary["0"]}' # [inconsistent-quotes]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[STRING]
check-quote-consistency=yes

[main]
py-version=3.12

[testoptions]
min_pyver=3.12
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inconsistent-quotes:5:0:None:None::"Quote delimiter "" is inconsistent with the rest of the file":UNDEFINED
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pylint: disable=missing-module-docstring

dictionary = {'0': 0}
# quotes are consistent when targetting 3.11 and earlier (cannot use single quotes here)
f_string = f'{dictionary["0"]}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[STRING]
check-quote-consistency=yes

[main]
py-version=3.11

[testoptions]
min_pyver=3.12

0 comments on commit a77f0c1

Please sign in to comment.