Skip to content

__setitem__ should accept a dictionary #1203

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

Closed
bersbersbers opened this issue Apr 26, 2025 · 4 comments · Fixed by #1204
Closed

__setitem__ should accept a dictionary #1203

bersbersbers opened this issue Apr 26, 2025 · 4 comments · Fixed by #1204
Labels
Blocked Requires external dependency before progressing mypy bug Requires mypy to fix a bug

Comments

@bersbersbers
Copy link

Describe the bug
__setitem__ does not accept a dictionary

To Reproduce

import pandas as pd

df = pd.DataFrame(columns=["X"])
df.loc[0] = {"X": 0}
print(df)
>mypy --version
mypy 1.15.0 (compiled: yes)

>mypy bug.py
bug.py:4: error: No overload variant of "__setitem__" of "_LocIndexerFrame" matches argument types "int", "dict[str, int]"  [call-overload]
bug.py:4: note: Possible overload variants:
bug.py:4: note:     def [ScalarT: str | bytes | date | datetime | timedelta | <7 more items> | complex] __setitem__(self, Series[builtins.bool] | ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | str_ | <9 more items>, str | bytes | date | datetime | timedelta | <15 more items> | None, /) -> None
bug.py:4: note:     def __setitem__(self, tuple[tuple[Index[Any] | Series[builtins.bool] | ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | list[Any] | slice[Any, Any, Any] | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex, ...], ...], Hashable], str | bytes | date | datetime | timedelta | <14 more items> | None, /) -> None
Found 1 error in 1 file (checked 1 source file)
>pyright --version
pyright 1.1.400

>pyright bug.py
bug.py
  bug.py:4:1 - error: No overloads for "__setitem__" match the provided arguments (reportCallIssue)
  bug.py:4:1 - error: Argument of type "dict[str, int]" cannot be assigned to parameter "value" of type "Scalar | ArrayLike | NAType | NaTType | Series[Unknown] | DataFrame | list[Unknown] | None" in function "__setitem__"
    Type "dict[str, int]" is not assignable to type "Scalar | ArrayLike | NAType | NaTType | Series[Unknown] | DataFrame | list[Unknown] | None"
      "dict[str, int]" is not assignable to "str"
      "dict[str, int]" is not assignable to "bytes"
      "dict[str, int]" is not assignable to "date"
      "dict[str, int]" is not assignable to "datetime"
      "dict[str, int]" is not assignable to "timedelta"
      "dict[str, int]" is not assignable to "datetime64[date | int | None]"
      "dict[str, int]" is not assignable to "timedelta64[timedelta | int | None]" (reportArgumentType)
2 errors, 0 warnings, 0 informations
>python --version
Python 3.13.2
>uv pip freeze | findstr "pandas"
pandas==2.2.3
pandas-stubs==2.2.3.250308
@loicdiridollou
Copy link
Member

Hey @bersbersbers,
This is indeed a known mypy issue (see #73) where the type of the setter is different than the type of the getter.
A fix should come with mypy 1.16 (at least for the issue aforementioned). I will to see if that also applies to the issue you are reporting but would assume so.

@loicdiridollou loicdiridollou added Blocked Requires external dependency before progressing mypy bug Requires mypy to fix a bug labels Apr 27, 2025
@bersbersbers
Copy link
Author

Thanks, and sure, let's wait for the next mypy. (I can reproduce this in pyright, too, though.)

@loicdiridollou
Copy link
Member

Yes indeed pyright is also raising the issue, I looked through the doc and this does not seem to be very well documented.
Maybe @Dr-Irv has more insight on this behavior we should support (maybe it will be fine with pyright but still cause issues with mypy until the new version).

@loicdiridollou
Copy link
Member

loicdiridollou commented Apr 27, 2025

Actually I misclassified the issue, it has nothing to do with the setter, this is just a missing stubs on the code side.
The behavior is properly supported in the code (meaning explicitly, it is not a "it happened to work magically at runtime"):

https://github.com/pandas-dev/pandas/blob/44c56135688e84de154625d09a199276c141ef3a/pandas/core/indexing.py#L1810-L1817

I will draft a PR to make sure it is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked Requires external dependency before progressing mypy bug Requires mypy to fix a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants