Skip to content

No error when mutating ParamSpec **kwargs #16157

Not planned
@kmurphy4

Description

@kmurphy4

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.

Activity

AlexWaygood

AlexWaygood commented on Sep 22, 2023

@AlexWaygood
Member

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.

kmurphy4

kmurphy4 commented on Sep 22, 2023

@kmurphy4
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

AlexWaygood commented on Sep 23, 2023

@AlexWaygood
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-paramspecPEP 612, ParamSpec, Concatenate

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kmurphy4@AlexWaygood

        Issue actions

          No error when mutating ParamSpec `**kwargs` · Issue #16157 · python/mypy