python / python/mypy

"An implementation for an overloaded function is not allowed in a stub file" for overloaded type hint

Open
#19,128 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

I'm documenting a compiled extension module written in Rust. I write docstrings in my .pyi file so that IDEs and API documentation can automatically pick up those docstrings.

Docstrings are allowed in functions but this errors when a function is overloaded.

To Reproduce

# tmp.pyi

from typing import overload

def fn1() -> None:
    """Call this function."""

@overload
def fn2(a: str) -> str: ...
@overload
def fn2(a: int) -> int: ...
def fn2(a: int | str) -> int | str:
    """Call this function."""
uv run mypy tmp.pyi

gives:

tmp.pyi:10: error: An implementation for an overloaded function is not allowed in a stub file  [misc]

Note that this does not error on line 3 for the non-overloaded function.

Expected Behavior

Expected mypy to not error when a docstring is included on a function that has overloads.

Actual Behavior

tmp.pyi:10: error: An implementation for an overloaded function is not allowed in a stub file  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:

    > uv run mypy --version
    mypy 1.15.0 (compiled: yes)
    
  • Mypy command-line flags: None, just uv run mypy tmp.pyi

  • Mypy configuration options from mypy.ini (and other config files): None

  • Python version used:

    > uv run python --version
    Python 3.9.21
    

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 running uv run mypy tmp.pyi with the reproducer and trace the diagnostic reported on line 10 for the overloaded stub function. Add regression coverage for allowing its docstring, and confirm the command completes without that error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.