Closed jonmooser closed 9 months ago
I can duplicate this with many cases. No errors in my code before I updated VSCode today, now all kinds of this popping up:
Argument of type "Literal['data']" cannot be assigned to parameter "__s" of type "slice" in function "__getitem__"
"Literal['data']" is incompatible with "slice"Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
I'm having a similar bug, where I try to obtain an indexed item in a list:
test = [1,2,3]
test[0]
Argument of type "Literal[0]" cannot be assigned to parameter "s" of type "slice" in function "getitem__" "Literal[0]" is incompatible with "slice"
@jonmooser, this is a bug in the pandas type stubs. Please file an issue at https://github.com/pandas-dev/pandas-stubs/issues.
Currently the _LocIndexerFrame.__getitem__
overloads that accept _IndexSliceTuple
objects, only accept them as the first element of the tuple.
Changing the second overload from:
@overload
def __getitem__(
self,
idx: (
IndexType
| MaskType
| Callable[[DataFrame], IndexType | MaskType | list[HashableT]]
| list[HashableT]
| tuple[
IndexType
| MaskType
| list[HashableT]
| slice
| _IndexSliceTuple
| Callable,
MaskType | list[HashableT] | slice | Callable,
]
),
) -> DataFrame: ...
to
@overload
def __getitem__(
self,
idx: (
IndexType
| MaskType
| Callable[[DataFrame], IndexType | MaskType | list[HashableT]]
| list[HashableT]
| tuple[
IndexType
| MaskType
| list[HashableT]
| slice
| _IndexSliceTuple
| Callable,
MaskType | list[HashableT] | slice | Callable | _IndexSliceTuple, # Note _IndexSliceTuple added here.
]
),
) -> DataFrame: ...
might be the right solution. It does eliminate this error. But the folks at pandas-stubs would know for sure.
I'm not sure if this is an issue with Pylance or Pandas, but I thought I'd start here. Some seemingly valid Pandas code is reported as a type error. But the syntax is exactly as shown in the pandas docs: (See https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#cross-section)
Environment data
Code Snippet
Repro Steps
Open the code above in a new file in VSCode
Expected behavior
Pylance should not find any errors
Actual behavior
Pylance reports a type error:
To be sure, the output is as expected:
Logs