diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f369fcabe3f01..33e475d4a9e20 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -342,11 +342,6 @@ repos: (?x) ^(asv_bench|pandas/tests|doc)/ |scripts/validate_min_versions_in_sync\.py$ - - id: unwanted-patterns-strings-to-concatenate - name: Check for use of not concatenated strings - language: python - entry: python scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate" - types_or: [python, cython] - id: unwanted-patterns-strings-with-misplaced-whitespace name: Check for strings with misplaced spaces language: python diff --git a/asv_bench/benchmarks/io/csv.py b/asv_bench/benchmarks/io/csv.py index f5aa421951a1d..ef25bf45b0f8f 100644 --- a/asv_bench/benchmarks/io/csv.py +++ b/asv_bench/benchmarks/io/csv.py @@ -318,7 +318,7 @@ def setup(self, sep, decimal, float_precision): "".join([random.choice(string.digits) for _ in range(28)]) for _ in range(15) ] - rows = sep.join([f"0{decimal}" + "{}"] * 3) + "\n" + rows = sep.join([f"0{decimal}*3)\n"]) data = rows * 5 data = data.format(*floats) * 200 # 1000 x 3 strings csv self.StringIO_input = StringIO(data) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 25ba4b697d97b..8eadfd6827a83 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3663,8 +3663,7 @@ def css(rgba, text_only) -> str: dark = relative_luminance(rgba) < text_color_threshold text_color = "#f1f1f1" if dark else "#000000" return ( - f"background-color: {_matplotlib.colors.rgb2hex(rgba)};" - + f"color: {text_color};" + f"background-color: {_matplotlib.colors.rgb2hex(rgba)}; color: {text_color};" ) else: return f"color: {_matplotlib.colors.rgb2hex(rgba)};" diff --git a/pandas/io/sas/sas_constants.py b/pandas/io/sas/sas_constants.py index a090b8a1acb3c..62c17bd03927e 100644 --- a/pandas/io/sas/sas_constants.py +++ b/pandas/io/sas/sas_constants.py @@ -4,9 +4,9 @@ magic: Final = ( b"\x00\x00\x00\x00\x00\x00\x00\x00" - + b"\x00\x00\x00\x00\xc2\xea\x81\x60" - + b"\xb3\x14\x11\xcf\xbd\x92\x08\x00" - + b"\x09\xc7\x31\x8c\x18\x1f\x10\x11" + b"\x00\x00\x00\x00\xc2\xea\x81\x60" + b"\xb3\x14\x11\xcf\xbd\x92\x08\x00" + b"\x09\xc7\x31\x8c\x18\x1f\x10\x11" ) align_1_checker_value: Final = b"3" diff --git a/pandas/tests/groupby/test_raises.py b/pandas/tests/groupby/test_raises.py index 76ba4c974b3fd..918b2260175e1 100644 --- a/pandas/tests/groupby/test_raises.py +++ b/pandas/tests/groupby/test_raises.py @@ -339,27 +339,19 @@ def test_groupby_raises_category(how, by, groupby_series, groupby_func): "cumcount": (None, ""), "cummax": ( (NotImplementedError, TypeError), - "(category type does not support cummax operations|" - + "category dtype not supported|" - + "cummax is not supported for category dtype)", + "(category type does not support cummax operations|category dtype not supported|cummax is not supported for category dtype)", ), "cummin": ( (NotImplementedError, TypeError), - "(category type does not support cummin operations|" - + "category dtype not supported|" - "cummin is not supported for category dtype)", + "(category type does not support cummin operations|category dtype not supported|cummin is not supported for category dtype)", ), "cumprod": ( (NotImplementedError, TypeError), - "(category type does not support cumprod operations|" - + "category dtype not supported|" - "cumprod is not supported for category dtype)", + "(category type does not support cumprod operations|category dtype not supported|cumprod is not supported for category dtype)", ), "cumsum": ( (NotImplementedError, TypeError), - "(category type does not support cumsum operations|" - + "category dtype not supported|" - "cumsum is not supported for category dtype)", + "(category type does not support cumsum operations|category dtype not supported|cumsum is not supported for category dtype)", ), "diff": ( TypeError, @@ -368,8 +360,7 @@ def test_groupby_raises_category(how, by, groupby_series, groupby_func): "ffill": (None, ""), "fillna": ( TypeError, - r"Cannot setitem on a Categorical with a new category \(0\), " - + "set the categories first", + r"Cannot setitem on a Categorical with a new category \(0\),set the categories first", ), "first": (None, ""), "idxmax": (None, ""), @@ -534,34 +525,25 @@ def test_groupby_raises_category_on_category( "cumcount": (None, ""), "cummax": ( (NotImplementedError, TypeError), - "(cummax is not supported for category dtype|" - + "category dtype not supported|" - + "category type does not support cummax operations)", + "(cummax is not supported for category dtype|category dtype not supported|category type does not support cummax operations)", ), "cummin": ( (NotImplementedError, TypeError), - "(cummin is not supported for category dtype|" - + "category dtype not supported|" - "category type does not support cummin operations)", + "(cummin is not supported for category dtype|category dtype not supported|category type does not support cummin operations)", ), "cumprod": ( (NotImplementedError, TypeError), - "(cumprod is not supported for category dtype|" - + "category dtype not supported|" - "category type does not support cumprod operations)", + "(cumprod is not supported for category dtype|category dtype not supported|category type does not support cumprod operations)", ), "cumsum": ( (NotImplementedError, TypeError), - "(cumsum is not supported for category dtype|" - + "category dtype not supported|" - + "category type does not support cumsum operations)", + "(cumsum is not supported for category dtype|category dtype not supported|category type does not support cumsum operations)", ), "diff": (TypeError, "unsupported operand type"), "ffill": (None, ""), "fillna": ( TypeError, - r"Cannot setitem on a Categorical with a new category \(0\), " - + "set the categories first", + r"Cannot setitem on a Categorical with a new category \(0\), set the categories first", ), "first": (None, ""), "idxmax": (ValueError, "attempt to get argmax of an empty sequence") diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 5fdd0e4311d88..bcaf629dd42db 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -340,8 +340,7 @@ def test_index_col_with_unnamed(self, read_ext, index_col): def test_usecols_pass_non_existent_column(self, read_ext): msg = ( - "Usecols do not match columns, " - "columns expected but not found: " + r"\['E'\]" + "Usecols do not match columns,columns expected but not found: r\['E'\]" ) with pytest.raises(ValueError, match=msg): @@ -349,8 +348,7 @@ def test_usecols_pass_non_existent_column(self, read_ext): def test_usecols_wrong_type(self, read_ext): msg = ( - "'usecols' must either be list-like of " - "all strings, all unicode, all integers or a callable." + "'usecols' must either be listlike of all strings, all unicode,all integers or a callable." ) with pytest.raises(ValueError, match=msg): diff --git a/pandas/tests/io/formats/test_css.py b/pandas/tests/io/formats/test_css.py index 70c91dd02751a..6c2212a7289d4 100644 --- a/pandas/tests/io/formats/test_css.py +++ b/pandas/tests/io/formats/test_css.py @@ -192,8 +192,7 @@ def test_css_border_shorthands(prop, expected): ( "margin: 1px; margin-top: 2px", "", - "margin-left: 1px; margin-right: 1px; " - + "margin-bottom: 1px; margin-top: 2px", + "margin-left: 1px; margin-right: 1px; margin-bottom: 1px; margin-top: 2px", ), ("margin-top: 2px", "margin: 1px", "margin: 1px; margin-top: 2px"), ("margin: 1px", "margin-top: 2px", "margin: 1px"), diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index af117af0c8d3d..fc8187b29becb 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2152,18 +2152,18 @@ def test_freq_name_separation(self): def test_to_string_mixed(self): s = Series(["foo", np.nan, -1.23, 4.56]) result = s.to_string() - expected = "0 foo\n" + "1 NaN\n" + "2 -1.23\n" + "3 4.56" + expected = "0 foo\n 1 NaN\n 2 -1.23\n 3 4.56" assert result == expected # but don't count NAs as floats s = Series(["foo", np.nan, "bar", "baz"]) result = s.to_string() - expected = "0 foo\n" + "1 NaN\n" + "2 bar\n" + "3 baz" + expected = "0 foo\n 1 NaN\n 2 bar\n 3 baz" assert result == expected s = Series(["foo", 5, "bar", "baz"]) result = s.to_string() - expected = "0 foo\n" + "1 5\n" + "2 bar\n" + "3 baz" + expected = "0 foo\n 1 5\n 2 bar\n 3 baz" assert result == expected def test_to_string_float_na_spacing(self): @@ -2172,11 +2172,7 @@ def test_to_string_float_na_spacing(self): result = s.to_string() expected = ( - "0 NaN\n" - + "1 1.5678\n" - + "2 NaN\n" - + "3 -3.0000\n" - + "4 NaN" + "0 NaN\n 1 1.5678\n 2 NaN\n 3 -3.0000\n 4 NaN" ) assert result == expected @@ -2184,7 +2180,7 @@ def test_to_string_without_index(self): # GH 11729 Test index=False option s = Series([1, 2, 3, 4]) result = s.to_string(index=False) - expected = "1\n" + "2\n" + "3\n" + "4" + expected = "1\n 2\n 3\n 4" assert result == expected def test_unicode_name_in_footer(self): @@ -2815,7 +2811,7 @@ def dtype(self): series = Series(ExtTypeStub()) res = repr(series) # This line crashed before #33770 was fixed. - expected = "0 [False True]\n" + "1 [ True False]\n" + "dtype: DtypeStub" + expected = "0 [False True]\n 1 [ True False]\n dtype: DtypeStub" assert res == expected diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index 4f025e84e2bd3..1941a0410b734 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -396,8 +396,7 @@ def test_record_prefix(self, state_data): def test_non_ascii_key(self): testjson = ( - b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},' - + b'{"\xc3\x9cnic\xc3\xb8de":1,"sub":{"A":3, "B":4}}]' + b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},{"\xc3\x9cnic\xc3\xb8de":1,"sub":{"A":3, "B":4}}]' ).decode("utf8") testdata = { diff --git a/pandas/tests/io/parser/conftest.py b/pandas/tests/io/parser/conftest.py index 0462d1fe6da0b..f1f86a19a1ad3 100644 --- a/pandas/tests/io/parser/conftest.py +++ b/pandas/tests/io/parser/conftest.py @@ -189,7 +189,7 @@ def all_parsers_all_precisions(request): _encoding_prefixes = ["utf", "UTF"] _encoding_fmts = [ - f"{prefix}{sep}" + "{0}" for sep in _encoding_seps for prefix in _encoding_prefixes + f"{prefix}{sep}{0}" for sep in _encoding_seps for prefix in _encoding_prefixes ] diff --git a/pandas/tests/io/parser/test_network.py b/pandas/tests/io/parser/test_network.py index 3768f37b65546..e7e9f8294ab28 100644 --- a/pandas/tests/io/parser/test_network.py +++ b/pandas/tests/io/parser/test_network.py @@ -65,8 +65,7 @@ def test_url_encoding_csv(): GH 10424 """ path = ( - "https://raw.githubusercontent.com/pandas-dev/pandas/main/" - + "pandas/tests/io/parser/data/unicode_series.csv" + "https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/parser/data/unicode_series.csv" ) df = read_csv(path, encoding="latin-1", header=None) assert df.loc[15, 1] == "Á köldum klaka (Cold Fever) (1994)" diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index d5c6eccad4783..c4cf1fee40822 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2916,8 +2916,7 @@ def test_datetime_time(self, tz_aware): def _get_index_columns(self, tbl_name): ixs = sql.read_sql_query( - "SELECT * FROM sqlite_master WHERE type = 'index' " - + f"AND tbl_name = '{tbl_name}'", + "SELECT * FROM sqlite_master WHERE type = 'index' AND tbl_name = '{tbl_name}'", self.conn, ) ix_cols = [] diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 04dcabd6b832c..d2cb01448532b 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -289,18 +289,14 @@ def __repr__(self) -> str: def test_categorical_repr(self): a = Series(Categorical([1, 2, 3, 4])) exp = ( - "0 1\n1 2\n2 3\n3 4\n" - + "dtype: category\nCategories (4, int64): [1, 2, 3, 4]" + "0 1\n1 2\n2 3\n3 4\n dtype: category\nCategories (4, int64): [1, 2, 3, 4]" ) assert exp == a.__str__() a = Series(Categorical(["a", "b"] * 25)) exp = ( - "0 a\n1 b\n" - + " ..\n" - + "48 a\n49 b\n" - + "Length: 50, dtype: category\nCategories (2, object): ['a', 'b']" + "0 a\n1 b\n ..\n 48 a\n49 b\n Length: 50, dtype: category\n Categories (2, object): ['a', 'b']" ) with option_context("display.max_rows", 5): assert exp == repr(a) @@ -308,7 +304,7 @@ def test_categorical_repr(self): levs = list("abcdefghijklmnopqrstuvwxyz") a = Series(Categorical(["a", "b"], categories=levs, ordered=True)) exp = ( - "0 a\n1 b\n" + "dtype: category\n" + "0 a\n1 b\n dtype: category\n" "Categories (26, object): ['a' < 'b' < 'c' < 'd' ... 'w' < 'x' < 'y' < 'z']" ) assert exp == a.__str__() diff --git a/pyproject.toml b/pyproject.toml index 511cc6a4d46eb..15c31d71b97f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -199,6 +199,8 @@ update-check = false target-version = "py38" select = [ + # implicit string concatenation + "ISC", # pyflakes "F", # pycodestyle diff --git a/scripts/tests/test_validate_unwanted_patterns.py b/scripts/tests/test_validate_unwanted_patterns.py index 81e06f758d700..7748d14bf32a5 100644 --- a/scripts/tests/test_validate_unwanted_patterns.py +++ b/scripts/tests/test_validate_unwanted_patterns.py @@ -152,49 +152,6 @@ def test_pytest_raises_raises(self, data, expected): result = list(validate_unwanted_patterns.bare_pytest_raises(fd)) assert result == expected - -@pytest.mark.parametrize( - "data, expected", - [ - ( - 'msg = ("bar " "baz")', - [ - ( - 1, - ( - "String unnecessarily split in two by black. " - "Please merge them manually." - ), - ) - ], - ), - ( - 'msg = ("foo " "bar " "baz")', - [ - ( - 1, - ( - "String unnecessarily split in two by black. " - "Please merge them manually." - ), - ), - ( - 1, - ( - "String unnecessarily split in two by black. " - "Please merge them manually." - ), - ), - ], - ), - ], -) -def test_strings_to_concatenate(data, expected): - fd = io.StringIO(data.strip()) - result = list(validate_unwanted_patterns.strings_to_concatenate(fd)) - assert result == expected - - class TestStringsWithWrongPlacedWhitespace: @pytest.mark.parametrize( "data", diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index 8d4aecd596328..082ec1556139d 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -232,54 +232,6 @@ def private_import_across_module(file_obj: IO[str]) -> Iterable[Tuple[int, str]] yield (node.lineno, f"Import of internal function {repr(module_name)}") -def strings_to_concatenate(file_obj: IO[str]) -> Iterable[Tuple[int, str]]: - """ - This test case is necessary after 'Black' (https://github.com/psf/black), - is formatting strings over multiple lines. - - For example, when this: - - >>> foo = ( - ... "bar " - ... "baz" - ... ) - - Is becoming this: - - >>> foo = ("bar " "baz") - - 'Black' is not considering this as an - issue (see https://github.com/psf/black/issues/1051), - so we are checking it here instead. - - Parameters - ---------- - file_obj : IO - File-like object containing the Python code to validate. - - Yields - ------ - line_number : int - Line number of unconcatenated string. - msg : str - Explanation of the error. - - Notes - ----- - GH #30454 - """ - tokens: List = list(tokenize.generate_tokens(file_obj.readline)) - - for current_token, next_token in zip(tokens, tokens[1:]): - if current_token.type == next_token.type == token.STRING: - yield ( - current_token.start[0], - ( - "String unnecessarily split in two by black. " - "Please merge them manually." - ), - ) - def strings_with_wrong_placed_whitespace( file_obj: IO[str],