Skip to content

Commit cda37fb

Browse files
authored
STYLE fixup cython linting (#42142)
1 parent 9db86be commit cda37fb

13 files changed

+51
-29
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ repos:
4444
- flake8-bugbear==21.3.2
4545
- pandas-dev-flaker==0.2.0
4646
- id: flake8
47+
alias: flake8-cython
4748
name: flake8 (cython)
4849
types: [cython]
4950
args: [--append-config=flake8/cython.cfg]

flake8/cython.cfg

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
[flake8]
22
filename = *.pyx,*.pxd
3-
select=E501,E302,E203,E111,E114,E221,E303,E128,E231,E126,E265,E305,E301,E127,E261,E271,E129,W291,E222,E241,E123,F403,C400,C401,C402,C403,C404,C405,C406,C407,C408,C409,C410,C411
3+
extend_ignore=
4+
# whitespace before '('
5+
E211,
6+
# missing whitespace around operator
7+
E225,
8+
# missing whitespace around arithmetic operator
9+
E226,
10+
# missing whitespace around bitwise or shift operator
11+
E227,
12+
# ambiguous variable name (# FIXME maybe this one can be fixed)
13+
E741,
14+
# invalid syntax
15+
E999,
16+
# invalid escape sequence (# FIXME maybe this one can be fixed)
17+
W605,

pandas/_libs/algos.pyx

+12-6
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,22 @@ cdef inline numeric kth_smallest_c(numeric* arr, Py_ssize_t k, Py_ssize_t n) nog
274274
j = m
275275

276276
while 1:
277-
while arr[i] < x: i += 1
278-
while x < arr[j]: j -= 1
277+
while arr[i] < x:
278+
i += 1
279+
while x < arr[j]:
280+
j -= 1
279281
if i <= j:
280282
swap(&arr[i], &arr[j])
281-
i += 1; j -= 1
283+
i += 1
284+
j -= 1
282285

283-
if i > j: break
286+
if i > j:
287+
break
284288

285-
if j < k: l = i
286-
if k < i: m = j
289+
if j < k:
290+
l = i
291+
if k < i:
292+
m = j
287293
return arr[k]
288294

289295

pandas/_libs/khash.pxd

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ cdef extern from "khash_python.h":
2626
double imag
2727

2828
bint are_equivalent_khcomplex128_t \
29-
"kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil
29+
"kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil
3030

3131
ctypedef struct khcomplex64_t:
3232
float real
3333
float imag
3434

3535
bint are_equivalent_khcomplex64_t \
36-
"kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil
36+
"kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil
3737

3838
bint are_equivalent_float64_t \
39-
"kh_floats_hash_equal" (float64_t a, float64_t b) nogil
39+
"kh_floats_hash_equal" (float64_t a, float64_t b) nogil
4040

4141
bint are_equivalent_float32_t \
42-
"kh_floats_hash_equal" (float32_t a, float32_t b) nogil
42+
"kh_floats_hash_equal" (float32_t a, float32_t b) nogil
4343

4444
uint32_t kh_python_hash_func(object key)
4545
bint kh_python_hash_equal(object a, object b)

pandas/_libs/lib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,9 @@ cpdef bint is_interval_array(ndarray values):
21072107
return False
21082108
elif numeric:
21092109
if not (
2110-
util.is_float_object(val.left)
2111-
or util.is_integer_object(val.left)
2112-
):
2110+
util.is_float_object(val.left)
2111+
or util.is_integer_object(val.left)
2112+
):
21132113
# i.e. datetime64 or timedelta64
21142114
return False
21152115
elif td64:

pandas/_libs/parsers.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ cdef class TextReader:
356356
thousands=None, # bytes | str
357357
dtype=None,
358358
usecols=None,
359-
on_bad_lines = ERROR,
359+
on_bad_lines=ERROR,
360360
bint na_filter=True,
361361
na_values=None,
362362
na_fvalues=None,
@@ -1442,7 +1442,7 @@ cdef _categorical_convert(parser_t *parser, int64_t col,
14421442

14431443
if na_filter:
14441444
if kh_get_str_starts_item(na_hashset, word):
1445-
# is in NA values
1445+
# is in NA values
14461446
na_count += 1
14471447
codes[i] = NA
14481448
continue
@@ -1578,7 +1578,7 @@ cdef inline int _try_double_nogil(parser_t *parser,
15781578
strcasecmp(word, cposinfty) == 0):
15791579
data[0] = INF
15801580
elif (strcasecmp(word, cneginf) == 0 or
1581-
strcasecmp(word, cneginfty) == 0 ):
1581+
strcasecmp(word, cneginfty) == 0):
15821582
data[0] = NEGINF
15831583
else:
15841584
return 1

pandas/_libs/tslibs/conversion.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
200200

201201
@cython.boundscheck(False)
202202
@cython.wraparound(False)
203-
def ensure_datetime64ns(arr: ndarray, copy: bool=True):
203+
def ensure_datetime64ns(arr: ndarray, copy: bool = True):
204204
"""
205205
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]'
206206
@@ -260,7 +260,7 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):
260260
return result
261261

262262

263-
def ensure_timedelta64ns(arr: ndarray, copy: bool=True):
263+
def ensure_timedelta64ns(arr: ndarray, copy: bool = True):
264264
"""
265265
Ensure a np.timedelta64 array has dtype specifically 'timedelta64[ns]'
266266

pandas/_libs/tslibs/np_datetime.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ cdef extern from "src/datetime/np_datetime.h":
3838
void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
3939
NPY_DATETIMEUNIT fr,
4040
pandas_timedeltastruct *result
41-
) nogil
41+
) nogil
4242

4343
npy_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS
4444

pandas/_libs/tslibs/offsets.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ cdef class BusinessHour(BusinessMixin):
14541454

14551455
def __init__(
14561456
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
1457-
):
1457+
):
14581458
BusinessMixin.__init__(self, n, normalize, offset)
14591459

14601460
# must be validated here to equality check
@@ -3897,7 +3897,7 @@ cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods):
38973897
return result.base
38983898

38993899

3900-
def shift_month(stamp: datetime, months: int, day_opt: object=None) -> datetime:
3900+
def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetime:
39013901
"""
39023902
Given a datetime (or Timestamp) `stamp`, an integer `months` and an
39033903
option `day_opt`, return a new datetimelike that many months later,

pandas/_libs/tslibs/period.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ cdef freq_conv_func get_asfreq_func(int from_freq, int to_freq) nogil:
197197
return <freq_conv_func>asfreq_BtoW
198198
elif to_group == FR_BUS:
199199
return <freq_conv_func>no_op
200-
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
200+
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
201201
return <freq_conv_func>asfreq_BtoDT
202202
else:
203203
return <freq_conv_func>nofunc

pandas/_libs/tslibs/strptime.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,17 @@ def array_strptime(ndarray[object] values, object fmt, bint exact=True, errors='
199199
year = int(found_dict['Y'])
200200
elif parse_code == 2:
201201
month = int(found_dict['m'])
202-
elif parse_code == 3:
203202
# elif group_key == 'B':
203+
elif parse_code == 3:
204204
month = locale_time.f_month.index(found_dict['B'].lower())
205-
elif parse_code == 4:
206205
# elif group_key == 'b':
206+
elif parse_code == 4:
207207
month = locale_time.a_month.index(found_dict['b'].lower())
208-
elif parse_code == 5:
209208
# elif group_key == 'd':
209+
elif parse_code == 5:
210210
day = int(found_dict['d'])
211-
elif parse_code == 6:
212211
# elif group_key == 'H':
212+
elif parse_code == 6:
213213
hour = int(found_dict['H'])
214214
elif parse_code == 7:
215215
hour = int(found_dict['I'])

pandas/_libs/tslibs/timedeltas.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ def _binary_op_method_timedeltalike(op, name):
641641
return NaT
642642

643643
elif is_datetime64_object(other) or (
644-
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)):
644+
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)
645+
):
645646
# this case is for a datetime object that is specifically
646647
# *not* a Timestamp, as the Timestamp case will be
647648
# handled after `_validate_ops_compat` returns False below

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ default 'raise'
19581958
self.second / 3600.0 +
19591959
self.microsecond / 3600.0 / 1e+6 +
19601960
self.nanosecond / 3600.0 / 1e+9
1961-
) / 24.0)
1961+
) / 24.0)
19621962

19631963

19641964
# Aliases

0 commit comments

Comments
 (0)