Skip to content

Error merging nested overloads #12606

@cdce8p

Description

@cdce8p

Bug Report

Mypy can't merge nested overloads. Those can happen if all are wrapped with if TYPE_CHECKING and combined with a nested conditional overload.

https://mypy.readthedocs.io/en/stable/more_types.html#conditional-overloads

=> I'm working on a fix already and plan to open a PR soon.

To Reproduce

from typing import overload

class A: ...
class B: ...
class C: ...
class D: ...

@overload
def f1(g: A) -> A: ...
if True:
    @overload
    def f1(g: B) -> B: ...
    if True:
        @overload
        def f1(g: C) -> C: ...
        @overload
        def f1(g: D) -> D: ...
def f1(g): ...

Expected Behavior

Overloads should merge just fine.

reveal_type(f1(A()))  # N: Revealed type is "__main__.A"
reveal_type(f1(B()))  # N: Revealed type is "__main__.B"
reveal_type(f1(C()))  # N: Revealed type is "__main__.C"
reveal_type(f1(D()))  # N: Revealed type is "__main__.D"

Actual Behavior

test.py:8: error: Single overload definition, multiple required
test.py:11: error: Name "f1" already defined on line 8
test.py:11: error: An overloaded function outside a stub file must have an implementation
test.py:18: error: Name "f1" already defined on line 8

Your Environment

  • Mypy version used: mypy 0.960+dev.44993e6a18f852a9296786267ecf26ea730b4f31
  • Mypy command-line flags: mypy test2.py --always-true True --always-false False

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions