Skip to content

Detect missing or incorrect overload defaults #19411

Open
@MarcoGorelli

Description

@MarcoGorelli

Feature

Is it in-scope for either mypy or stubtest to detect incorrect or missing defaults in overloads?

Incorrect default

from typing import Literal, overload, reveal_type

@overload
def foo(a: Literal[True] = ...) -> None: ...
@overload
def foo(a: Literal[False]) -> int: ...
def foo(a: bool = False) -> None | int:
    return 1 if not a else None

reveal_type(foo())

Here, the default for a is False, yet the overload has a: Literal[True] = ..., meaning that foo() will be revealed to be of type None instead of type int

Missing overload example

from typing import Literal, overload

@overload
def foo(a: Literal[True]) -> None: ...
@overload
def foo(a: Literal[False]) -> int: ...
def foo(a: bool = False) -> None | int:
    return 1 if not a else None

The default for a is False, so the second overload should probably include a: Literal[False] = ...?

Pitch

I tried putting together a little static analysis tool to detect simple cases of the above: https://github.com/MarcoGorelli/fix-overload-defaults

It only uses ast parsing, and is very simple, so it can't detect much, but it was already enough to find issues in a few libraries where I tried it:

So, I figured that this might be broadly useful?

I'm much more bothered about detecting incorrect defaults than potentially missing ones. Might this be in-scope for mypy or mypy.stubtest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions