Skip to content

STYLE enable B005 check in flake8-bugbear #49707

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

Merged
merged 10 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
priority = 0.5

def format_name(self):
return MethodDocumenter.format_name(self).rstrip(".__call__")
return pandas.util._str_methods.removesuffix(
MethodDocumenter.format_name(self), ".__call__"
)


class PandasAutosummary(Autosummary):
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/ops/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from pandas._libs.lib import item_from_zerodim
from pandas._libs.missing import is_matching_na
from pandas._typing import F
from pandas.util._str_methods import (
removeprefix,
removesuffix,
)

from pandas.core.dtypes.generic import (
ABCDataFrame,
Expand Down Expand Up @@ -52,7 +56,8 @@ def _unpack_zerodim_and_defer(method, name: str):
-------
method
"""
is_cmp = name.strip("__") in {"eq", "ne", "lt", "le", "gt", "ge"}
stripped_name = removesuffix(removeprefix(name, "__"), "__")
is_cmp = stripped_name in {"eq", "ne", "lt", "le", "gt", "ge"}

@wraps(method)
def new_method(self, other):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ def test_get_schema2(self, test_frame1):
def _get_sqlite_column_type(self, schema, column):

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

Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/util/test_str_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from pandas.util._str_methods import (
removeprefix,
removesuffix,
)


@pytest.mark.parametrize(
"string, prefix, expected",
(
("wildcat", "wild", "cat"),
("blackbird", "black", "bird"),
("housefly", "house", "fly"),
("ladybug", "lady", "bug"),
("rattlesnake", "rattle", "snake"),
("baboon", "badger", "baboon"),
("quetzal", "elk", "quetzal"),
),
)
def test_remove_prefix(string, prefix, expected):
result = removeprefix(string, prefix)
assert result == expected


@pytest.mark.parametrize(
"string, suffix, expected",
(
("wildcat", "cat", "wild"),
("blackbird", "bird", "black"),
("housefly", "fly", "house"),
("ladybug", "bug", "lady"),
("rattlesnake", "snake", "rattle"),
("seahorse", "horse", "sea"),
("baboon", "badger", "baboon"),
("quetzal", "elk", "quetzal"),
),
)
def test_remove_suffix(string, suffix, expected):
result = removesuffix(string, suffix)
assert result == expected
18 changes: 18 additions & 0 deletions pandas/util/_str_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Python3.9 introduces removesuffix and remove prefix.

They're reimplemented here for use in Python3.8.
"""
from __future__ import annotations


def removesuffix(string: str, suffix: str) -> str:
if string.endswith(suffix):
return string[: -len(suffix)]
return string


def removeprefix(string: str, prefix: str) -> str:
if string.startswith(prefix):
return string[len(prefix) :]
return string
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ ignore =
# found modulo formatter (incorrect picks up mod operations)
S001,
# controversial
B005,
# controversial
B006,
# controversial
B007,
Expand Down