Skip to content
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

No error when mutating ParamSpec **kwargs #16157

Closed
kmurphy4 opened this issue Sep 21, 2023 · 3 comments
Closed

No error when mutating ParamSpec **kwargs #16157

kmurphy4 opened this issue Sep 21, 2023 · 3 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@kmurphy4
Copy link

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.

@kmurphy4 kmurphy4 added the bug mypy got something wrong label Sep 21, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Sep 22, 2023

We could possibly change ParamSpec.kwargs objects so that their upper bound is Mapping[str, object] rather than dict[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 of ParamSpec.kwargs objects.

@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Sep 22, 2023
@kmurphy4
Copy link
Author

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 with Concatenate[..., 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 to Concatenate a kwarg...?).

In any case, I think I can work around this on my end. Thanks for the response!

@AlexWaygood
Copy link
Member

No worries! Not sure there's anything actionable here without changing the spec, so I'm closing this for now

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

No branches or pull requests

2 participants