-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Allow literals as kwargs dict keys #20416
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
Conversation
|
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/generic.py:4771: error: Unused "type: ignore[misc]" comment [unused-ignore]
+ pandas/core/generic.py:4842: error: Unused "type: ignore[misc]" comment [unused-ignore]
+ pandas/core/generic.py:5610: error: Unused "type: ignore" comment [unused-ignore]
rotki (https://github.com/rotki/rotki)
+ rotkehlchen/exchanges/binance.py:355: error: Unused "type: ignore" comment [unused-ignore]
xarray (https://github.com/pydata/xarray)
- xarray/core/common.py:460: error: Keywords must be strings [misc]
+ xarray/core/common.py:460: error: Argument after ** must have string keys [arg-type]
- xarray/core/dataset.py:5204: error: Keywords must be strings [misc]
+ xarray/core/dataset.py:5204: error: Argument after ** must have string keys [arg-type]
|
JukkaL
left a comment
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.
Thanks!
| return ( | ||
| is_subtype( | ||
| ( | ||
| # This is a little ad hoc, ideally we would have a map_instance_to_supertype |
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.
Imo this whole approach is technically incorrect. SupportsKeysAndGetItem is invariant in key type, so prescribing str will cause errors if a dict with literal keys is given
| [case testLiteralKwargs] | ||
| from typing import Any, Literal | ||
| kw: dict[Literal["a", "b"], Any] | ||
| def func(a, b): ... | ||
| func(**kw) | ||
|
|
||
| badkw: dict[Literal["one", 1], Any] | ||
| func(**badkw) # E: Argument after ** must have string keys | ||
| [builtins fixtures/dict.pyi] |
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 modified test produces false-positive for good_kw:
# [case testLiteralKwargs]
from typing import Literal
def func(a: int, b: int) -> None: ...
class A: pass
good_kw: dict[Literal["a", "b"], int]
bad_kw: dict[Literal["one", 1], int]
func(**good_kw)
func(**bad_kw) # E: Argument after ** must have string keys
# [builtins fixtures/dict.pyi]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.
Hm, this PR does handle correctly handle that test case. The patch is ad hoc though, so e.g. it would fail if you changed it to Mapping
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.
You're right, I think tested with the wrong commit
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.
I did make the proposed change into a helper function: #20419
|
I've added a code that fixes the I could move this into a separate PR, and maybe it's worth making that procedure into a helper function? |
Fixes #10023 , fixes #13674 , closes #10237