You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Objects matching SupportsKeysAndGetItem[str, Any] cannot be used after ** #14986
mypy generates an error at line 19: Argument after ** must be a mapping, not "MyThing"
Expected Behavior
No error should be generated since it works at runtime. This has been discussed over in the Python Type School, and pyrighthas been updated to support this.
Your Environment
Mypy version used: 1.1.1
Mypy command-line flags: n/a
Mypy configuration options from mypy.ini (and other config files): n/a
There's a good chance the core of that change is that simple. Will need some effort in unit tests as well if you do it that way, since I don't think we currently have a _typeshed stub in unit tests.
Both of those need to be changed from typing.Mapping to _typeshed.SupportsKeysAndGetItem. With the change to the line you noted as well as these extra changes, it seems that it fixes the issue (when I run tests by calling python -m mypy foo.py in my venv).
However, I'm unable to get the unit tests to pick up a _typeshed.pyi file in test-data/unit/lib-stub or in test-data/unit/fixtures (I had to modify mypy/test/data.py to attempt the latter, but it still didn't work). It's been a while since I've worked on mypy, so I may be missing something really simple. The changed calls always raise KeyError: '_typeshed' in the unit test runner.
Might need to do something like import _typeshed in the builtins.pyi fixture you're using so that it's always part of the mypy build. Also feel free to open a draft PR and I can help fix up whenever I get time. The test fixtures are the most annoying part of mypy dev
(edit: maybe best to do this in a custom builtins.pyi fixture)
@hauntsaninja I was able to fix some of the unit test issues by updating the Mapping and dict definitions to add keys() and __getitem__() where needed and adding import _typeshed to some fixture files, but I'm still running into problems. This may be better solved by coming up with a way to force mypy to evaluate _typeshed early (much like it does with builtins). I submitted a draft PR as you suggested.
Activity
hauntsaninja commentedon Mar 31, 2023
Should be
mypy/mypy/checkexpr.py
Line 2129 in c99133f
bryanforbes commentedon Mar 31, 2023
@hauntsaninja so that could simply be changed to
self.chk.named_type("_typeshed.SupportsKeysAndGetItem")?hauntsaninja commentedon Mar 31, 2023
There's a good chance the core of that change is that simple. Will need some effort in unit tests as well if you do it that way, since I don't think we currently have a
_typeshedstub in unit tests.bryanforbes commentedon Mar 31, 2023
@hauntsaninja Well, I gave it a shot. I figured out that there are a couple of other places that need to be changed:
mypy/mypy/checkexpr.py
Lines 4933 to 4950 in 89469ac
Both of those need to be changed from
typing.Mappingto_typeshed.SupportsKeysAndGetItem. With the change to the line you noted as well as these extra changes, it seems that it fixes the issue (when I run tests by callingpython -m mypy foo.pyin my venv).However, I'm unable to get the unit tests to pick up a
_typeshed.pyifile intest-data/unit/lib-stubor intest-data/unit/fixtures(I had to modifymypy/test/data.pyto attempt the latter, but it still didn't work). It's been a while since I've worked on mypy, so I may be missing something really simple. The changed calls always raiseKeyError: '_typeshed'in the unit test runner.hauntsaninja commentedon Mar 31, 2023
Might need to do something like
import _typeshedin thebuiltins.pyifixture you're using so that it's always part of the mypy build. Also feel free to open a draft PR and I can help fix up whenever I get time. The test fixtures are the most annoying part of mypy dev(edit: maybe best to do this in a custom builtins.pyi fixture)
bryanforbes commentedon Mar 31, 2023
@hauntsaninja I was able to fix some of the unit test issues by updating the
Mappinganddictdefinitions to addkeys()and__getitem__()where needed and addingimport _typeshedto some fixture files, but I'm still running into problems. This may be better solved by coming up with a way to forcemypyto evaluate_typeshedearly (much like it does withbuiltins). I submitted a draft PR as you suggested.Allow objects matching `SupportsKeysAndGetItem` to be unpacked (#14990)