Skip to content

Commit 371f313

Browse files
jbrockmendelPingviinituutti
authored andcommitted
STYLE: Standardize cython spacing for casting, with linting (pandas-dev#23474)
1 parent 7228881 commit 371f313

19 files changed

+72
-64
lines changed

ci/code_checks.sh

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
4444
flake8 pandas/_libs --filename=*.pxi.in,*.pxd --select=E501,E302,E203,E111,E114,E221,E303,E231,E126,F403
4545
RET=$(($RET + $?)) ; echo $MSG "DONE"
4646

47+
# Check that cython casting is of the form `<type>obj` as opposed to `<type> obj`;
48+
# it doesn't make a difference, but we want to be internally consistent.
49+
# Note: this grep pattern is (intended to be) equivalent to the python
50+
# regex r'(?<![ ->])> '
51+
MSG='Linting .pyx code for spacing conventions in casting' ; echo $MSG
52+
! grep -r -E --include '*.pyx' --include '*.pxi.in' '> ' pandas/_libs | grep -v '[ ->]> '
53+
RET=$(($RET + $?)) ; echo $MSG "DONE"
54+
4755
# readability/casting: Warnings about C casting instead of C++ casting
4856
# runtime/int: Warnings about using C number types instead of C++ ones
4957
# build/include_subdir: Warnings about prefacing included header files with directory

pandas/_libs/algos.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import missing
3232

3333
cdef float64_t FP_ERR = 1e-13
3434

35-
cdef double NaN = <double> np.NaN
35+
cdef double NaN = <double>np.NaN
3636
cdef double nan = NaN
3737

3838
cdef int64_t iNaT = get_nat()
@@ -242,7 +242,7 @@ def nancorr(ndarray[float64_t, ndim=2] mat, bint cov=0, minp=None):
242242
int64_t nobs = 0
243243
float64_t vx, vy, sumx, sumy, sumxx, sumyy, meanx, meany, divisor
244244

245-
N, K = (<object> mat).shape
245+
N, K = (<object>mat).shape
246246

247247
if minp is None:
248248
minpv = 1
@@ -307,7 +307,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
307307
int64_t nobs = 0
308308
float64_t vx, vy, sumx, sumxx, sumyy, mean, divisor
309309

310-
N, K = (<object> mat).shape
310+
N, K = (<object>mat).shape
311311

312312
result = np.empty((K, K), dtype=np.float64)
313313
mask = np.isfinite(mat).view(np.uint8)
@@ -531,7 +531,7 @@ def pad_2d_inplace(ndarray[algos_t, ndim=2] values,
531531
algos_t val
532532
int lim, fill_count = 0
533533

534-
K, N = (<object> values).shape
534+
K, N = (<object>values).shape
535535

536536
# GH#2778
537537
if N == 0:
@@ -730,7 +730,7 @@ def backfill_2d_inplace(ndarray[algos_t, ndim=2] values,
730730
algos_t val
731731
int lim, fill_count = 0
732732

733-
K, N = (<object> values).shape
733+
K, N = (<object>values).shape
734734

735735
# GH#2778
736736
if N == 0:

pandas/_libs/algos_common_helper.pxi.in

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def diff_2d_{{name}}(ndarray[{{c_type}}, ndim=2] arr,
3434
cdef:
3535
Py_ssize_t i, j, sx, sy
3636

37-
sx, sy = (<object> arr).shape
37+
sx, sy = (<object>arr).shape
3838
if arr.flags.f_contiguous:
3939
if axis == 0:
4040
if periods >= 0:
@@ -88,14 +88,14 @@ def put2d_{{name}}_{{dest_name}}(ndarray[{{c_type}}, ndim=2, cast=True] values,
8888
# ensure_dtype
8989
#----------------------------------------------------------------------
9090

91-
cdef int PLATFORM_INT = (<ndarray> np.arange(0, dtype=np.intp)).descr.type_num
91+
cdef int PLATFORM_INT = (<ndarray>np.arange(0, dtype=np.intp)).descr.type_num
9292

9393

9494
def ensure_platform_int(object arr):
9595
# GH3033, GH1392
9696
# platform int is the size of the int pointer, e.g. np.intp
9797
if util.is_array(arr):
98-
if (<ndarray> arr).descr.type_num == PLATFORM_INT:
98+
if (<ndarray>arr).descr.type_num == PLATFORM_INT:
9999
return arr
100100
else:
101101
return arr.astype(np.intp)
@@ -105,7 +105,7 @@ def ensure_platform_int(object arr):
105105

106106
def ensure_object(object arr):
107107
if util.is_array(arr):
108-
if (<ndarray> arr).descr.type_num == NPY_OBJECT:
108+
if (<ndarray>arr).descr.type_num == NPY_OBJECT:
109109
return arr
110110
else:
111111
return arr.astype(np.object_)
@@ -142,7 +142,7 @@ def get_dispatch(dtypes):
142142

143143
def ensure_{{name}}(object arr, copy=True):
144144
if util.is_array(arr):
145-
if (<ndarray> arr).descr.type_num == NPY_{{c_type}}:
145+
if (<ndarray>arr).descr.type_num == NPY_{{c_type}}:
146146
return arr
147147
else:
148148
return arr.astype(np.{{dtype}}, copy=copy)

pandas/_libs/algos_rank_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def rank_2d_{{dtype}}(object in_arr, axis=0, ties_method='average',
263263
np.putmask(values, mask, nan_value)
264264
{{endif}}
265265

266-
n, k = (<object> values).shape
266+
n, k = (<object>values).shape
267267
ranks = np.empty((n, k), dtype='f8')
268268

269269
{{if dtype == 'object'}}

pandas/_libs/algos_take_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ cdef _take_2d(ndarray[take_t, ndim=2] values, object idx):
278278
ndarray[take_t, ndim=2] result
279279
object val
280280

281-
N, K = (<object> values).shape
281+
N, K = (<object>values).shape
282282

283283
if take_t is object:
284284
# evaluated at compile-time

pandas/_libs/groupby.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ from algos import take_2d_axis1_float64_float64, groupsort_indexer, tiebreakers
2222

2323
cdef int64_t iNaT = get_nat()
2424

25-
cdef double NaN = <double> np.NaN
25+
cdef double NaN = <double>np.NaN
2626
cdef double nan = NaN
2727

2828

@@ -115,7 +115,7 @@ def group_median_float64(ndarray[float64_t, ndim=2] out,
115115
assert min_count == -1, "'min_count' only used in add and prod"
116116

117117
ngroups = len(counts)
118-
N, K = (<object> values).shape
118+
N, K = (<object>values).shape
119119

120120
indexer, _counts = groupsort_indexer(labels, ngroups)
121121
counts[:] = _counts[1:]
@@ -152,7 +152,7 @@ def group_cumprod_float64(float64_t[:, :] out,
152152
float64_t[:, :] accum
153153
int64_t lab
154154

155-
N, K = (<object> values).shape
155+
N, K = (<object>values).shape
156156
accum = np.ones_like(values)
157157

158158
with nogil:
@@ -189,7 +189,7 @@ def group_cumsum(numeric[:, :] out,
189189
numeric[:, :] accum
190190
int64_t lab
191191

192-
N, K = (<object> values).shape
192+
N, K = (<object>values).shape
193193
accum = np.zeros_like(values)
194194

195195
with nogil:
@@ -226,7 +226,7 @@ def group_shift_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
226226
int64_t[:] label_seen = np.zeros(ngroups, dtype=np.int64)
227227
int64_t[:, :] label_indexer
228228

229-
N, = (<object> labels).shape
229+
N, = (<object>labels).shape
230230

231231
if periods < 0:
232232
periods = -periods

pandas/_libs/groupby_helper.pxi.in

+16-16
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def group_add_{{name}}(ndarray[{{c_type}}, ndim=2] out,
4848
nobs = np.zeros_like(out)
4949
sumx = np.zeros_like(out)
5050

51-
N, K = (<object> values).shape
51+
N, K = (<object>values).shape
5252

5353
with nogil:
5454

@@ -95,7 +95,7 @@ def group_prod_{{name}}(ndarray[{{c_type}}, ndim=2] out,
9595
nobs = np.zeros_like(out)
9696
prodx = np.ones_like(out)
9797

98-
N, K = (<object> values).shape
98+
N, K = (<object>values).shape
9999

100100
with nogil:
101101
for i in range(N):
@@ -141,7 +141,7 @@ def group_var_{{name}}(ndarray[{{c_type}}, ndim=2] out,
141141
nobs = np.zeros_like(out)
142142
mean = np.zeros_like(out)
143143

144-
N, K = (<object> values).shape
144+
N, K = (<object>values).shape
145145

146146
out[:, :] = 0.0
147147

@@ -193,7 +193,7 @@ def group_mean_{{name}}(ndarray[{{c_type}}, ndim=2] out,
193193
nobs = np.zeros_like(out)
194194
sumx = np.zeros_like(out)
195195

196-
N, K = (<object> values).shape
196+
N, K = (<object>values).shape
197197

198198
with nogil:
199199
for i in range(N):
@@ -238,7 +238,7 @@ def group_ohlc_{{name}}(ndarray[{{c_type}}, ndim=2] out,
238238
if len(labels) == 0:
239239
return
240240

241-
N, K = (<object> values).shape
241+
N, K = (<object>values).shape
242242

243243
if out.shape[1] != 4:
244244
raise ValueError('Output array must have 4 columns')
@@ -312,14 +312,14 @@ def group_last_{{name}}(ndarray[{{c_type}}, ndim=2] out,
312312
if not len(values) == len(labels):
313313
raise AssertionError("len(index) != len(labels)")
314314

315-
nobs = np.zeros((<object> out).shape, dtype=np.int64)
315+
nobs = np.zeros((<object>out).shape, dtype=np.int64)
316316
{{if name == 'object'}}
317-
resx = np.empty((<object> out).shape, dtype=object)
317+
resx = np.empty((<object>out).shape, dtype=object)
318318
{{else}}
319319
resx = np.empty_like(out)
320320
{{endif}}
321321

322-
N, K = (<object> values).shape
322+
N, K = (<object>values).shape
323323

324324
{{if name == "object"}}
325325
if True: # make templating happy
@@ -369,14 +369,14 @@ def group_nth_{{name}}(ndarray[{{c_type}}, ndim=2] out,
369369
if not len(values) == len(labels):
370370
raise AssertionError("len(index) != len(labels)")
371371

372-
nobs = np.zeros((<object> out).shape, dtype=np.int64)
372+
nobs = np.zeros((<object>out).shape, dtype=np.int64)
373373
{{if name=='object'}}
374-
resx = np.empty((<object> out).shape, dtype=object)
374+
resx = np.empty((<object>out).shape, dtype=object)
375375
{{else}}
376376
resx = np.empty_like(out)
377377
{{endif}}
378378

379-
N, K = (<object> values).shape
379+
N, K = (<object>values).shape
380380

381381
{{if name == "object"}}
382382
if True: # make templating happy
@@ -462,7 +462,7 @@ def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
462462

463463
tiebreak = tiebreakers[ties_method]
464464
keep_na = na_option == 'keep'
465-
N, K = (<object> values).shape
465+
N, K = (<object>values).shape
466466
grp_sizes = np.ones_like(out)
467467

468468
# Copy values into new array in order to fill missing data
@@ -635,7 +635,7 @@ def group_max(ndarray[groupby_t, ndim=2] out,
635635
maxx.fill(-np.inf)
636636
nan_val = NAN
637637

638-
N, K = (<object> values).shape
638+
N, K = (<object>values).shape
639639

640640
with nogil:
641641
for i in range(N):
@@ -697,7 +697,7 @@ def group_min(ndarray[groupby_t, ndim=2] out,
697697
minx.fill(np.inf)
698698
nan_val = NAN
699699

700-
N, K = (<object> values).shape
700+
N, K = (<object>values).shape
701701

702702
with nogil:
703703
for i in range(N):
@@ -744,7 +744,7 @@ def group_cummin(ndarray[groupby_t, ndim=2] out,
744744
ndarray[groupby_t, ndim=2] accum
745745
int64_t lab
746746

747-
N, K = (<object> values).shape
747+
N, K = (<object>values).shape
748748
accum = np.empty_like(values)
749749
if groupby_t is int64_t:
750750
accum.fill(_int64_max)
@@ -792,7 +792,7 @@ def group_cummax(ndarray[groupby_t, ndim=2] out,
792792
ndarray[groupby_t, ndim=2] accum
793793
int64_t lab
794794

795-
N, K = (<object> values).shape
795+
N, K = (<object>values).shape
796796
accum = np.empty_like(values)
797797
if groupby_t is int64_t:
798798
accum.fill(-_int64_max)

pandas/_libs/hashtable_class_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ cdef class {{name}}HashTable(HashTable):
318318
for i in range(n):
319319
key = keys[i]
320320
k = kh_put_{{dtype}}(self.table, key, &ret)
321-
self.table.vals[k] = <Py_ssize_t> values[i]
321+
self.table.vals[k] = <Py_ssize_t>values[i]
322322

323323
@cython.boundscheck(False)
324324
def map_locations(self, ndarray[{{dtype}}_t, ndim=1] values):

pandas/_libs/join.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from numpy cimport (ndarray,
1111
cnp.import_array()
1212

1313

14-
cdef double NaN = <double> np.NaN
14+
cdef double NaN = <double>np.NaN
1515
cdef double nan = NaN
1616

1717
from pandas._libs.algos import groupsort_indexer, ensure_platform_int

pandas/_libs/lib.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def fast_zip(list ndarrays):
304304

305305
# initialize tuples on first pass
306306
arr = ndarrays[0]
307-
it = <flatiter> PyArray_IterNew(arr)
307+
it = <flatiter>PyArray_IterNew(arr)
308308
for i in range(n):
309309
val = PyArray_GETITEM(arr, PyArray_ITER_DATA(it))
310310
tup = PyTuple_New(k)
@@ -316,7 +316,7 @@ def fast_zip(list ndarrays):
316316

317317
for j in range(1, k):
318318
arr = ndarrays[j]
319-
it = <flatiter> PyArray_IterNew(arr)
319+
it = <flatiter>PyArray_IterNew(arr)
320320
if len(arr) != n:
321321
raise ValueError('all arrays must be same length')
322322

@@ -1994,8 +1994,8 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
19941994
break
19951995
elif util.is_integer_object(val):
19961996
seen.int_ = 1
1997-
floats[i] = <float64_t> val
1998-
complexes[i] = <double complex> val
1997+
floats[i] = <float64_t>val
1998+
complexes[i] = <double complex>val
19991999
if not seen.null_:
20002000
seen.saw_int(int(val))
20012001

pandas/_libs/missing.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cimport util
1313
from tslibs.np_datetime cimport get_timedelta64_value, get_datetime64_value
1414
from tslibs.nattype import NaT
1515

16-
cdef double INF = <double> np.inf
16+
cdef double INF = <double>np.inf
1717
cdef double NEGINF = -INF
1818

1919
cdef int64_t NPY_NAT = util.get_nat()
@@ -224,7 +224,7 @@ def isnaobj2d(ndarray arr):
224224

225225
assert arr.ndim == 2, "'arr' must be 2-D."
226226

227-
n, m = (<object> arr).shape
227+
n, m = (<object>arr).shape
228228
result = np.zeros((n, m), dtype=np.uint8)
229229
for i in range(n):
230230
for j in range(m):
@@ -268,7 +268,7 @@ def isnaobj2d_old(ndarray arr):
268268

269269
assert arr.ndim == 2, "'arr' must be 2-D."
270270

271-
n, m = (<object> arr).shape
271+
n, m = (<object>arr).shape
272272
result = np.zeros((n, m), dtype=np.uint8)
273273
for i in range(n):
274274
for j in range(m):

0 commit comments

Comments
 (0)