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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ repos:
rev: v0.0.244
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Copy link
Member

Choose a reason for hiding this comment

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

can we not add this please? there's a few false positives which risk confusing contributors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Would remove it.

- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
Expand Down
652 changes: 371 additions & 281 deletions doc/source/user_guide/style.ipynb

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,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: 3 additions & 3 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ class CSSResolver:
SIDES = ("top", "right", "bottom", "left")

CSS_EXPANSIONS = {
**{
{
(f"border-{prop}" if prop else "border"): _border_expander(prop)
for prop in ["", "top", "right", "bottom", "left"]
},
**{
{
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}"),
},
Copy link
Member

Choose a reason for hiding this comment

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

looks like this one is causing some test failures

Copy link
Member

Choose a reason for hiding this comment

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

You probably want

    CSS_EXPANSIONS = {
        **{
            (f"border-{prop}" if prop else "border"): _border_expander(prop)
            for prop in ["", "top", "right", "bottom", "left"]
        },
        **{
            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}"),
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the PIE error that was generated.

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 @@ -5119,13 +5119,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
1 change: 0 additions & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ class StataMissingValue:
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.

for i in range(27):
key = struct.unpack("<d", float64_base)[0]
MISSING_VALUES[key] = "."
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}),
Copy link
Member

Choose a reason for hiding this comment

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

this isn't right, you want

"column_dtypes": DictLike(A=np.int8, B=np.float32),

you can run this test with

pytest pandas/tests/frame/methods/test_to_records.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Running pytest command is giving:

ImportError while loading conftest 'C:\Users\Victor Momodu\Documents\Programming\Data Science\Code\pandas\pandas\conftest.py'.      
pandas\__init__.py:22: in <module>
    from pandas.compat import is_numpy_dev as _is_numpy_dev  # pyright: ignore # noqa:F401
pandas\compat\__init__.py:25: in <module>
    from pandas.compat.numpy import (
pandas\compat\numpy\__init__.py:4: in <module>
    from pandas.util.version import Version
pandas\util\__init__.py:2: in <module>
    from pandas.util._decorators import (  # noqa:F401
pandas\util\_decorators.py:14: in <module>
    from pandas._libs.properties import cache_readonly
pandas\_libs\__init__.py:13: in <module>
    from pandas._libs.interval import Interval
E   ModuleNotFoundError: No module named 'pandas._libs.interval'

I have checked the module that was said to be missing and it exists. I searched and found this.

I have made the required change and will be pushing it to remote.

Copy link
Member

Choose a reason for hiding this comment

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

you need to follow the instructions here to run tests https://pandas.pydata.org/docs/dev/development/contributing_environment.html

"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",
]

ignore = [
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