BLD: fix build warnings
State: closed

xref #25995, #17936

AFAICT these warnings are caused by cython casting `len(some_ndarray)` to `int` or `Py_ssize_t` but casting `len(some_memoryview)` to `size_t`.  Easiest fix is to declare the affected len variables as `size_t` since we know they'll never be negative.

Labels:
name: Clean

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=h1) Report
> Merging [#26757](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0f3e8e8bd2118d56c7febe5d6081c5058cb22be7?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26757/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26757      +/-   ##
==========================================
- Coverage   91.71%    91.7%   -0.01%     
==========================================
  Files         178      178              
  Lines       50740    50740              
==========================================
- Hits        46538    46533       -5     
- Misses       4202     4207       +5
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.3% <ø> (ø)` | :arrow_up: |
| #single | `41.21% <ø> (-0.11%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.88% <0%> (-0.12%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `90.84% <0%> (-0.11%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=footer). Last update [0f3e8e8...8ac7bb9](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=h1) Report
> Merging [#26757](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/646ff0b9eca3812ad4c191f08ef032aad8479219?src=pr&el=desc) will **increase** coverage by `1.4%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26757/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master   #26757     +/-   ##
=========================================
+ Coverage   90.35%   91.76%   +1.4%     
=========================================
  Files         178      178             
  Lines       50753    50753             
=========================================
+ Hits        45859    46571    +712     
+ Misses       4894     4182    -712
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.35% <ø> (ø)` | :arrow_up: |
| #single | `41.04% <ø> (?)` | |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2NhdGVnb3JpY2FsLnB5) | `95.92% <0%> (+0.12%)` | :arrow_up: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.37% <0%> (+0.16%)` | :arrow_up: |
| [pandas/io/formats/printing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvcHJpbnRpbmcucHk=) | `85.56% <0%> (+1.06%)` | :arrow_up: |
| [pandas/io/clipboard/clipboards.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2NsaXBib2FyZC9jbGlwYm9hcmRzLnB5) | `34.78% <0%> (+2.89%)` | :arrow_up: |
| [pandas/core/computation/expr.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tcHV0YXRpb24vZXhwci5weQ==) | `97.8% <0%> (+3.02%)` | :arrow_up: |
| [pandas/core/computation/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tcHV0YXRpb24vY29tbW9uLnB5) | `89.47% <0%> (+5.26%)` | :arrow_up: |
| [pandas/io/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3B5dGFibGVzLnB5) | `90.29% <0%> (+26.47%)` | :arrow_up: |
| [pandas/core/computation/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/26757/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tcHV0YXRpb24vcHl0YWJsZXMucHk=) | `90.24% <0%> (+27.74%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=footer). Last update [646ff0b...5228964](https://codecov.io/gh/pandas-dev/pandas/pull/26757?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Hmm understood that these would never be negative but since Py_ssize_t is what Python would use for indexing that still seems like the more natural data type to use. Is there not a way to silence these warnings while still using that type for indexers?
@scoder any thoughts on whether this is worthwhile and if not, how to silence the warnings?
`size_t`, or, more generally, unsigned integer types in C, have their advantages and disadvantages. Special use cases like bit manipulation aside, it's nice that they explicitly state that their value cannot be lower than zero. On the downside, they mix badly with C's default (signed) integer behaviour and signed arithmetic in general, and fail to support Python's negative indexing.

I think `Py_ssize_t` is the most obvious return type for `len()`, and memory views shouldn't diverge from this. This is something we can change in Cython 3.0. If you have an opinion, please discuss it at
https://github.com/cython/cython/issues/2992

As for a work-around for now, it's probably best to explicitly cast the return values to `Py_ssize_t`, at least where it's not used in an unsigned context anyway.
Updated to use Py_ssize_t consistently
lgtm. merge on green (and close the issues above if this & others cover)
> and close the issues above if this & others cover

There's still a bunch in interval.pyx that I'm not sure are purely cosmetic (cc @jschendel)
thanks @jbrockmendel