Not planned
Description
Bug Report
Even though it fails at runtime, mypy
doesn't complain if I mutate ParamSpec.kwargs
.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=f5cea6637a9ea13c48b8ba75aa8d73dd
If I run this same code in an interpreter, I get
Traceback (most recent call last):
File "/tmp/repro.py", line 16, in <module>
print(bar(foo))
File "/tmp/repro.py", line 13, in bar
return f(*args, **kwargs)
TypeError: foo() got an unexpected keyword argument 'ka'
Expected Behavior
I would expect this to trigger an error on line 12.
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
AlexWaygood commentedon Sep 22, 2023
We could possibly change
ParamSpec.kwargs
objects so that their upper bound isMapping[str, object]
rather thandict[str, object]
. However, note that prior to #12668, previously we received complaints that people couldn't mutate ParamSpec kwargs: #12386. I don't know that there's a foolproof way of catching all unsafe code here without emitting false positives on "safe" transformations ofParamSpec.kwargs
objects.kmurphy4 commentedon Sep 22, 2023
Yeah that makes sense -- the
::pop
use-case is pretty idiomatic.It would be nice if an operation like
kwargs["ka"]
were able to return something withConcatenate[..., P]
, but that seems tricky as well: we'd have to return a new object rather than mutate in-place (plus, I'm not sure it's even possible toConcatenate
a kwarg...?).In any case, I think I can work around this on my end. Thanks for the response!
AlexWaygood commentedon Sep 23, 2023
No worries! Not sure there's anything actionable here without changing the spec, so I'm closing this for now