Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
9 changes: 9 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5436,6 +5436,15 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
expected_types.append(
self.chk.named_generic_type("_typeshed.SupportsKeysAndGetItem", [kt, vt])
)
# If this DictExpr came from a dict() call translation, validate that
# any unpacked dict has string keys (keywords must be strings)
if e.from_dict_call:
value_type = self.accept(value)
if not self.is_valid_keyword_var_arg(value_type):
is_mapping = is_subtype(
value_type, self.chk.named_type("_typeshed.SupportsKeysAndGetItem")
)
self.msg.invalid_keyword_var_arg(value_type, is_mapping, value)
else:
tup = TupleExpr([key, value])
if key.line >= 0:
Expand Down
4 changes: 3 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2672,15 +2672,17 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
class DictExpr(Expression):
"""Dictionary literal expression {key: value, ...}."""

__slots__ = ("items",)
__slots__ = ("items", "from_dict_call")

__match_args__ = ("items",)

items: list[tuple[Expression | None, Expression]]
from_dict_call: bool # True if this came from a dict(...) call translation

def __init__(self, items: list[tuple[Expression | None, Expression]]) -> None:
super().__init__()
self.items = items
self.from_dict_call = False

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_dict_expr(self)
Expand Down
13 changes: 13 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6051,13 +6051,26 @@ def translate_dict_call(self, call: CallExpr) -> DictExpr | None:
for a in call.args:
a.accept(self)
return None
# Check if any **kwargs argument is a dict literal with non-string keys.
# In that case, don't translate so that normal function call type checking
# will catch the "keywords must be strings" error.
for kind, arg in zip(call.arg_kinds, call.args):
if kind == ARG_STAR2 and isinstance(arg, DictExpr):
# Check if all keys in the dict literal are strings (not bytes!)
for key, _ in arg.items:
if key is not None and not isinstance(key, StrExpr):
# Non-string key found, don't translate
for a in call.args:
a.accept(self)
return None
Comment thread
Vikash-Kumar-23 marked this conversation as resolved.
Outdated
expr = DictExpr(
[
(StrExpr(key) if key is not None else None, value)
for key, value in zip(call.arg_names, call.args)
]
)
expr.set_line(call)
expr.from_dict_call = True
expr.accept(self)
return expr

Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2582,3 +2582,11 @@ def last_known_value() -> None:
x, y, z = xy # E: Unpacking a string is disallowed
reveal_type(z) # N: Revealed type is "builtins.str"
[builtins fixtures/primitives.pyi]


[case testDictUnpackNonStringKey]
def f() -> None:
dict(**{10: 20}) # E: Argument after ** must have string keys
dict(**{**{1: 1}}) # E: Argument after ** must have string keys
dict(**{b'a': 1}) # E: Argument after ** must have string keys
[builtins fixtures/dict.pyi]