Skip to content

Commit 4a5d77f

Browse files
authored
STYLE enable B005 check in flake8-bugbear (#49707)
* fixup b005 errors * use sys.version_info check * replace redefined func * fixup * use version_info check in _str_methods too * move import * put unnecessary else statement Co-authored-by: MarcoGorelli <>
1 parent b1f1f37 commit 4a5d77f

File tree

7 files changed

+103
-14
lines changed

7 files changed

+103
-14
lines changed

doc/source/conf.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,14 @@ class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
583583
priority = 0.5
584584

585585
def format_name(self):
586-
return MethodDocumenter.format_name(self).rstrip(".__call__")
586+
if sys.version_info < (3, 9):
587+
# NOTE pyupgrade will remove this when we run it with --py39-plus
588+
# so don't remove the unnecessary `else` statement below
589+
from pandas.util._str_methods import removesuffix
590+
591+
return removesuffix(MethodDocumenter.format_name(self), ".__call__")
592+
else:
593+
return MethodDocumenter.format_name(self).removesuffix(".__call__")
587594

588595

589596
class PandasAutosummary(Autosummary):

pandas/core/ops/common.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import annotations
55

66
from functools import wraps
7+
import sys
78
from typing import Callable
89

910
from pandas._libs.lib import item_from_zerodim
@@ -52,7 +53,16 @@ def _unpack_zerodim_and_defer(method, name: str):
5253
-------
5354
method
5455
"""
55-
is_cmp = name.strip("__") in {"eq", "ne", "lt", "le", "gt", "ge"}
56+
if sys.version_info < (3, 9):
57+
from pandas.util._str_methods import (
58+
removeprefix,
59+
removesuffix,
60+
)
61+
62+
stripped_name = removesuffix(removeprefix(name, "__"), "__")
63+
else:
64+
stripped_name = name.removeprefix("__").removesuffix("__")
65+
is_cmp = stripped_name in {"eq", "ne", "lt", "le", "gt", "ge"}
5666

5767
@wraps(method)
5868
def new_method(self, other):

pandas/core/strings/object_array.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Callable # noqa: PDF001
44
import functools
55
import re
6+
import sys
67
import textwrap
78
from typing import (
89
TYPE_CHECKING,
@@ -462,16 +463,14 @@ def removeprefix(text: str) -> str:
462463
return self._str_map(removeprefix)
463464

464465
def _str_removesuffix(self, suffix: str) -> Series:
465-
# this could be used on Python 3.9+
466-
# f = lambda x: x.removesuffix(suffix)
467-
# return self._str_map(str.removesuffix)
466+
if sys.version_info < (3, 9):
467+
# NOTE pyupgrade will remove this when we run it with --py39-plus
468+
# so don't remove the unnecessary `else` statement below
469+
from pandas.util._str_methods import removesuffix
468470

469-
def removesuffix(text: str) -> str:
470-
if text.endswith(suffix):
471-
return text[: -len(suffix)]
472-
return text
473-
474-
return self._str_map(removesuffix)
471+
return self._str_map(functools.partial(removesuffix, suffix=suffix))
472+
else:
473+
return self._str_map(lambda x: x.removesuffix(suffix))
475474

476475
def _str_extract(self, pat: str, flags: int = 0, expand: bool = True):
477476
regex = re.compile(pat, flags=flags)

pandas/tests/io/test_sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ def test_get_schema2(self, test_frame1):
15871587
def _get_sqlite_column_type(self, schema, column):
15881588

15891589
for col in schema.split("\n"):
1590-
if col.split()[0].strip('""') == column:
1590+
if col.split()[0].strip('"') == column:
15911591
return col.split()[1]
15921592
raise ValueError(f"Column {column} not found")
15931593

pandas/tests/util/test_str_methods.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
3+
import pytest
4+
5+
if sys.version_info < (3, 9):
6+
from pandas.util._str_methods import (
7+
removeprefix,
8+
removesuffix,
9+
)
10+
11+
@pytest.mark.parametrize(
12+
"string, prefix, expected",
13+
(
14+
("wildcat", "wild", "cat"),
15+
("blackbird", "black", "bird"),
16+
("housefly", "house", "fly"),
17+
("ladybug", "lady", "bug"),
18+
("rattlesnake", "rattle", "snake"),
19+
("baboon", "badger", "baboon"),
20+
("quetzal", "elk", "quetzal"),
21+
),
22+
)
23+
def test_remove_prefix(string, prefix, expected):
24+
result = removeprefix(string, prefix)
25+
assert result == expected
26+
27+
@pytest.mark.parametrize(
28+
"string, suffix, expected",
29+
(
30+
("wildcat", "cat", "wild"),
31+
("blackbird", "bird", "black"),
32+
("housefly", "fly", "house"),
33+
("ladybug", "bug", "lady"),
34+
("rattlesnake", "snake", "rattle"),
35+
("seahorse", "horse", "sea"),
36+
("baboon", "badger", "baboon"),
37+
("quetzal", "elk", "quetzal"),
38+
),
39+
)
40+
def test_remove_suffix(string, suffix, expected):
41+
result = removesuffix(string, suffix)
42+
assert result == expected
43+
44+
else:
45+
# NOTE: remove this file when pyupgrade --py39-plus removes
46+
# the above block
47+
pass

pandas/util/_str_methods.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Python3.9 introduces removesuffix and remove prefix.
3+
4+
They're reimplemented here for use in Python3.8.
5+
6+
NOTE: when pyupgrade --py39-plus removes nearly everything in this file,
7+
this file and the associated tests should be removed.
8+
"""
9+
from __future__ import annotations
10+
11+
import sys
12+
13+
if sys.version_info < (3, 9):
14+
15+
def removesuffix(string: str, suffix: str) -> str:
16+
if string.endswith(suffix):
17+
return string[: -len(suffix)]
18+
return string
19+
20+
def removeprefix(string: str, prefix: str) -> str:
21+
if string.startswith(prefix):
22+
return string[len(prefix) :]
23+
return string
24+
25+
else:
26+
# NOTE: remove this file when pyupgrade --py39-plus removes
27+
# the above block
28+
pass

setup.cfg

-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ ignore =
194194
# found modulo formatter (incorrect picks up mod operations)
195195
S001,
196196
# controversial
197-
B005,
198-
# controversial
199197
B006,
200198
# controversial
201199
B007,

0 commit comments

Comments
 (0)