From 26bfc631c1cd25a874cce21c0b08df5e7d939edb Mon Sep 17 00:00:00 2001 From: Purna Chandra Mansingh <42216008+purna135@users.noreply.github.com> Date: Wed, 2 Mar 2022 07:42:56 +0530 Subject: [PATCH] Improve the "Argument must be a mapping" error message (#12222) Fixes #12070 --- mypy/messages.py | 5 +---- test-data/unit/check-kwargs.test | 7 +++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 96cb2f3ae371..bb6977190105 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -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: diff --git a/test-data/unit/check-kwargs.test b/test-data/unit/check-kwargs.test index 7f0b8af3ba0e..9f8de1265ee7 100644 --- a/test-data/unit/check-kwargs.test +++ b/test-data/unit/check-kwargs.test @@ -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] +d = {} # type: Dict[A, A] 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]