Skip to content

Commit 4d89314

Browse files
Fixed another code smell and added report.md
1 parent 6b3e10f commit 4d89314

File tree

3 files changed

+1214
-3
lines changed

3 files changed

+1214
-3
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Static Analysis Report
2+
3+
## Tool Used
4+
5+
**Tool:** ruff v0.14.3
6+
**Command:** `ruff check pandas/util/_validators.py --select ALL --ignore E501,D203,D213`
7+
**Target File:** `pandas/util/_validators.py`
8+
9+
## Key Findings
10+
11+
Ruff identified **88 code smells** in the target file across multiple categories:
12+
13+
- **EM102 (25 instances):** F-string literals in exceptions - should assign to variable first
14+
- **TRY003 (15 instances):** Long exception messages outside exception class
15+
- **ANN001 (23 instances):** Missing type annotations for function arguments
16+
- **D205/D401 (12 instances):** Docstring formatting issues
17+
- **PLC0415 (1 instance):** Import not at top-level of file
18+
- **SIM102 (1 instance):** Nested if statements can be combined
19+
- **FBT001/FBT002 (8 instances):** Boolean-typed positional arguments
20+
21+
Total: **88 code smells detected**
22+
23+
## Fixes Summary
24+
25+
### Fix #1: F-String in Exception (EM102)
26+
**Assigned to:** Sandeep Ramavath
27+
**Location:** Line 292 in `pandas/util/_validators.py`
28+
**Issue:** Exception uses f-string literal directly instead of assigning to variable first
29+
30+
**Before:**
31+
```python
32+
def validate_bool_kwarg_nullable(value, arg_name: str) -> None:
33+
if (
34+
value is lib.no_default
35+
or isinstance(value, bool)
36+
or value is None
37+
or value is NA
38+
or (lib.is_float(value) and np.isnan(value))
39+
):
40+
return
41+
raise ValueError(f"{arg_name} must be None, pd.NA, np.nan, True, or False; got {value}")
42+
```
43+
44+
**After:**
45+
```python
46+
def validate_bool_kwarg_nullable(value, arg_name: str) -> None:
47+
if (
48+
value is lib.no_default
49+
or isinstance(value, bool)
50+
or value is None
51+
or value is NA
52+
or (lib.is_float(value) and np.isnan(value))
53+
):
54+
return
55+
msg = f"{arg_name} must be None, pd.NA, np.nan, True, or False; got {value}"
56+
raise ValueError(msg)
57+
```
58+
59+
**Rationale:** Assigning error messages to variables before raising exceptions improves code maintainability, makes testing easier, and follows Python best practices for exception handling.
60+
61+
---
62+
63+
### Fix #2: Import Not at Top-Level (PLC0415)
64+
**Assigned to:** Nithikesh Bobbili
65+
**Location:** Line 314 in `pandas/util/_validators.py`
66+
**Issue:** Import statement inside function body instead of at module level
67+
68+
**Before:**
69+
```python
70+
def validate_fillna_kwargs(value, method, validate_scalar_dict_value: bool = True):
71+
"""Validate the keyword arguments to 'fillna'.
72+
...
73+
"""
74+
from pandas.core.missing import clean_fill_method # Import inside function
75+
76+
if value is None and method is None:
77+
raise ValueError("Must specify a fill 'value' or 'method'.")
78+
if value is None and method is not None:
79+
method = clean_fill_method(method)
80+
# ...
81+
```
82+
83+
**After:**
84+
```python
85+
# At module level (top of file, after line 20):
86+
from pandas.core.missing import clean_fill_method
87+
88+
# ...
89+
90+
def validate_fillna_kwargs(value, method, validate_scalar_dict_value: bool = True):
91+
"""Validate the keyword arguments to 'fillna'.
92+
...
93+
"""
94+
if value is None and method is None:
95+
raise ValueError("Must specify a fill 'value' or 'method'.")
96+
if value is None and method is not None:
97+
method = clean_fill_method(method)
98+
# ...
99+
```
100+
101+
**Rationale:** Module-level imports are executed once at module load time rather than every function call, improving performance. It also makes dependencies more visible and follows PEP 8 conventions.
102+
103+
---

0 commit comments

Comments
 (0)