python / python/mypy

warn when default values on overloads differ from implementation signature

Open
#11,397 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

from typing import overload


@overload
def foo(value1: str, value2: bool = False) -> str: # this default value is wrong/useless
    ...


@overload
def foo(value1: int, value2: bool = False) -> int: # this default value is wrong/useless
    ...


def foo(value1: str | int, value2: bool = True) -> str | int:
    ...

in fact, maybe even prohibit defaults on overload signatures entirely, like in typescript:

function foo(value1: number, value2: boolean = false): number //error: A parameter initializer is only allowed in a function or constructor implementation.
function foo(value1: string, value2: boolean = false): string //error: A parameter initializer is only allowed in a function or constructor implementation.
function foo(value1: number | string, value2: boolean = true): number | string {
    return 1
}

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

Start by reproducing the overload example and trace mypy's overload-signature checking. Determine whether the accepted behavior should be a warning for defaults that differ from the implementation or an error for any defaults on overloads. Done means the chosen rule is enforced consistently and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.