8000 Pass literals as kwargs by arvi18 · Pull Request #9 · coderabbit-test/mypy · GitHub
[go: up one dir, main page]

Skip to content

Pass literals as kwargs #9

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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
vickyhuo committed Mar 30, 2021
commit 49b2599a1cae8c4fe26fd5b0d4f2efe2bd2b1684
36 changes: 23 additions & 13 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from contextlib import contextmanager
import itertools
from typing import (
Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator
Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator, Iterable
)
from typing_extensions import ClassVar, Final, overload

Expand Down Expand Up @@ -1439,15 +1439,21 @@ def check_for_extra_actual_arguments(self,
context)
is_unexpected_arg_error = True
ok = False
elif actual_type.type.has_base('typing.Mapping'):
if messages:
elif isinstance(actual_type, Instance) and \
actual_type.type.has_base('typing.Mapping'):
if messages and actual_type.args:
args = try_getting_str_literals_from_type(actual_type.args[0])
if args and nodes.ARG_STAR2 not in callee.arg_kinds:
messages.unexpected_keyword_argument(callee, args[0], actual_type.args[0], context)
messages.unexpected_keyword_argument(
callee, args[0], actual_type.args[0], context)
is_unexpected_arg_error = True
elif args and nodes.ARG_POS in callee.arg_kinds and not all(arg in callee.arg_names for arg in args):
act_name = [name for name, kind in zip(actual_names, actual_kinds) if kind != nodes.ARG_STAR2]
messages.too_few_arguments(callee, context, act_name)
elif args and nodes.ARG_POS in callee.arg_kinds and \
not all(arg in callee.arg_names for arg in args) and \
isinstance(actual_names, Iterable):
act_names = [name for name, kind in
zip(iter(actual_names), actual_kinds)
if kind != nodes.ARG_STAR2]
messages.too_few_arguments(callee, context, act_names)
ok = False

# *args/**kwargs can be applied even if the function takes a fixed
Expand Down Expand Up @@ -3948,28 +3954,32 @@ 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?"""
key_args = None
if hasattr(typ, 'args') and typ.args:
key_args = try_getting_str_literals_from_type(typ.args[0]) # type: ignore

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)]))
or
(is_subtype(typ, self.chk.named_type('typing.Mapping')) and
try_getting_str_literals_from_type(typ.args[0])))
key_args is not None))

else:
return (
is_subtype(typ, self.chk.named_generic_type(
(is_subtype(typ, self.chk.named_generic_type(
'typing.Mapping',
[self.named_type('builtins.str'),
AnyType(TypeOfAny.special_form)]))
AnyType(TypeOfAny.special_form)])))
or
is_subtype(typ, self.chk.named_generic_type(
(is_subtype(typ, self.chk.named_generic_type(
'typing.Mapping',
[self.named_type('builtins.unicode'),
AnyType(TypeOfAny.special_form)]))
AnyType(TypeOfAny.special_form)])))
or
(is_subtype(typ, self.chk.named_type('typing.Mapping')) and
try_getting_str_literals_from_type(typ.args[0])))
key_args is not None))

def has_member(self, typ: Type, member: str) -> bool:
"""Does type have member with the given name?"""
Expand Down
0