python / python/mypy

Detect missing or incorrect overload defaults

Open
#19,411 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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?

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 reviewing mypy and mypy.stubtest's existing overload checks, then compare them with the AST-based fix-overload-defaults tool linked in the issue. Validate behavior against the two foo examples and determine whether detecting incorrect defaults, missing defaults, or both is in scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.