-
Notifications
You must be signed in to change notification settings - Fork 205
Fix: Allow list[str] for pandas DataFrame columns and index parameters #1664
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
Open
Jack-GitHub12
wants to merge
3
commits into
facebook:main
Choose a base branch
from
Jack-GitHub12:1657
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| use crate::test::util::TestEnv; | ||
| use crate::testcase; | ||
|
|
||
| testcase!( | ||
| test_dataframe_list_str_columns, | ||
| { | ||
| let mut env = TestEnv::new(); | ||
| // Add corrected pandas stubs inline | ||
| env.add( | ||
| "pandas._typing", | ||
| r#" | ||
| from typing import Any, Iterator, Protocol, Sequence, TypeVar, overload | ||
| from typing_extensions import SupportsIndex | ||
| _T_co = TypeVar("_T_co", covariant=True) | ||
|
|
||
| class SequenceNotStr(Protocol[_T_co]): | ||
| @overload | ||
| def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... | ||
| @overload | ||
| def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... | ||
| def __contains__(self, value: object, /) -> bool: ... | ||
| def __len__(self) -> int: ... | ||
| def __iter__(self) -> Iterator[_T_co]: ... | ||
| # FIXED: All parameters position-only to match list.index | ||
| def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ... | ||
| def count(self, value: Any, /) -> int: ... | ||
| def __reversed__(self) -> Iterator[_T_co]: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas.core.frame", | ||
| r#" | ||
| from typing import Any | ||
| from pandas._typing import SequenceNotStr | ||
| Axes = SequenceNotStr[Any] | range | ||
|
|
||
| class DataFrame: | ||
| def __init__( | ||
| self, | ||
| data: Any = None, | ||
| index: Axes | None = None, | ||
| columns: Axes | None = None, | ||
| dtype: Any = None, | ||
| copy: bool | None = None, | ||
| ) -> None: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas", | ||
| r#" | ||
| from pandas.core.frame import DataFrame as DataFrame | ||
| "#, | ||
| ); | ||
| env | ||
| }, | ||
| r#" | ||
| import pandas as pd | ||
|
|
||
| # This should work: passing list[str] for columns | ||
| df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["A", "B", "C"]) | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_dataframe_list_str_both, | ||
| { | ||
| let mut env = TestEnv::new(); | ||
| env.add( | ||
| "pandas._typing", | ||
| r#" | ||
| from typing import Any, Iterator, Protocol, Sequence, TypeVar, overload | ||
| from typing_extensions import SupportsIndex | ||
| _T_co = TypeVar("_T_co", covariant=True) | ||
|
|
||
| class SequenceNotStr(Protocol[_T_co]): | ||
| @overload | ||
| def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... | ||
| @overload | ||
| def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... | ||
| def __contains__(self, value: object, /) -> bool: ... | ||
| def __len__(self) -> int: ... | ||
| def __iter__(self) -> Iterator[_T_co]: ... | ||
| # FIXED: All parameters position-only to match list.index | ||
| def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ... | ||
| def count(self, value: Any, /) -> int: ... | ||
| def __reversed__(self) -> Iterator[_T_co]: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas.core.frame", | ||
| r#" | ||
| from typing import Any | ||
| from pandas._typing import SequenceNotStr | ||
| Axes = SequenceNotStr[Any] | range | ||
|
|
||
| class DataFrame: | ||
| def __init__( | ||
| self, | ||
| data: Any = None, | ||
| index: Axes | None = None, | ||
| columns: Axes | None = None, | ||
| dtype: Any = None, | ||
| copy: bool | None = None, | ||
| ) -> None: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas", | ||
| "from pandas.core.frame import DataFrame as DataFrame", | ||
| ); | ||
| env | ||
| }, | ||
| r#" | ||
| import pandas as pd | ||
|
|
||
| # Test list[str] for both columns and index | ||
| df = pd.DataFrame( | ||
| [[1, 2, 3], [4, 5, 6]], | ||
| columns=["A", "B", "C"], | ||
| index=["row1", "row2"] | ||
| ) | ||
| "#, | ||
| ); | ||
|
|
||
| // Test with BROKEN pandas 2.x stubs (without position-only markers) | ||
| // This demonstrates the edge case fix in is_subset_param_list works | ||
| testcase!( | ||
| test_dataframe_with_broken_stubs, | ||
| { | ||
| let mut env = TestEnv::new(); | ||
| // Use pandas 2.x stubs WITHOUT position-only markers (the actual broken stubs) | ||
| env.add( | ||
| "pandas._typing", | ||
| r#" | ||
| from typing import Any, Iterator, Protocol, Sequence, TypeVar, overload | ||
| from typing_extensions import SupportsIndex | ||
| _T_co = TypeVar("_T_co", covariant=True) | ||
|
|
||
| class SequenceNotStr(Protocol[_T_co]): | ||
| @overload | ||
| def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... | ||
| @overload | ||
| def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... | ||
| def __contains__(self, value: object, /) -> bool: ... | ||
| def __len__(self) -> int: ... | ||
| def __iter__(self) -> Iterator[_T_co]: ... | ||
| # BROKEN: Missing position-only markers (actual pandas 2.x stubs) | ||
| # This should still work thanks to the edge case in is_subset_param_list | ||
| def index(self, value: Any, start: int = ..., stop: int = ...) -> int: ... | ||
| def count(self, value: Any, /) -> int: ... | ||
| def __reversed__(self) -> Iterator[_T_co]: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas.core.frame", | ||
| r#" | ||
| from typing import Any | ||
| from pandas._typing import SequenceNotStr | ||
| Axes = SequenceNotStr[Any] | range | ||
|
|
||
| class DataFrame: | ||
| def __init__( | ||
| self, | ||
| data: Any = None, | ||
| index: Axes | None = None, | ||
| columns: Axes | None = None, | ||
| dtype: Any = None, | ||
| copy: bool | None = None, | ||
| ) -> None: ... | ||
| "#, | ||
| ); | ||
| env.add( | ||
| "pandas", | ||
| r#" | ||
| from pandas.core.frame import DataFrame as DataFrame | ||
| "#, | ||
| ); | ||
| env | ||
| }, | ||
| r#" | ||
| import pandas as pd | ||
|
|
||
| # This should work even with broken stubs: list[str] should match SequenceNotStr[Any] | ||
| # because list.index() has position-only params, and our edge case allows PosOnly | ||
| # to match Pos in protocol checking | ||
| df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["A", "B", "C"]) | ||
| "#, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #![cfg(test)] | ||
| mod dataframe; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems backwards to me. The implementation should be less restrictive, so that it can be used everywhere the protocol can be used.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jack-GitHub12 what I had in mind if we wanted a hard-coding based fix is that we could just say
SequenceNotStr <: list[str], so that we never even get to the level of protocol callable checks where this branch gets hitIf we do it that way, the hack is specific to Pandas
SequenceNotStrinstead of affecting arbitrary protocol checks