Skip to content

overloads in if TYPE_CHECKING shouldn't require implementations #19169

Open
@A5rocks

Description

@A5rocks
Collaborator

Feature

As I explained in the title.

Pitch

Currently this raises an error:

from typing import TYPE_CHECKING, overload

if TYPE_CHECKING:
    @overload  # E: An overloaded function outside a stub file must have an implementation
    def f(x: int) -> str:
        ...
    
    @overload
    def f(x: str) -> bytes:
        ...

I think that if code is in if TYPE_CHECKING then it shouldn't need an implementation though.

Activity

Avasam

Avasam commented on May 30, 2025

@Avasam
SponsorContributor

Isn't TYPE_CHECKING meant to be transparent to type-checkers? The above can be done as:

from typing import TYPE_CHECKING, overload

if TYPE_CHECKING:
    @overload
    def f(x: int) -> str: ...
    @overload
    def f(x: str) -> bytes: ...
    def f(x: int | str) -> str |  bytes: ...

mypy doesn't really know either if you meant the above, or if you legitimately misplaced your overloads/forgot the implementation:

from typing import TYPE_CHECKING, overload

if TYPE_CHECKING:
    # Imagine this is an overload that uses types or decorators that would fail at runtime (older python or stub-only symbols)
    @overload
    def f(x: int) -> str: ...
    @overload
    def f(x: str) -> bytes: ...
    
def oops_wongly_placed() -> None:
    return None

def f(x):
    if isinstance(x, int):
        return str(x)
    else:
        return x.encode()
A5rocks

A5rocks commented on May 30, 2025

@A5rocks
CollaboratorAuthor

I mean maybe it's supposed to be transparent?

I think empty body errors get disabled in there. I think in general it should be treated more like stubs.

uko3211

uko3211 commented on May 30, 2025

@uko3211

checker.py

According to the comment on that line, it seems the error for this case was intended not to be shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Avasam@A5rocks@sterliakov@uko3211

        Issue actions

          overloads in `if TYPE_CHECKING` shouldn't require implementations · Issue #19169 · python/mypy