Skip to content

Errors when merging consecutive leading-indented (?) overload statements #17521

Open
@bzoracler

Description

@bzoracler

Bug Report, To Reproduce, & Actual Behaviour

In the following, sys.version_info >= ... can be any other compile-time constant (e.g. those set by always_true / always_false, typing.TYPE_CHECKING, etc.).

It appears that consecutive overload merge candidate blocks cause this issue; adding an additional statement between the blocks (here, # UNCOMMENT_ME_TO_FIX = ...) allow the overloads to resolve properly.

mypy Playground

import sys
import typing as tp

if sys.version_info >= (3, 10):
    @tp.overload
    def a(arg: str, /) -> str: ...
    @tp.overload
    def a(arg: int, /) -> int: ...

def a(arg: str | int, /) -> str | int:
    return arg

# UNCOMMENT_ME_TO_FIX = ...

if sys.version_info >= (3, 9):
    @tp.overload  # main.py:16: error: An overloaded function outside a stub file must have an implementation  [no-overload-impl]
    def b(arg: str, /) -> str: ...
    @tp.overload
    def b(arg: int, /) -> int: ...

def b(arg: str | int, /) -> str | int:  # main.py:21: error: Name "b" already defined on line 16  [no-redef]
    return arg

Expected Behavior

No issues

Your Environment

  • Mypy version used: 1.10.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions