mypy icon indicating copy to clipboard operation
mypy copied to clipboard

`*args` and `**kwargs` are allowed even to methods with no arguments

Open sobolevn opened this issue 3 years ago • 1 comments

I am working on removing dict and **kwargs hack from mypy after https://github.com/python/typeshed/pull/8517 is merged.

But, I found a very confusing thing in how *args and **kwargs are checked.

Simple repro (all of the examples below work):

def some() -> None: ...

# args
some(*[1, 2])
args = [1, 2]
some(*args)

# kwargs
some(**{'a': 1})
kw = {'a': 2}
some(**kw)

All of these would raise TypeError in runtime. I think that the main idea was to allow calls like some(*[]) and some(**{}) which are fine in runtime.

This affects how overloads are selected in complex cases like:

class dict2(Generic[KT, VT]):
    @overload
    def __init__(self, __iterable: Iterable[Tuple[KT, VT]]) -> None: pass
    @overload
    def __init__(self: "dict2[str, VT]", __iterable: Iterable[Tuple[str, VT]], **kwargs: VT) -> None: pass

it = [(1, 'x')]
kw = {'x': 'y'}
reveal_type(dict2(it, **kw))

It looks like the second @overload will raise an error: Iterable[Tuple[str, VT]] is required, but Iterable[Tuple[int, str]] is given.

But it does not, because of how **kwargs are silently ignored. So, first @overload always matches.

This is broken, if you ask me 😢

sobolevn avatar Aug 11 '22 07:08 sobolevn

CC @AlexWaygood

sobolevn avatar Aug 11 '22 07:08 sobolevn

All of these would raise TypeError in runtime. I think that the main idea was to allow calls like some(*[]) and some(**{}) which are fine in runtime.

I do think that was the motivation. It doesn't seem like a practical concern; surely there is no reason for valid code to do some(*args) if some() doesn't take positional args.

JelleZijlstra avatar Aug 13 '22 00:08 JelleZijlstra

This is behaviour that there has already been some back and forth on, so I would look at old issues before making a change

hauntsaninja avatar Aug 13 '22 00:08 hauntsaninja