Skip to content

STYLE fixup cython linting #42142

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 5 commits into from
Aug 8, 2021
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repos:
- flake8-bugbear==21.3.2
- pandas-dev-flaker==0.2.0
- id: flake8
alias: flake8-cython
name: flake8 (cython)
types: [cython]
args: [--append-config=flake8/cython.cfg]
Expand Down
16 changes: 15 additions & 1 deletion flake8/cython.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
[flake8]
filename = *.pyx,*.pxd
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
extend_ignore=
# whitespace before '('
E211,
# missing whitespace around operator
E225,
# missing whitespace around arithmetic operator
E226,
# missing whitespace around bitwise or shift operator
E227,
# ambiguous variable name (# FIXME maybe this one can be fixed)
E741,
# invalid syntax
E999,
# invalid escape sequence (# FIXME maybe this one can be fixed)
W605,
18 changes: 12 additions & 6 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,22 @@ cdef inline numeric kth_smallest_c(numeric* arr, Py_ssize_t k, Py_ssize_t n) nog
j = m

while 1:
while arr[i] < x: i += 1
while x < arr[j]: j -= 1
while arr[i] < x:
i += 1
while x < arr[j]:
j -= 1
if i <= j:
swap(&arr[i], &arr[j])
i += 1; j -= 1
i += 1
j -= 1

if i > j: break
if i > j:
break

if j < k: l = i
if k < i: m = j
if j < k:
l = i
if k < i:
m = j
return arr[k]


Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/khash.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ cdef extern from "khash_python.h":
double imag

bint are_equivalent_khcomplex128_t \
"kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil
"kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil

ctypedef struct khcomplex64_t:
float real
float imag

bint are_equivalent_khcomplex64_t \
"kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil
"kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil

bint are_equivalent_float64_t \
"kh_floats_hash_equal" (float64_t a, float64_t b) nogil
"kh_floats_hash_equal" (float64_t a, float64_t b) nogil

bint are_equivalent_float32_t \
"kh_floats_hash_equal" (float32_t a, float32_t b) nogil
"kh_floats_hash_equal" (float32_t a, float32_t b) nogil

uint32_t kh_python_hash_func(object key)
bint kh_python_hash_equal(object a, object b)
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2107,9 +2107,9 @@ cpdef bint is_interval_array(ndarray values):
return False
elif numeric:
if not (
util.is_float_object(val.left)
or util.is_integer_object(val.left)
):
util.is_float_object(val.left)
or util.is_integer_object(val.left)
):
# i.e. datetime64 or timedelta64
return False
elif td64:
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ cdef class TextReader:
thousands=None, # bytes | str
dtype=None,
usecols=None,
on_bad_lines = ERROR,
on_bad_lines=ERROR,
bint na_filter=True,
na_values=None,
na_fvalues=None,
Expand Down Expand Up @@ -1442,7 +1442,7 @@ cdef _categorical_convert(parser_t *parser, int64_t col,

if na_filter:
if kh_get_str_starts_item(na_hashset, word):
Copy link
Member

Choose a reason for hiding this comment

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

the linter wants comments before the if?

Copy link
Member Author

Choose a reason for hiding this comment

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

or just an indented comment - I've gone with that, thanks

# is in NA values
# is in NA values
na_count += 1
codes[i] = NA
continue
Expand Down Expand Up @@ -1578,7 +1578,7 @@ cdef inline int _try_double_nogil(parser_t *parser,
strcasecmp(word, cposinfty) == 0):
data[0] = INF
elif (strcasecmp(word, cneginf) == 0 or
strcasecmp(word, cneginfty) == 0 ):
strcasecmp(word, cneginfty) == 0):
data[0] = NEGINF
else:
return 1
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:

@cython.boundscheck(False)
@cython.wraparound(False)
def ensure_datetime64ns(arr: ndarray, copy: bool=True):
def ensure_datetime64ns(arr: ndarray, copy: bool = True):
"""
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]'

Expand Down Expand Up @@ -260,7 +260,7 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):
return result


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

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cdef extern from "src/datetime/np_datetime.h":
void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
NPY_DATETIMEUNIT fr,
pandas_timedeltastruct *result
) nogil
) nogil

npy_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ cdef class BusinessHour(BusinessMixin):

def __init__(
self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0)
):
):
BusinessMixin.__init__(self, n, normalize, offset)

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


def shift_month(stamp: datetime, months: int, day_opt: object=None) -> datetime:
def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetime:
"""
Given a datetime (or Timestamp) `stamp`, an integer `months` and an
option `day_opt`, return a new datetimelike that many months later,
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ cdef freq_conv_func get_asfreq_func(int from_freq, int to_freq) nogil:
return <freq_conv_func>asfreq_BtoW
elif to_group == FR_BUS:
return <freq_conv_func>no_op
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_BtoDT
else:
return <freq_conv_func>nofunc
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ def array_strptime(ndarray[object] values, object fmt, bint exact=True, errors='
year = int(found_dict['Y'])
elif parse_code == 2:
month = int(found_dict['m'])
elif parse_code == 3:
# elif group_key == 'B':
elif parse_code == 3:
month = locale_time.f_month.index(found_dict['B'].lower())
elif parse_code == 4:
# elif group_key == 'b':
elif parse_code == 4:
month = locale_time.a_month.index(found_dict['b'].lower())
elif parse_code == 5:
# elif group_key == 'd':
elif parse_code == 5:
day = int(found_dict['d'])
elif parse_code == 6:
# elif group_key == 'H':
elif parse_code == 6:
hour = int(found_dict['H'])
elif parse_code == 7:
hour = int(found_dict['I'])
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ def _binary_op_method_timedeltalike(op, name):
return NaT

elif is_datetime64_object(other) or (
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)):
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)
):
# this case is for a datetime object that is specifically
# *not* a Timestamp, as the Timestamp case will be
# handled after `_validate_ops_compat` returns False below
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ default 'raise'
self.second / 3600.0 +
self.microsecond / 3600.0 / 1e+6 +
self.nanosecond / 3600.0 / 1e+9
) / 24.0)
) / 24.0)


# Aliases
Expand Down