Skip to content

Better error message for incompatible unpacked **kwargs#8960

Closed
rohitkg98 wants to merge 6 commits into
python:masterfrom
rohitkg98:unpacked-kwargs
Closed

Better error message for incompatible unpacked **kwargs#8960
rohitkg98 wants to merge 6 commits into
python:masterfrom
rohitkg98:unpacked-kwargs

Conversation

@rohitkg98
Copy link
Copy Markdown

Closes #8874
Added a note for unpacked **kwargs.
I believe there should be multiple error messages, to inform of all callable argument type mismatch.
Hence, I have added just a note.
Also, added in Common Issues.

you need to ensure the types of dictionary members by providing either
a TypedDict or a variable with type Dict[str, Any].

For example, the following function calls are valid:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion - maybe an example that shows some offending cases or invalid function calls would help drive the point home? (otherwise good job!)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will add them.

Comment thread mypy/messages.py
arg_type_str = '*' + arg_type_str
elif arg_kind == ARG_STAR2:
arg_type_str = '**' + arg_type_str
notes.append("Use TypedDict or Dict[str, Any] for unpacked **kwargs")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! You will also have to modify the tests affected by this change :) Cheers!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, its a work in progress

@rohitkg98
Copy link
Copy Markdown
Author

Any updates on this? Updated PR with respect to master

@gvanrossum
Copy link
Copy Markdown
Member

I'm not sure adding this note is actually helpful. The root problem seems to be that the current error is just confusing -- it says (for example) argument has incompatible type "**Dict[str, float]"; expected "int" where it should explain that the dict's value type is not compatible with all of the function's argument type, or something like that. It is easy to come up with examples where the note is just wrong, e.g.

def f(a: int): ...
f(**dict(a=3.14))

@rohitkg98
Copy link
Copy Markdown
Author

The note is needed in cases where the Dict is untyped and inferred type becomes **Dict[str, object]. In such cases, the dict needs to explicitly typed. e.g.

def f(a: int, b: str): ...
f(**dict(a=1, b="some_str"))

Even though types are correct, it fails.

In cases where inferred dict type is not **Dict[str, object], we can modify the error message to explain the dict's value type is not compatible with functions arguments. Would that work?

@gvanrossum
Copy link
Copy Markdown
Member

The note is needed in cases where the Dict is untyped and inferred type becomes **Dict[str, object]. In such cases, the dict needs to explicitly typed. e.g.

def f(a: int, b: str): ...
f(**dict(a=1, b="some_str"))

Even though types are correct, it fails.

In this case we should do what Jukka suggested in his first bullet in #8874: "Generate a single error message instead of multiple ones."

In cases where inferred dict type is not **Dict[str, object], we can modify the error message to explain the dict's value type is not compatible with functions arguments. Would that work?

I'm thinking there are several cases.

  • The key type is not compatible with str -- that should be a separate error message
  • The value type is not compatible with one or more argument types -- that should be another error message

In the example you give, maybe the error should be

error: first **kwargs argument to "f" has value type "object", incompatible with formal parameter 1 ("int") and 2 ("str")

(However, I don't know if all the information needed to format it that way is actually available.)

Copy link
Copy Markdown
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes as per gvanrossum's comment

@97littleleaf11
Copy link
Copy Markdown
Collaborator

Hi, any progress on this PR?

@JukkaL
Copy link
Copy Markdown
Collaborator

JukkaL commented Jun 15, 2022

Closing due to inactivity.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate better error message for incompatible **kwargs

7 participants