Skip to content

Commit 29140d4

Browse files
authored
STYLE: replace unwanted-patterns-strings-to-concatenate with ruff (#51613)
* STYLE: replace unwanted-patterns-strings-to-concatenate with ruff * STYLE: replace unwanted-patterns-strings-to-concatenate with ruff II
1 parent 5dd6efc commit 29140d4

16 files changed

+44
-135
lines changed

.pre-commit-config.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,6 @@ repos:
342342
(?x)
343343
^(asv_bench|pandas/tests|doc)/
344344
|scripts/validate_min_versions_in_sync\.py$
345-
- id: unwanted-patterns-strings-to-concatenate
346-
name: Check for use of not concatenated strings
347-
language: python
348-
entry: python scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate"
349-
types_or: [python, cython]
350345
- id: unwanted-patterns-strings-with-misplaced-whitespace
351346
name: Check for strings with misplaced spaces
352347
language: python

asv_bench/benchmarks/io/csv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def setup(self, sep, decimal, float_precision):
318318
"".join([random.choice(string.digits) for _ in range(28)])
319319
for _ in range(15)
320320
]
321-
rows = sep.join([f"0{decimal}" + "{}"] * 3) + "\n"
321+
rows = sep.join([f"0{decimal}{{}}"] * 3) + "\n"
322322
data = rows * 5
323323
data = data.format(*floats) * 200 # 1000 x 3 strings csv
324324
self.StringIO_input = StringIO(data)

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3658,7 +3658,7 @@ def css(rgba, text_only) -> str:
36583658
text_color = "#f1f1f1" if dark else "#000000"
36593659
return (
36603660
f"background-color: {_matplotlib.colors.rgb2hex(rgba)};"
3661-
+ f"color: {text_color};"
3661+
f"color: {text_color};"
36623662
)
36633663
else:
36643664
return f"color: {_matplotlib.colors.rgb2hex(rgba)};"

pandas/io/sas/sas_constants.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
magic: Final = (
66
b"\x00\x00\x00\x00\x00\x00\x00\x00"
7-
+ b"\x00\x00\x00\x00\xc2\xea\x81\x60"
8-
+ b"\xb3\x14\x11\xcf\xbd\x92\x08\x00"
9-
+ b"\x09\xc7\x31\x8c\x18\x1f\x10\x11"
7+
b"\x00\x00\x00\x00\xc2\xea\x81\x60"
8+
b"\xb3\x14\x11\xcf\xbd\x92\x08\x00"
9+
b"\x09\xc7\x31\x8c\x18\x1f\x10\x11"
1010
)
1111

1212
align_1_checker_value: Final = b"3"

pandas/tests/groupby/test_raises.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -342,25 +342,25 @@ def test_groupby_raises_category(
342342
"cummax": (
343343
(NotImplementedError, TypeError),
344344
"(category type does not support cummax operations|"
345-
+ "category dtype not supported|"
346-
+ "cummax is not supported for category dtype)",
345+
"category dtype not supported|"
346+
"cummax is not supported for category dtype)",
347347
),
348348
"cummin": (
349349
(NotImplementedError, TypeError),
350350
"(category type does not support cummin operations|"
351-
+ "category dtype not supported|"
351+
"category dtype not supported|"
352352
"cummin is not supported for category dtype)",
353353
),
354354
"cumprod": (
355355
(NotImplementedError, TypeError),
356356
"(category type does not support cumprod operations|"
357-
+ "category dtype not supported|"
357+
"category dtype not supported|"
358358
"cumprod is not supported for category dtype)",
359359
),
360360
"cumsum": (
361361
(NotImplementedError, TypeError),
362362
"(category type does not support cumsum operations|"
363-
+ "category dtype not supported|"
363+
"category dtype not supported|"
364364
"cumsum is not supported for category dtype)",
365365
),
366366
"diff": (
@@ -371,7 +371,7 @@ def test_groupby_raises_category(
371371
"fillna": (
372372
TypeError,
373373
r"Cannot setitem on a Categorical with a new category \(0\), "
374-
+ "set the categories first",
374+
"set the categories first",
375375
)
376376
if not using_copy_on_write
377377
else (None, ""), # no-op with CoW
@@ -539,33 +539,33 @@ def test_groupby_raises_category_on_category(
539539
"cummax": (
540540
(NotImplementedError, TypeError),
541541
"(cummax is not supported for category dtype|"
542-
+ "category dtype not supported|"
543-
+ "category type does not support cummax operations)",
542+
"category dtype not supported|"
543+
"category type does not support cummax operations)",
544544
),
545545
"cummin": (
546546
(NotImplementedError, TypeError),
547547
"(cummin is not supported for category dtype|"
548-
+ "category dtype not supported|"
548+
"category dtype not supported|"
549549
"category type does not support cummin operations)",
550550
),
551551
"cumprod": (
552552
(NotImplementedError, TypeError),
553553
"(cumprod is not supported for category dtype|"
554-
+ "category dtype not supported|"
554+
"category dtype not supported|"
555555
"category type does not support cumprod operations)",
556556
),
557557
"cumsum": (
558558
(NotImplementedError, TypeError),
559559
"(cumsum is not supported for category dtype|"
560-
+ "category dtype not supported|"
561-
+ "category type does not support cumsum operations)",
560+
"category dtype not supported|"
561+
"category type does not support cumsum operations)",
562562
),
563563
"diff": (TypeError, "unsupported operand type"),
564564
"ffill": (None, ""),
565565
"fillna": (
566566
TypeError,
567567
r"Cannot setitem on a Categorical with a new category \(0\), "
568-
+ "set the categories first",
568+
"set the categories first",
569569
)
570570
if not using_copy_on_write
571571
else (None, ""), # no-op with CoW

pandas/tests/io/excel/test_readers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ def test_index_col_with_unnamed(self, read_ext, index_col):
341341
def test_usecols_pass_non_existent_column(self, read_ext):
342342
msg = (
343343
"Usecols do not match columns, "
344-
"columns expected but not found: " + r"\['E'\]"
344+
"columns expected but not found: "
345+
r"\['E'\]"
345346
)
346347

347348
with pytest.raises(ValueError, match=msg):

pandas/tests/io/formats/test_css.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_css_border_shorthands(prop, expected):
193193
"margin: 1px; margin-top: 2px",
194194
"",
195195
"margin-left: 1px; margin-right: 1px; "
196-
+ "margin-bottom: 1px; margin-top: 2px",
196+
"margin-bottom: 1px; margin-top: 2px",
197197
),
198198
("margin-top: 2px", "margin: 1px", "margin: 1px; margin-top: 2px"),
199199
("margin: 1px", "margin-top: 2px", "margin: 1px"),

pandas/tests/io/formats/test_format.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2160,18 +2160,18 @@ def test_freq_name_separation(self):
21602160
def test_to_string_mixed(self):
21612161
s = Series(["foo", np.nan, -1.23, 4.56])
21622162
result = s.to_string()
2163-
expected = "0 foo\n" + "1 NaN\n" + "2 -1.23\n" + "3 4.56"
2163+
expected = "".join(["0 foo\n", "1 NaN\n", "2 -1.23\n", "3 4.56"])
21642164
assert result == expected
21652165

21662166
# but don't count NAs as floats
21672167
s = Series(["foo", np.nan, "bar", "baz"])
21682168
result = s.to_string()
2169-
expected = "0 foo\n" + "1 NaN\n" + "2 bar\n" + "3 baz"
2169+
expected = "".join(["0 foo\n", "1 NaN\n", "2 bar\n", "3 baz"])
21702170
assert result == expected
21712171

21722172
s = Series(["foo", 5, "bar", "baz"])
21732173
result = s.to_string()
2174-
expected = "0 foo\n" + "1 5\n" + "2 bar\n" + "3 baz"
2174+
expected = "".join(["0 foo\n", "1 5\n", "2 bar\n", "3 baz"])
21752175
assert result == expected
21762176

21772177
def test_to_string_float_na_spacing(self):
@@ -2181,18 +2181,18 @@ def test_to_string_float_na_spacing(self):
21812181
result = s.to_string()
21822182
expected = (
21832183
"0 NaN\n"
2184-
+ "1 1.5678\n"
2185-
+ "2 NaN\n"
2186-
+ "3 -3.0000\n"
2187-
+ "4 NaN"
2184+
"1 1.5678\n"
2185+
"2 NaN\n"
2186+
"3 -3.0000\n"
2187+
"4 NaN"
21882188
)
21892189
assert result == expected
21902190

21912191
def test_to_string_without_index(self):
21922192
# GH 11729 Test index=False option
21932193
s = Series([1, 2, 3, 4])
21942194
result = s.to_string(index=False)
2195-
expected = "1\n" + "2\n" + "3\n" + "4"
2195+
expected = "\n".join(["1", "2", "3", "4"])
21962196
assert result == expected
21972197

21982198
def test_unicode_name_in_footer(self):
@@ -2823,7 +2823,9 @@ def dtype(self):
28232823

28242824
series = Series(ExtTypeStub())
28252825
res = repr(series) # This line crashed before #33770 was fixed.
2826-
expected = "0 [False True]\n" + "1 [ True False]\n" + "dtype: DtypeStub"
2826+
expected = "\n".join(
2827+
["0 [False True]", "1 [ True False]", "dtype: DtypeStub"]
2828+
)
28272829
assert res == expected
28282830

28292831

pandas/tests/io/json/test_normalize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def test_record_prefix(self, state_data):
397397
def test_non_ascii_key(self):
398398
testjson = (
399399
b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},'
400-
+ b'{"\xc3\x9cnic\xc3\xb8de":1,"sub":{"A":3, "B":4}}]'
400+
b'{"\xc3\x9cnic\xc3\xb8de":1,"sub":{"A":3, "B":4}}]'
401401
).decode("utf8")
402402

403403
testdata = {

pandas/tests/io/parser/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def all_parsers_all_precisions(request):
189189
_encoding_prefixes = ["utf", "UTF"]
190190

191191
_encoding_fmts = [
192-
f"{prefix}{sep}" + "{0}" for sep in _encoding_seps for prefix in _encoding_prefixes
192+
f"{prefix}{sep}{{0}}" for sep in _encoding_seps for prefix in _encoding_prefixes
193193
]
194194

195195

pandas/tests/io/parser/test_network.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_url_encoding_csv():
6666
"""
6767
path = (
6868
"https://raw.githubusercontent.com/pandas-dev/pandas/main/"
69-
+ "pandas/tests/io/parser/data/unicode_series.csv"
69+
"pandas/tests/io/parser/data/unicode_series.csv"
7070
)
7171
df = read_csv(path, encoding="latin-1", header=None)
7272
assert df.loc[15, 1] == "Á köldum klaka (Cold Fever) (1994)"

pandas/tests/io/test_sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,7 @@ def test_datetime_time(self, tz_aware):
29212921
def _get_index_columns(self, tbl_name):
29222922
ixs = sql.read_sql_query(
29232923
"SELECT * FROM sqlite_master WHERE type = 'index' "
2924-
+ f"AND tbl_name = '{tbl_name}'",
2924+
f"AND tbl_name = '{tbl_name}'",
29252925
self.conn,
29262926
)
29272927
ix_cols = []

pandas/tests/series/test_repr.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -290,25 +290,26 @@ def test_categorical_repr(self):
290290
a = Series(Categorical([1, 2, 3, 4]))
291291
exp = (
292292
"0 1\n1 2\n2 3\n3 4\n"
293-
+ "dtype: category\nCategories (4, int64): [1, 2, 3, 4]"
293+
"dtype: category\nCategories (4, int64): [1, 2, 3, 4]"
294294
)
295295

296296
assert exp == a.__str__()
297297

298298
a = Series(Categorical(["a", "b"] * 25))
299299
exp = (
300300
"0 a\n1 b\n"
301-
+ " ..\n"
302-
+ "48 a\n49 b\n"
303-
+ "Length: 50, dtype: category\nCategories (2, object): ['a', 'b']"
301+
" ..\n"
302+
"48 a\n49 b\n"
303+
"Length: 50, dtype: category\nCategories (2, object): ['a', 'b']"
304304
)
305305
with option_context("display.max_rows", 5):
306306
assert exp == repr(a)
307307

308308
levs = list("abcdefghijklmnopqrstuvwxyz")
309309
a = Series(Categorical(["a", "b"], categories=levs, ordered=True))
310310
exp = (
311-
"0 a\n1 b\n" + "dtype: category\n"
311+
"0 a\n1 b\n"
312+
"dtype: category\n"
312313
"Categories (26, object): ['a' < 'b' < 'c' < 'd' ... 'w' < 'x' < 'y' < 'z']"
313314
)
314315
assert exp == a.__str__()

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ select = [
216216
"PIE",
217217
# tidy imports
218218
"TID",
219+
# implicit string concatenation
220+
"ISC",
219221
]
220222

221223
ignore = [

scripts/tests/test_validate_unwanted_patterns.py

-42
Original file line numberDiff line numberDiff line change
@@ -153,48 +153,6 @@ def test_pytest_raises_raises(self, data, expected):
153153
assert result == expected
154154

155155

156-
@pytest.mark.parametrize(
157-
"data, expected",
158-
[
159-
(
160-
'msg = ("bar " "baz")',
161-
[
162-
(
163-
1,
164-
(
165-
"String unnecessarily split in two by black. "
166-
"Please merge them manually."
167-
),
168-
)
169-
],
170-
),
171-
(
172-
'msg = ("foo " "bar " "baz")',
173-
[
174-
(
175-
1,
176-
(
177-
"String unnecessarily split in two by black. "
178-
"Please merge them manually."
179-
),
180-
),
181-
(
182-
1,
183-
(
184-
"String unnecessarily split in two by black. "
185-
"Please merge them manually."
186-
),
187-
),
188-
],
189-
),
190-
],
191-
)
192-
def test_strings_to_concatenate(data, expected):
193-
fd = io.StringIO(data.strip())
194-
result = list(validate_unwanted_patterns.strings_to_concatenate(fd))
195-
assert result == expected
196-
197-
198156
class TestStringsWithWrongPlacedWhitespace:
199157
@pytest.mark.parametrize(
200158
"data",

scripts/validate_unwanted_patterns.py

-50
Original file line numberDiff line numberDiff line change
@@ -232,55 +232,6 @@ def private_import_across_module(file_obj: IO[str]) -> Iterable[Tuple[int, str]]
232232
yield (node.lineno, f"Import of internal function {repr(module_name)}")
233233

234234

235-
def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]:
236-
"""
237-
This test case is necessary after 'Black' (https://github.com/psf/black),
238-
is formatting strings over multiple lines.
239-
240-
For example, when this:
241-
242-
>>> foo = (
243-
... "bar "
244-
... "baz"
245-
... )
246-
247-
Is becoming this:
248-
249-
>>> foo = ("bar " "baz")
250-
251-
'Black' is not considering this as an
252-
issue (see https://github.com/psf/black/issues/1051),
253-
so we are checking it here instead.
254-
255-
Parameters
256-
----------
257-
file_obj : IO
258-
File-like object containing the Python code to validate.
259-
260-
Yields
261-
------
262-
line_number : int
263-
Line number of unconcatenated string.
264-
msg : str
265-
Explanation of the error.
266-
267-
Notes
268-
-----
269-
GH #30454
270-
"""
271-
tokens: List = list(tokenize.generate_tokens(file_obj.readline))
272-
273-
for current_token, next_token in zip(tokens, tokens[1:]):
274-
if current_token.type == next_token.type == token.STRING:
275-
yield (
276-
current_token.start[0],
277-
(
278-
"String unnecessarily split in two by black. "
279-
"Please merge them manually."
280-
),
281-
)
282-
283-
284235
def strings_with_wrong_placed_whitespace(
285236
file_obj: IO[str],
286237
) -> Iterable[Tuple[int, str]]:
@@ -457,7 +408,6 @@ def main(
457408
"bare_pytest_raises",
458409
"private_function_across_module",
459410
"private_import_across_module",
460-
"strings_to_concatenate",
461411
"strings_with_wrong_placed_whitespace",
462412
]
463413

0 commit comments

Comments
 (0)