Skip to content

Commit 3edd3b3

Browse files
GH1269 Add @Final decorator to match pandas code where possible (#1277)
* GH1269 Add @Final decorator to match pandas code where possible * GH1269 Add more changes
1 parent d88e0e4 commit 3edd3b3

File tree

15 files changed

+180
-10
lines changed

15 files changed

+180
-10
lines changed

pandas-stubs/core/arrays/sparse/array.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from enum import Enum
22
from typing import (
33
Any,
4+
final,
45
overload,
56
)
67

@@ -16,6 +17,7 @@ from pandas._typing import (
1617
SequenceIndexer,
1718
)
1819

20+
@final
1921
class ellipsis(Enum):
2022
Ellipsis = "..."
2123

pandas-stubs/core/frame.pyi

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ from typing import (
1919
Generic,
2020
Literal,
2121
NoReturn,
22+
final,
2223
overload,
2324
)
2425

@@ -705,6 +706,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
705706
def transpose(self, *args: Any, copy: _bool = ...) -> Self: ...
706707
@property
707708
def T(self) -> Self: ...
709+
@final
708710
def __getattr__(self, name: str) -> Series: ...
709711
def isetitem(
710712
self, loc: int | Sequence[int], value: Scalar | ArrayLike | list[Any]
@@ -795,6 +797,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
795797
allow_duplicates: _bool = ...,
796798
) -> None: ...
797799
def assign(self, **kwargs: IntoColumn) -> Self: ...
800+
@final
798801
def align(
799802
self,
800803
other: NDFrameT,
@@ -1700,6 +1703,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17001703
@property
17011704
def values(self) -> np.ndarray: ...
17021705
# methods
1706+
@final
17031707
def abs(self) -> Self: ...
17041708
def add(
17051709
self,
@@ -1708,7 +1712,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17081712
level: Level | None = ...,
17091713
fill_value: float | None = ...,
17101714
) -> Self: ...
1715+
@final
17111716
def add_prefix(self, prefix: _str, axis: Axis | None = None) -> Self: ...
1717+
@final
17121718
def add_suffix(self, suffix: _str, axis: Axis | None = None) -> Self: ...
17131719
@overload
17141720
def all(
@@ -1744,7 +1750,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17441750
skipna: _bool = ...,
17451751
**kwargs: Any,
17461752
) -> Series[_bool]: ...
1753+
@final
17471754
def asof(self, where, subset: _str | list[_str] | None = ...) -> Self: ...
1755+
@final
17481756
def asfreq(
17491757
self,
17501758
freq,
@@ -1753,18 +1761,21 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17531761
normalize: _bool = ...,
17541762
fill_value: Scalar | None = ...,
17551763
) -> Self: ...
1764+
@final
17561765
def astype(
17571766
self,
17581767
dtype: AstypeArg | Mapping[Any, Dtype] | Series,
17591768
copy: _bool = ...,
17601769
errors: IgnoreRaise = ...,
17611770
) -> Self: ...
1771+
@final
17621772
def at_time(
17631773
self,
17641774
time: _str | dt.time,
17651775
asof: _bool = ...,
17661776
axis: Axis | None = ...,
17671777
) -> Self: ...
1778+
@final
17681779
def between_time(
17691780
self,
17701781
start_time: _str | dt.time,
@@ -1859,6 +1870,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
18591870
inplace: Literal[True],
18601871
**kwargs: Any,
18611872
) -> None: ...
1873+
@final
18621874
def copy(self, deep: _bool = ...) -> Self: ...
18631875
def cummax(
18641876
self, axis: Axis | None = ..., skipna: _bool = ..., *args: Any, **kwargs: Any
@@ -1872,6 +1884,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
18721884
def cumsum(
18731885
self, axis: Axis | None = ..., skipna: _bool = ..., *args: Any, **kwargs: Any
18741886
) -> Self: ...
1887+
@final
18751888
def describe(
18761889
self,
18771890
percentiles: list[float] | None = ...,
@@ -1892,9 +1905,12 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
18921905
level: Level | None = ...,
18931906
fill_value: float | None = ...,
18941907
) -> Self: ...
1908+
@final
18951909
def droplevel(self, level: Level | list[Level], axis: Axis = ...) -> Self: ...
18961910
def eq(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
1911+
@final
18971912
def equals(self, other: Series | DataFrame) -> _bool: ...
1913+
@final
18981914
def ewm(
18991915
self,
19001916
com: float | None = ...,
@@ -1906,6 +1922,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
19061922
ignore_na: _bool = ...,
19071923
axis: Axis = ...,
19081924
) -> ExponentialMovingWindow[Self]: ...
1925+
@final
19091926
def expanding(
19101927
self,
19111928
min_periods: int = ...,
@@ -1937,7 +1954,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
19371954
regex: _str | None = ...,
19381955
axis: Axis | None = ...,
19391956
) -> Self: ...
1957+
@final
19401958
def first(self, offset) -> Self: ...
1959+
@final
19411960
def first_valid_index(self) -> Scalar: ...
19421961
def floordiv(
19431962
self,
@@ -1958,7 +1977,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
19581977
@overload
19591978
def get(self, key: list[Hashable], default: _T) -> Self | _T: ...
19601979
def gt(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
1980+
@final
19611981
def head(self, n: int = ...) -> Self: ...
1982+
@final
19621983
def infer_objects(self) -> Self: ...
19631984
# def info
19641985
@overload
@@ -2000,7 +2021,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
20002021
numeric_only: _bool = ...,
20012022
**kwargs: Any,
20022023
) -> Series: ...
2024+
@final
20032025
def last(self, offset) -> Self: ...
2026+
@final
20042027
def last_valid_index(self) -> Scalar: ...
20052028
def le(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
20062029
def lt(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
@@ -2088,6 +2111,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
20882111
fill_value: float | None = ...,
20892112
) -> Self: ...
20902113
def ne(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
2114+
@final
20912115
def pct_change(
20922116
self,
20932117
periods: int = ...,
@@ -2130,6 +2154,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
21302154
level: Level | None = ...,
21312155
fill_value: float | None = ...,
21322156
) -> Self: ...
2157+
@final
21332158
def rank(
21342159
self,
21352160
axis: Axis = ...,
@@ -2146,6 +2171,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
21462171
level: Level | None = ...,
21472172
fill_value: float | None = ...,
21482173
) -> Self: ...
2174+
@final
21492175
def reindex_like(
21502176
self,
21512177
other: DataFrame,
@@ -2264,7 +2290,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
22642290
level: Level | None = ...,
22652291
fill_value: float | None = ...,
22662292
) -> Self: ...
2267-
# sample is missing a weights arg
2293+
@final
22682294
def sample(
22692295
self,
22702296
n: int | None = ...,
@@ -2294,6 +2320,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
22942320
numeric_only: _bool = ...,
22952321
**kwargs: Any,
22962322
) -> Series: ...
2323+
@final
22972324
def squeeze(self, axis: Axis | None = ...) -> DataFrame | Series | Scalar: ...
22982325
def std(
22992326
self,
@@ -2327,7 +2354,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
23272354
min_count: int = ...,
23282355
**kwargs: Any,
23292356
) -> Series: ...
2357+
@final
23302358
def swapaxes(self, axis1: Axis, axis2: Axis, copy: _bool = ...) -> Self: ...
2359+
@final
23312360
def tail(self, n: int = ...) -> Self: ...
23322361
@overload
23332362
def to_json(
@@ -2441,6 +2470,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
24412470
max_colwidth: int | None = ...,
24422471
encoding: _str | None = ...,
24432472
) -> _str: ...
2473+
@final
24442474
def to_xarray(self) -> xr.Dataset: ...
24452475
def truediv(
24462476
self,
@@ -2449,20 +2479,23 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
24492479
level: Level | None = ...,
24502480
fill_value: float | None = ...,
24512481
) -> Self: ...
2482+
@final
24522483
def truncate(
24532484
self,
24542485
before: dt.date | _str | int | None = ...,
24552486
after: dt.date | _str | int | None = ...,
24562487
axis: Axis | None = ...,
24572488
copy: _bool = ...,
24582489
) -> Self: ...
2490+
@final
24592491
def tz_convert(
24602492
self,
24612493
tz: TimeZones,
24622494
axis: Axis = ...,
24632495
level: Level | None = ...,
24642496
copy: _bool = ...,
24652497
) -> Self: ...
2498+
@final
24662499
def tz_localize(
24672500
self,
24682501
tz: TimeZones,
@@ -2514,7 +2547,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
25142547
level: Level | None = ...,
25152548
) -> Self: ...
25162549
# Move from generic because Series is Generic and it returns Series[bool] there
2550+
@final
25172551
def __invert__(self) -> Self: ...
2552+
@final
25182553
def xs(
25192554
self,
25202555
key: Hashable,
@@ -2531,6 +2566,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
25312566
) -> Self: ...
25322567
def __truediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
25332568
def __rtruediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
2569+
@final
25342570
def __bool__(self) -> NoReturn: ...
25352571

25362572
class _PandasNamedTuple(tuple[Any, ...]):

pandas-stubs/core/generic.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ from pandas.io.sql import SQLTable
6767
class NDFrame(indexing.IndexingMixin):
6868
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
6969

70+
@final
7071
def set_flags(
7172
self,
7273
*,
@@ -84,18 +85,24 @@ class NDFrame(indexing.IndexingMixin):
8485
@property
8586
def size(self) -> int: ...
8687
def equals(self, other: Series) -> _bool: ...
88+
@final
8789
def __neg__(self) -> Self: ...
90+
@final
8891
def __pos__(self) -> Self: ...
92+
@final
8993
def __nonzero__(self) -> None: ...
9094
@final
9195
def bool(self) -> _bool: ...
9296
def __abs__(self) -> Self: ...
97+
@final
9398
def __round__(self, decimals: int = ...) -> Self: ...
99+
@final
94100
def __contains__(self, key) -> _bool: ...
95101
@property
96102
def empty(self) -> _bool: ...
97103
__array_priority__: int = ...
98104
def __array__(self, dtype=...) -> np.ndarray: ...
105+
@final
99106
def to_excel(
100107
self,
101108
excel_writer,
@@ -113,6 +120,7 @@ class NDFrame(indexing.IndexingMixin):
113120
inf_rep: _str = ...,
114121
freeze_panes: tuple[int, int] | None = ...,
115122
) -> None: ...
123+
@final
116124
def to_hdf(
117125
self,
118126
path_or_buf: FilePath | HDFStore,
@@ -149,6 +157,7 @@ class NDFrame(indexing.IndexingMixin):
149157
storage_options: StorageOptions = ...,
150158
**kwargs: Any,
151159
) -> _str: ...
160+
@final
152161
def to_sql(
153162
self,
154163
name: _str,
@@ -168,13 +177,15 @@ class NDFrame(indexing.IndexingMixin):
168177
| None
169178
) = ...,
170179
) -> int | None: ...
180+
@final
171181
def to_pickle(
172182
self,
173183
path: FilePath | WriteBuffer[bytes],
174184
compression: CompressionOptions = ...,
175185
protocol: int = ...,
176186
storage_options: StorageOptions = ...,
177187
) -> None: ...
188+
@final
178189
def to_clipboard(
179190
self,
180191
excel: _bool = ...,
@@ -300,6 +311,7 @@ class NDFrame(indexing.IndexingMixin):
300311
errors: OpenFileErrors = ...,
301312
storage_options: StorageOptions = ...,
302313
) -> _str: ...
314+
@final
303315
def __delitem__(self, idx: Hashable) -> None: ...
304316
@overload
305317
def drop(
@@ -387,10 +399,15 @@ class NDFrame(indexing.IndexingMixin):
387399
*args: Any,
388400
**kwargs: Any,
389401
) -> T: ...
402+
@final
390403
def __finalize__(self, other, method=..., **kwargs) -> Self: ...
404+
@final
391405
def __setattr__(self, name: _str, value) -> None: ...
406+
@final
392407
def __copy__(self, deep: _bool = ...) -> Self: ...
408+
@final
393409
def __deepcopy__(self, memo=...) -> Self: ...
410+
@final
394411
def convert_dtypes(
395412
self,
396413
infer_objects: _bool = ...,

0 commit comments

Comments
 (0)