Skip to content

Commit 0fd68ab

Browse files
authored
HDFStore.__iter__ (#539)
* HDFStore.__iter__ * enable Y034 * added missing IntervalIndex.closed
1 parent 15e66d0 commit 0fd68ab

File tree

12 files changed

+29
-11
lines changed

12 files changed

+29
-11
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
- flake8-pyi==23.1.2
3131
types: [pyi]
3232
args: [
33-
--ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 W503 Y019 Y034 Y037 Y041 Y042,
33+
--ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 W503 Y019 Y037 Y041 Y042,
3434
# TypeVars in private files are already private
3535
--per-file-ignores=_*.pyi:Y001
3636
]

pandas-stubs/_libs/missing.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from typing_extensions import Self
2+
13
class NAType:
2-
def __new__(cls, *args, **kwargs) -> NAType: ...
4+
def __new__(cls, *args, **kwargs) -> Self: ...
35
def __format__(self, format_spec: str) -> str: ...
46
def __bool__(self) -> None: ...
57
def __hash__(self) -> int: ...

pandas-stubs/core/indexes/category.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import numpy as np
44
from pandas.core import accessor
55
from pandas.core.indexes.base import Index # , maybe_extract_name
66
from pandas.core.indexes.extension import ExtensionIndex
7+
from typing_extensions import Self
78

89
from pandas._typing import DtypeArg
910

@@ -18,7 +19,7 @@ class CategoricalIndex(ExtensionIndex, accessor.PandasDelegate):
1819
dtype=...,
1920
copy: bool = ...,
2021
name=...,
21-
) -> CategoricalIndex: ...
22+
) -> Self: ...
2223
def equals(self, other): ...
2324
@property
2425
def inferred_type(self) -> str: ...

pandas-stubs/core/indexes/interval.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ _TimestampLike: TypeAlias = Union[pd.Timestamp, np.datetime64, dt.datetime]
7070
_TimedeltaLike: TypeAlias = Union[pd.Timedelta, np.timedelta64, dt.timedelta]
7171

7272
class IntervalIndex(IntervalMixin, Generic[IntervalT]):
73+
closed: IntervalClosedType
74+
7375
def __new__(
7476
cls,
7577
data: Sequence[IntervalT],

pandas-stubs/core/indexes/multi.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing import (
1111
import numpy as np
1212
import pandas as pd
1313
from pandas.core.indexes.base import Index
14+
from typing_extensions import Self
1415

1516
from pandas._typing import (
1617
T1,
@@ -32,7 +33,7 @@ class MultiIndex(Index):
3233
name=...,
3334
verify_integrity: bool = ...,
3435
_set_identity: bool = ...,
35-
) -> MultiIndex: ...
36+
) -> Self: ...
3637
def __init__(
3738
self,
3839
levels=...,

pandas-stubs/io/excel/_base.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from odf.opendocument import OpenDocument
1616
from openpyxl.workbook.workbook import Workbook
1717
from pandas.core.frame import DataFrame
1818
import pyxlsb.workbook
19+
from typing_extensions import Self
1920
from xlrd.book import Book
2021

2122
from pandas._typing import (
@@ -130,7 +131,7 @@ class ExcelWriter:
130131
@property
131132
def if_sheet_exists(self) -> Literal["error", "new", "replace", "overlay"]: ...
132133
def __fspath__(self) -> str: ...
133-
def __enter__(self) -> ExcelWriter: ...
134+
def __enter__(self) -> Self: ...
134135
def __exit__(
135136
self,
136137
exc_type: type[BaseException] | None,
@@ -214,7 +215,7 @@ class ExcelFile:
214215
@property
215216
def sheet_names(self) -> list[int | str]: ...
216217
def close(self) -> None: ...
217-
def __enter__(self) -> ExcelFile: ...
218+
def __enter__(self) -> Self: ...
218219
def __exit__(
219220
self,
220221
exc_type: type[BaseException] | None,

pandas-stubs/io/parsers/readers.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ from typing import (
1818
from pandas.core.frame import DataFrame
1919
from pandas.core.indexes.base import Index
2020
from pandas.core.series import Series
21+
from typing_extensions import Self
2122

2223
from pandas._typing import (
2324
CompressionOptions,
@@ -479,7 +480,7 @@ class TextFileReader(abc.Iterator):
479480
def read(self, nrows: int | None = ...) -> DataFrame: ...
480481
def get_chunk(self, size: int | None = ...) -> DataFrame: ...
481482
def __next__(self) -> DataFrame: ...
482-
def __enter__(self) -> TextFileReader: ...
483+
def __enter__(self) -> Self: ...
483484
def __exit__(
484485
self,
485486
exc_type: type[BaseException] | None,

pandas-stubs/io/pytables.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import (
22
Generator,
3+
Iterator,
34
Sequence,
45
)
56
from types import TracebackType
@@ -15,6 +16,7 @@ from pandas import (
1516
)
1617
from pandas.core.computation.pytables import PyTablesExpr
1718
from pandas.core.generic import NDFrame
19+
from typing_extensions import Self
1820

1921
from pandas._typing import (
2022
FilePath,
@@ -113,15 +115,15 @@ class HDFStore:
113115
def __getattr__(self, name: str) -> DataFrame | Series: ...
114116
def __contains__(self, key: str) -> bool: ...
115117
def __len__(self) -> int: ...
116-
def __enter__(self) -> HDFStore: ...
118+
def __enter__(self) -> Self: ...
117119
def __exit__(
118120
self,
119121
exc_type: type[BaseException] | None,
120122
exc_value: BaseException | None,
121123
traceback: TracebackType | None,
122124
) -> None: ...
123125
def keys(self) -> list[str]: ...
124-
def __iter__(self) -> list[str]: ...
126+
def __iter__(self) -> Iterator[str]: ...
125127
def open(self, mode: Literal["a", "w", "r", "r+"] = ..., **kwargs) -> None: ...
126128
def close(self) -> None: ...
127129
@property

pandas-stubs/io/sas/sasreader.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from typing import (
99
)
1010

1111
from pandas import DataFrame
12+
from typing_extensions import Self
1213

1314
from pandas._typing import (
1415
CompressionOptions as CompressionOptions,
@@ -24,7 +25,7 @@ class ReaderBase(metaclass=ABCMeta):
2425
def read(self, nrows: int | None = ...) -> DataFrame: ...
2526
@abstractmethod
2627
def close(self) -> None: ...
27-
def __enter__(self) -> ReaderBase: ...
28+
def __enter__(self) -> Self: ...
2829
def __exit__(self, exc_type, exc_value, traceback) -> None: ...
2930

3031
@overload

pandas-stubs/io/stata.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from typing import (
99
)
1010

1111
from pandas.core.frame import DataFrame
12+
from typing_extensions import Self
1213

1314
from pandas._typing import (
1415
CompressionOptions,
@@ -92,7 +93,7 @@ class StataReader(StataParser, abc.Iterator):
9293
compression: CompressionOptions = ...,
9394
storage_options: StorageOptions = ...,
9495
) -> None: ...
95-
def __enter__(self) -> StataReader: ...
96+
def __enter__(self) -> Self: ...
9697
def __exit__(
9798
self,
9899
exc_type: type[BaseException] | None,

tests/test_interval_index.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pandas as pd
44
from typing_extensions import assert_type
55

6+
from pandas._typing import IntervalClosedType
7+
68
from tests import check
79

810

@@ -34,3 +36,5 @@ def test_from_tuples() -> None:
3436
def test_is_overlapping() -> None:
3537
ind = pd.IntervalIndex.from_tuples([(0, 2), (1, 3), (4, 5)])
3638
check(assert_type(ind.is_overlapping, bool), bool)
39+
40+
check(assert_type(ind.closed, IntervalClosedType), str)

tests/test_io.py

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ def test_hdfstore():
329329
DataFrame,
330330
)
331331
check(assert_type(store.get("df"), Union[DataFrame, Series]), DataFrame)
332+
for key in store:
333+
check(assert_type(key, str), str)
332334
check(assert_type(store.close(), None), type(None))
333335

334336
store = HDFStore(path, model="r")

0 commit comments

Comments
 (0)