1
1
from __future__ import annotations
2
2
3
- from typing import Any
3
+ from typing import (
4
+ TYPE_CHECKING ,
5
+ Any ,
6
+ )
4
7
5
8
import numpy as np
6
9
33
36
dtype_to_arrow_c_fmt ,
34
37
)
35
38
39
+ if TYPE_CHECKING :
40
+ from pandas .core .interchange .dataframe_protocol import Buffer
41
+
36
42
_NP_KINDS = {
37
43
"i" : DtypeKind .INT ,
38
44
"u" : DtypeKind .UINT ,
@@ -296,7 +302,7 @@ def get_buffers(self) -> ColumnBuffers:
296
302
297
303
def _get_data_buffer (
298
304
self ,
299
- ) -> tuple [PandasBuffer , Any ]: # Any is for self.dtype tuple
305
+ ) -> tuple [Buffer , tuple [ DtypeKind , int , str , str ]]:
300
306
"""
301
307
Return the buffer containing the data and the buffer's associated dtype.
302
308
"""
@@ -307,7 +313,7 @@ def _get_data_buffer(
307
313
np_arr = self ._col .dt .tz_convert (None ).to_numpy ()
308
314
else :
309
315
np_arr = self ._col .to_numpy ()
310
- buffer = PandasBuffer (np_arr , allow_copy = self ._allow_copy )
316
+ buffer : Buffer = PandasBuffer (np_arr , allow_copy = self ._allow_copy )
311
317
dtype = (
312
318
DtypeKind .INT ,
313
319
64 ,
@@ -324,7 +330,9 @@ def _get_data_buffer(
324
330
arr = self ._col .array
325
331
if isinstance (self ._col .dtype , ArrowDtype ):
326
332
buffer = PandasBufferPyarrow (
327
- arr ._pa_array , is_validity = False , allow_copy = self ._allow_copy
333
+ arr ._pa_array , # type: ignore[attr-defined]
334
+ is_validity = False ,
335
+ allow_copy = self ._allow_copy ,
328
336
)
329
337
if self .dtype [0 ] == DtypeKind .BOOL :
330
338
dtype = (
@@ -371,7 +379,7 @@ def _get_data_buffer(
371
379
372
380
return buffer , dtype
373
381
374
- def _get_validity_buffer (self ) -> tuple [PandasBuffer , Any ]:
382
+ def _get_validity_buffer (self ) -> tuple [Buffer , Any ] | None :
375
383
"""
376
384
Return the buffer containing the mask values indicating missing data and
377
385
the buffer's associated dtype.
@@ -382,10 +390,15 @@ def _get_validity_buffer(self) -> tuple[PandasBuffer, Any]:
382
390
if isinstance (self ._col .dtype , ArrowDtype ):
383
391
arr = self ._col .array
384
392
dtype = (DtypeKind .BOOL , 1 , ArrowCTypes .BOOL , Endianness .NATIVE )
385
- if all (chunk .buffers ()[0 ] is None for chunk in arr ._pa_array .chunks ):
393
+ if all (
394
+ chunk .buffers ()[0 ] is None
395
+ for chunk in arr ._pa_array .chunks # type: ignore[attr-defined]
396
+ ):
386
397
return None
387
- buffer = PandasBufferPyarrow (
388
- arr ._pa_array , is_validity = True , allow_copy = self ._allow_copy
398
+ buffer : Buffer = PandasBufferPyarrow (
399
+ arr ._pa_array , # type: ignore[attr-defined]
400
+ is_validity = True ,
401
+ allow_copy = self ._allow_copy ,
389
402
)
390
403
return buffer , dtype
391
404
0 commit comments