Skip to content

@overload cannot be used with conditional "if TYPE_CHECKING" #9744

@blueyed

Description

@blueyed

Bug Report

The handling of @overload decorators happens in (code) blocks only, which makes it not find the implementation when using:

from typing import overload
from typing import TYPE_CHECKING
from typing import Union


class K:
    if TYPE_CHECKING:
        @overload
        def foo(self, arg: int) -> int:
            ...

        @overload
        def foo(self, arg: str) -> str:
            ...

    def foo(self, arg: Union[int, str]) -> Union[int, str]:
        ...
t-overload.py:8: error: An overloaded function outside a stub file must have an implementation  [misc]
t-overload.py:16: error: Name 'foo' already defined on line 8  [no-redef]
Found 2 errors in 1 file (checked 1 source file)

I think it would be nice / feasible to merge if TYPE_CHECKING blocks maybe, so that the parser would handle it together?

mypy 0.800+dev.6e99a2db100d794b1bf900746470527cd03fce16

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions