Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect py-version for inconsistent-quotes inside f-strings #9152

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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