Skip to content

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

Open
@sobolevn

Description

@sobolevn

I am working on removing dict and **kwargs hack from mypy after python/typeshed#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 😢

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-callsFunction calls, *args, **kwargs, defaults

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions