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 all commits
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
9 changes: 8 additions & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,14 @@ class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
priority = 0.5

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

return removesuffix(MethodDocumenter.format_name(self), ".__call__")
else:
return MethodDocumenter.format_name(self).removesuffix(".__call__")


class PandasAutosummary(Autosummary):
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/ops/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

from functools import wraps
import sys
from typing import Callable

from pandas._libs.lib import item_from_zerodim
Expand Down Expand Up @@ -52,7 +53,16 @@ def _unpack_zerodim_and_defer(method, name: str):
-------
method
"""
is_cmp = name.strip("__") in {"eq", "ne", "lt", "le", "gt", "ge"}
if sys.version_info < (3, 9):
from pandas.util._str_methods import (
removeprefix,
removesuffix,
)

stripped_name = removesuffix(removeprefix(name, "__"), "__")
else:
stripped_name = name.removeprefix("__").removesuffix("__")
is_cmp = stripped_name in {"eq", "ne", "lt", "le", "gt", "ge"}

@wraps(method)
def new_method(self, other):
Expand Down
17 changes: 8 additions & 9 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Callable # noqa: PDF001
import functools
import re
import sys
import textwrap
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -462,16 +463,14 @@ def removeprefix(text: str) -> str:
return self._str_map(removeprefix)

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

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

return self._str_map(removesuffix)
return self._str_map(functools.partial(removesuffix, suffix=suffix))
else:
return self._str_map(lambda x: x.removesuffix(suffix))

def _str_extract(self, pat: str, flags: int = 0, expand: bool = True):
regex = re.compile(pat, flags=flags)
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
47 changes: 47 additions & 0 deletions pandas/tests/util/test_str_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys

import pytest

if sys.version_info < (3, 9):
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

else:
# NOTE: remove this file when pyupgrade --py39-plus removes
# the above block
pass
28 changes: 28 additions & 0 deletions pandas/util/_str_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Python3.9 introduces removesuffix and remove prefix.

They're reimplemented here for use in Python3.8.

NOTE: when pyupgrade --py39-plus removes nearly everything in this file,
this file and the associated tests should be removed.
"""
from __future__ import annotations

import sys

if sys.version_info < (3, 9):

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

else:
# NOTE: remove this file when pyupgrade --py39-plus removes
# the above block
pass
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ ignore =
# found modulo formatter (incorrect picks up mod operations)
S001,
# controversial
B005,
# controversial
B006,
# controversial
B007,
Expand Down