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

Improve the "Argument must be a mapping" error message #12070

Closed
DevilXD opened this issue Jan 25, 2022 · 8 comments · Fixed by #12222
Closed

Improve the "Argument must be a mapping" error message #12070

DevilXD opened this issue Jan 25, 2022 · 8 comments · Fixed by #12222

Comments

@DevilXD
Copy link

DevilXD commented Jan 25, 2022

Feature

This could also be classified as a bug report, but it's also a feature request. Consider code like this:

from typing import Any, Optional

def test(**kwargs):
    ...

kwargs: Optional[Any]
test(**kwargs)

Now, this is obviously wrong on purpose, and in latest MyPy, results in this error message:

error: Argument after ** must be a mapping

The problem with this message is that, it doesn't say what exactly is wrong, unlike when you normally mismatch types where it says: Argument 1 to "test" has incompatible type "Union[int, str]"; expected "bool", for example. In my case, I had a variable with the same name inferring Optional[Any] higher up in the code, and tracking down the root cause took me good 10 minutes,

Pitch

Even though **kwargs usage like this is uncommon, having a similarly improved message would help with debugging why this may be happening. Something like: Argument after ** to "test" has incompatible type "Union[Any, None]", expected "Mapping[str, Any]" should help a lot.

@JelleZijlstra
Copy link
Member

Yes, we should improve this. PR welcome!

@himanshi-2602
Copy link

@JelleZijlstra
I would like to work on this issue.
I am a beginner but really interested in open source.

@JelleZijlstra
Copy link
Member

Go for it! I haven't looked at the code for this error, but here's what I would do:

  • Find the place where the must be a mapping error is raised. Start by grepping for this string, then keep grepping to look for the code where we actually perform the check.
  • Find out how to get additional information to make the error more informative. Look for examples where we already give a more informative error, e.g. when there's an argument type mismatch.
  • Update the code you found to give the more detailed error, and update the test suite.

@himanshi-2602
Copy link

@JelleZijlstra
Thanks for the response.

@purna135
Copy link
Contributor

Hi @JelleZijlstra, I am new to open source and really interested to contribute my favorite language i.e python, may I work on this issue or could you please suggest any other beginner-friendly issue to start with. Thanks!!

@JelleZijlstra
Copy link
Member

This is a pretty good one to start! Everyone reading this should feel free to go ahead, start coding, and submit a PR.

@purna135
Copy link
Contributor

Thanks @JelleZijlstra

@MihirHundiwala
Copy link

MihirHundiwala commented Feb 5, 2022

Hello, even i am a beginner to open source contribution, found this good-first-issue, so i have managed to find the file where we raise the issue 'Argument after ** must be a mapping' but i don't know what grepping is, how it is done, also how can i debug the process step by step, i know the python lib 'pdb' for debugging but how do i call it with mypy

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

Successfully merging a pull request may close this issue.

5 participants