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 #12222

Merged
merged 5 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,8 @@ def invalid_keyword_var_arg(self, typ: Type, is_mapping: bool, context: Context)
if isinstance(typ, Instance) and is_mapping:
self.fail('Keywords must be strings', context)
else:
suffix = ''
if isinstance(typ, Instance):
suffix = ', not {}'.format(format_type(typ))
self.fail(
'Argument after ** must be a mapping{}'.format(suffix),
'Argument after ** must be a mapping, not {}'.format(format_type(typ)),
context, code=codes.ARG_TYPE)

def undefined_in_superclass(self, member: str, context: Context) -> None:
Expand Down
5 changes: 4 additions & 1 deletion test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,15 @@ class A: pass
[builtins fixtures/dict.pyi]

[case testInvalidTypeForKeywordVarArg]
from typing import Dict
# flags: --strict-optional
from typing import Dict, Any, Optional
def f(**kwargs: 'A') -> None: pass
d = None # type: Dict[A, A]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
d = None # type: Dict[A, A]
d = {} # type: Dict[A, A]

Does this work?

f(**d) # E: Keywords must be strings
f(**A()) # E: Argument after ** must be a mapping, not "A"
class A: pass
kwargs: Optional[Any]
f(**kwargs) # E: Argument after ** must be a mapping, not "Optional[Any]"
[builtins fixtures/dict.pyi]

[case testPassingKeywordVarArgsToNonVarArgsFunction]
Expand Down