Skip to content

Allow subtypes of List[Scalar] by introducing ScalarArg #124

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 4 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Scalar = Union[
Timestamp,
Timedelta,
]
ScalarArg = TypeVar("ScalarArg", bound=Scalar)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe ScalarT to emphasize that it is a TypeVar.

Even though _typing.py is private, it might be good to make all variables that are not actually in pandas explicitly private. Users might want to import some of the type variables from pandas to annotate their own code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe ScalarT to emphasize that it is a TypeVar.

Good idea. Done in next commit.

Even though _typing.py is private, it might be good to make all variables that are not actually in pandas explicitly private. Users might want to import some of the type variables from pandas to annotate their own code.

That could be something we do in the future. One thing I would like to do is reconcile the things in pandas._typing.py and pandas-stubs._typing.pyi, because there are some inconsistencies. Created #128

# Refine the definitions below in 3.9 to use the specialized type.
np_ndarray_int8 = npt.NDArray[np.int8]
np_ndarray_int16 = npt.NDArray[np.int16]
Expand Down
3 changes: 2 additions & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ from pandas._typing import (
MaskType,
Renamer,
Scalar,
ScalarArg,
SeriesAxisType,
StrLike,
T as TType,
Expand Down Expand Up @@ -368,7 +369,7 @@ class DataFrame(NDFrame, OpsMixin):
Series[_bool],
DataFrame,
List[_str],
List[Scalar],
List[ScalarArg],
Index,
np_ndarray_str,
np_ndarray_bool,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,3 +1169,11 @@ def test_func(h: Hashable):
test_func(pd.DataFrame()) # type: ignore[arg-type]
test_func(pd.Series([], dtype=object)) # type: ignore[arg-type]
test_func(pd.Index([])) # type: ignore[arg-type]


def test_columns_mixlist() -> None:
# GH 97
df = pd.DataFrame({"a": [1, 2, 3], 1: [3, 4, 5]})
key: List[Union[int, str]]
key = [1]
check(assert_type(df[key], pd.DataFrame), pd.DataFrame)