Skip to content

Commit 0ddc312

Browse files
authored
Make annotation for .copy() more precise (#42)
* Make annotation for `.copy()` more precise https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html says the return value's `Object type matches caller.` Previously, this returned a `FrameOrSeries` which was less ergonomic. Without this fix, the newly added tests fail: ``` (venv3) aagrawal@aagrawal-mbp151 ~/src/pandas-stubs $ mypy --config-file mypy.ini third_party/3/pandas tests/snippets tests/snippets/test_series.py:40: error: Incompatible types in assignment (expression has type "Union[DataFrame, Series]", variable has type "Series") tests/snippets/test_frame.py:47: error: Incompatible types in assignment (expression has type "Union[DataFrame, Series]", variable has type "DataFrame") Found 2 errors in 2 files (checked 241 source files) ``` * Bump version
1 parent fc2a0de commit 0ddc312

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def list_packages(source_path: str = src_path) -> None:
2121
setup(
2222
name="pandas-stubs",
2323
package_dir={"": src_path},
24-
version="1.1.0.6",
24+
version="1.1.0.7",
2525
description="Type annotations for Pandas",
2626
long_description=(open("README.md").read()
2727
if os.path.exists("README.md") else ""),

tests/snippets/test_frame.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def test_types_to_csv_when_path_passed() -> None:
4242
path.unlink()
4343

4444

45+
def test_types_copy() -> None:
46+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
47+
df2: pd.DataFrame = df.copy()
48+
49+
4550
def test_types_getitem() -> None:
4651
df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4], 5: [6, 7]})
4752
i = pd.Index(['col1', 'col2'])

tests/snippets/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def test_types_csv() -> None:
3535
s4: pd.DataFrame = pd.read_csv(file.name)
3636

3737

38+
def test_types_copy() -> None:
39+
s = pd.Series(data=[1, 2, 3, 4])
40+
s2: pd.Series = s.copy()
41+
42+
3843
def test_types_select() -> None:
3944
s = pd.Series(data={'row1': 1, 'row2': 2})
4045
s[0]

third_party/3/pandas/core/generic.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from typing import Any, Callable, Dict, Hashable, List, Mapping, Optional, Sequence, Tuple, Union, AnyStr, overload
4+
from typing import Any, Callable, Dict, Hashable, List, Mapping, Optional, Sequence, Tuple, TypeVar, Union, AnyStr, overload
55

66
if sys.version_info >= (3, 8):
77
from typing import Literal
@@ -23,6 +23,8 @@ from pandas.core.internals import BlockManager
2323

2424
bool_t = bool
2525

26+
Self = TypeVar('Self', bound=NDFrame)
27+
2628
class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
2729
__array_priority__: int = ...
2830
__bool__: Any = ...
@@ -115,7 +117,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
115117
@property
116118
def dtypes(self) -> Any: ...
117119
def astype(self, dtype: Any, copy: bool_t=..., errors: str=...) -> FrameOrSeries: ...
118-
def copy(self, deep: bool_t=...) -> FrameOrSeries: ...
120+
def copy(self: Self, deep: bool_t=...) -> Self: ...
119121
def infer_objects(self) -> FrameOrSeries: ...
120122
def convert_dtypes(self, infer_objects: bool_t=..., convert_string: bool_t=..., convert_integer: bool_t=..., convert_boolean: bool_t=...) -> FrameOrSeries: ...
121123
@overload

0 commit comments

Comments
 (0)