Skip to content

STYLE ruff: enable PIE #51434

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 15 commits into from
Feb 22, 2023
Merged
4 changes: 1 addition & 3 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ def index_flat(request):
key
for key, value in indices_dict.items()
if not (
key.startswith("int")
or key.startswith("uint")
or key.startswith("float")
key.startswith(("int", "uint", "float"))
or key in ["range", "empty", "repeats", "bool-dtype"]
)
and not isinstance(value, MultiIndex)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ def freq(self):
@classmethod
def _parse_dtype_strict(cls, freq: str_type) -> BaseOffset:
if isinstance(freq, str): # note: freq is already of type str!
if freq.startswith("period[") or freq.startswith("Period["):
if freq.startswith(("Period[", "period[")):
m = cls._match.search(freq)
if m is not None:
freq = m.group("freq")
Expand All @@ -916,7 +916,7 @@ def construct_from_string(cls, string: str_type) -> PeriodDtype:
"""
if (
isinstance(string, str)
and (string.startswith("period[") or string.startswith("Period["))
and (string.startswith(("period[", "Period[")))
or isinstance(string, BaseOffset)
):
# do not parse string like U as period[U]
Expand Down Expand Up @@ -980,7 +980,7 @@ def is_dtype(cls, dtype: object) -> bool:
if isinstance(dtype, str):
# PeriodDtype can be instantiated from freq string like "U",
# but doesn't regard freq str like "U" as dtype.
if dtype.startswith("period[") or dtype.startswith("Period["):
if dtype.startswith(("period[", "Period[")):
try:
return cls._parse_dtype_strict(dtype) is not None
except ValueError:
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,6 @@ def view(self, dtype: Dtype | None = None) -> Series:

# ----------------------------------------------------------------------
# NDArray Compat
_HANDLED_TYPES = (Index, ExtensionArray, np.ndarray)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error was PIE794. The fix that was suggested was removing one of the instances.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you're right, it is indeed duplicated. thanks!


def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
"""
Return the values as a NumPy array.
Expand Down
6 changes: 2 additions & 4 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ class CSSResolver:
f"border-{prop}": _side_expander(f"border-{{:s}}-{prop}")
for prop in ["color", "style", "width"]
},
**{
"margin": _side_expander("margin-{:s}"),
"padding": _side_expander("padding-{:s}"),
},
"margin": _side_expander("margin-{:s}"),
"padding": _side_expander("padding-{:s}"),
}

def __call__(
Expand Down
3 changes: 1 addition & 2 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,8 +1415,7 @@ def is_ok(col) -> bool:

col_lower = col.lower()
if (
col_lower.endswith("_at")
or col_lower.endswith("_time")
col_lower.endswith(("_at", "_time"))
or col_lower == "modified"
or col_lower == "date"
or col_lower == "datetime"
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5123,13 +5123,13 @@ def _dtype_to_kind(dtype_str: str) -> str:
"""
dtype_str = _ensure_decoded(dtype_str)

if dtype_str.startswith("string") or dtype_str.startswith("bytes"):
if dtype_str.startswith(("string", "bytes")):
kind = "string"
elif dtype_str.startswith("float"):
kind = "float"
elif dtype_str.startswith("complex"):
kind = "complex"
elif dtype_str.startswith("int") or dtype_str.startswith("uint"):
elif dtype_str.startswith(("int", "uint")):
kind = "integer"
elif dtype_str.startswith("datetime64"):
kind = "datetime64"
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,23 +865,23 @@ class StataMissingValue:
MISSING_VALUES[i + b] = "." + chr(96 + i)

float32_base: bytes = b"\x00\x00\x00\x7f"
increment: int = struct.unpack("<i", b"\x00\x08\x00\x00")[0]
increment_32: int = struct.unpack("<i", b"\x00\x08\x00\x00")[0]
for i in range(27):
key = struct.unpack("<f", float32_base)[0]
MISSING_VALUES[key] = "."
if i > 0:
MISSING_VALUES[key] += chr(96 + i)
int_value = struct.unpack("<i", struct.pack("<f", key))[0] + increment
int_value = struct.unpack("<i", struct.pack("<f", key))[0] + increment_32
float32_base = struct.pack("<i", int_value)

float64_base: bytes = b"\x00\x00\x00\x00\x00\x00\xe0\x7f"
increment = struct.unpack("q", b"\x00\x00\x00\x00\x00\x01\x00\x00")[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error was PIE794. The fix that was suggested was removing one of the instances.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - I don't think we can remove it though

can you rename the first one to increment_32 and the second one to increment_64 please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have corrected this.

increment_64 = struct.unpack("q", b"\x00\x00\x00\x00\x00\x01\x00\x00")[0]
for i in range(27):
key = struct.unpack("<d", float64_base)[0]
MISSING_VALUES[key] = "."
if i > 0:
MISSING_VALUES[key] += chr(96 + i)
int_value = struct.unpack("q", struct.pack("<d", key))[0] + increment
int_value = struct.unpack("q", struct.pack("<d", key))[0] + increment_64
float64_base = struct.pack("q", int_value)

BASE_MISSING_VALUES: Final = {
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/copy_view/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ def test_subset_chained_getitem(
# with ArrayManager, it doesn't matter whether we have
# single vs mixed block or numpy vs nullable dtypes
subset_is_view = test_callspec.endswith(
"column-iloc-slice"
) or test_callspec.endswith("column-loc-slice")
("column-iloc-slice", "column-loc-slice")
)

# modify subset -> don't modify parent
subset = method(df)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def keys(self):
df = DataFrame({"A": [1, 2], "B": [0.2, 1.5], "C": ["a", "bc"]})

dtype_mappings = {
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
"column_dtypes": DictLike(A=np.int8, B=np.float32),
"index_dtypes": f"{tm.ENDIAN}U2",
}

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def mi_styler(mi_df):
def mi_styler_comp(mi_styler):
# comprehensively add features to mi_styler
mi_styler = mi_styler._copy(deepcopy=True)
mi_styler.css = {**mi_styler.css, **{"row": "ROW", "col": "COL"}}
mi_styler.css = {**mi_styler.css, "row": "ROW", "col": "COL"}
mi_styler.uuid_len = 5
mi_styler.uuid = "abcde"
mi_styler.set_caption("capt")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def _is_annual(rule: str) -> bool:

def _is_quarterly(rule: str) -> bool:
rule = rule.upper()
return rule == "Q" or rule.startswith("Q-") or rule.startswith("BQ")
return rule == "Q" or rule.startswith(("Q-", "BQ"))


def _is_monthly(rule: str) -> bool:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ select = [
"Q",
# pylint
"PLE", "PLR", "PLW",
# misc lints
"PIE",
# tidy imports
"TID",
]
Expand Down
6 changes: 1 addition & 5 deletions scripts/validate_exception_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ def __init__(self, path: str, exception_set: set[str]) -> None:

def visit_ClassDef(self, node) -> None:
def is_an_exception_subclass(base_id: str) -> bool:
return (
base_id == "Exception"
or base_id.endswith("Warning")
or base_id.endswith("Error")
)
return base_id == "Exception" or base_id.endswith(("Warning", "Error"))

exception_classes = []

Expand Down