Skip to content

BUG: Use HashableT rather than Hashable to improve compat #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ from pandas._typing import (
FilePathOrBuffer,
FilePathOrBytesBuffer,
GroupByObjectNonScalar,
HashableT,
IgnoreRaise,
IndexingInt,
IndexLabel,
Expand Down Expand Up @@ -587,7 +588,7 @@ class DataFrame(NDFrame, OpsMixin):
def set_index(
self,
keys: Union[
Label, Series, Index, np.ndarray, Iterator[Hashable], List[Hashable]
Label, Series, Index, np.ndarray, Iterator[HashableT], List[HashableT]
],
drop: _bool = ...,
append: _bool = ...,
Expand All @@ -599,7 +600,7 @@ class DataFrame(NDFrame, OpsMixin):
def set_index(
self,
keys: Union[
Label, Series, Index, np.ndarray, Iterator[Hashable], List[Hashable]
Label, Series, Index, np.ndarray, Iterator[HashableT], List[HashableT]
],
drop: _bool = ...,
append: _bool = ...,
Expand All @@ -611,7 +612,7 @@ class DataFrame(NDFrame, OpsMixin):
def set_index(
self,
keys: Union[
Label, Series, Index, np.ndarray, Iterator[Hashable], List[Hashable]
Label, Series, Index, np.ndarray, Iterator[HashableT], List[HashableT]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Iterator wouldn't have needed the TypeVar:

from typing import Iterator, Hashable

def test(x: Iterator[Hashable]):
    ...

def iter() -> Iterator[str]:
    ...

test(iter())

],
drop: _bool = ...,
append: _bool = ...,
Expand All @@ -622,7 +623,7 @@ class DataFrame(NDFrame, OpsMixin):
def set_index(
self,
keys: Union[
Label, Series, Index, np.ndarray, Iterator[Hashable], List[Hashable]
Label, Series, Index, np.ndarray, Iterator[HashableT], List[HashableT]
],
drop: _bool = ...,
append: _bool = ...,
Expand Down
42 changes: 42 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# flake8: noqa: F841
from __future__ import annotations

import datetime
import io
from pathlib import Path
Expand Down Expand Up @@ -1305,3 +1307,43 @@ def test_groupby_result() -> None:

for kk, g in df.groupby("a"):
pass


def test_setitem_list():
# GH 153
lst1: list[str] = ["a", "b", "c"]
lst2: list[int] = [1, 2, 3]
lst3: list[float] = [4.0, 5.0, 6.0]
lst4: list[tuple[str, int]] = [("a", 1), ("b", 2), ("c", 3)]
lst5: list[complex] = [0 + 1j, 0 + 2j, 0 + 3j]

columns: list[Hashable] = [
"a",
"b",
"c",
1,
2,
3,
4.0,
5.0,
6.0,
("a", 1),
("b", 2),
("c", 3),
0 + 1j,
0 + 2j,
0 + 3j,
]

df = pd.DataFrame(np.empty((3, 15)), columns=columns)

check(assert_type(df.set_index(lst1), pd.DataFrame), pd.DataFrame)
check(assert_type(df.set_index(lst2), pd.DataFrame), pd.DataFrame)
check(assert_type(df.set_index(lst3), pd.DataFrame), pd.DataFrame)
check(assert_type(df.set_index(lst4), pd.DataFrame), pd.DataFrame)
check(assert_type(df.set_index(lst5), pd.DataFrame), pd.DataFrame)

iter1: Iterator[str] = (v for v in lst1)
iter2: Iterator[tuple[str, int]] = (v for v in lst4)
check(assert_type(df.set_index(iter1), pd.DataFrame), pd.DataFrame)
check(assert_type(df.set_index(iter2), pd.DataFrame), pd.DataFrame)