python / python/mypy

Better message for overloaded functions with required kwargs

Open
#16,528 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-overloads topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

The following:

from typing import overload, Union

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

says helpfully:

main.py:6: error: Too many positional arguments for "fun"  [misc]
Found 1 error in 1 file (checked 1 source file)

mypy-playground

In the following case however:

from typing import overload, Union

@overload
def fun(a: int, *, b: bool) -> None: ...

@overload
def fun(a: str, *, b: bool) -> None: ...

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

we get:

main.py:12: error: No overload variant of "fun" matches argument types "int", "bool"  [call-overload]
main.py:12: note: Possible overload variants:
main.py:12: note:     def fun(a: int, *, b: bool) -> None
main.py:12: note:     def fun(a: str, *, b: bool) -> None
Found 1 error in 1 file (checked 1 source file)

mypy-playground

I think the error should also point out that the number of positional arguments is incorrect.

Pitch

I found myself confused by this a couple of times and have wasted good few hours trying to understand what's wrong with the call which is clearly correct, and just glancing straight past the * - something that's easy to do with a longer / more complicated function signature. I think it would save a meaningful amount of time for users and it feels like a simple change.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce both Python examples in the linked mypy-playground cases and compare their diagnostics. Done means the overloaded-function error also explains that the positional argument count is invalid, while retaining the overload details.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.