Skip to content

Check kwargs key type as covariant #10223

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 9 additions & 14 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
TypeAliasExpr, BackquoteExpr, EnumCallExpr, TypeAlias, SymbolNode, PlaceholderNode,
ParamSpecExpr,
ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, COVARIANT, LITERAL_TYPE, REVEAL_TYPE,
)
from mypy.literals import literal
from mypy import nodes
Expand Down Expand Up @@ -3938,20 +3938,15 @@ def is_valid_var_arg(self, typ: Type) -> bool:
def is_valid_keyword_var_arg(self, typ: Type) -> bool:
"""Is a type valid as a **kwargs argument?"""
if self.chk.options.python_version[0] >= 3:
return is_subtype(typ, self.chk.named_generic_type(
'typing.Mapping', [self.named_type('builtins.str'),
AnyType(TypeOfAny.special_form)]))
key_type = self.named_type('builtins.str')
else:
return (
is_subtype(typ, self.chk.named_generic_type(
'typing.Mapping',
[self.named_type('builtins.str'),
AnyType(TypeOfAny.special_form)]))
or
is_subtype(typ, self.chk.named_generic_type(
'typing.Mapping',
[self.named_type('builtins.unicode'),
AnyType(TypeOfAny.special_form)])))
key_type = UnionType.make_union([
self.named_type('builtins.str'),
self.named_type('builtins.unicode')])
kwargs_type = self.chk.named_generic_type(
'typing.Mapping', [key_type, AnyType(TypeOfAny.special_form)])
kwargs_type.type.defn.type_vars[0].variance = COVARIANT
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this affect all uses of Mapping after this code runs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, you're right. Didn't realized that before.

Choose a reason for hiding this comment

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

The fix I was thinking was something along these lines.

Suggested change
kwargs_type.type.defn.type_vars[0].variance = COVARIANT
is_subtype(typ, self.chk.named_type('typing.Mapping')) and isinstance(typ.args[0], LiteralType)

This wouldn't cover the case of Literal["a", "b"] but you can try using the try_getting_str_literals_from_type method

return is_subtype(typ, kwargs_type)

def has_member(self, typ: Type, member: str) -> bool:
"""Does type have member with the given name?"""
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ f(**b)
class A: pass
[builtins fixtures/dict.pyi]

[case testPassingStringLiteralForKeywordVarArg]
from typing import Any, Dict
from typing_extensions import Literal
kw: Dict[Literal["a", "b"], Any]
def f(a, b): pass
f(**kw)
[builtins fixtures/dict.pyi]

[case tesPassingVariousLiteralsForKeywordVarArg]
from typing import Any, Dict
from typing_extensions import Literal
kw: Dict[Literal[1, "a"], Any]
def f(a, b): pass
f(**kw) # E: Keywords must be strings
[builtins fixtures/dict.pyi]

[case testPassingMappingSubclassForKeywordVarArg]
from typing import Mapping
class MappingSubclass(Mapping[str, str]): pass
Expand Down