Skip to content

spurious "Need type annotation" error with overloads #4227

Closed
@chadrik

Description

@chadrik

I'm getting "Need type annotation for variable" errors which I think are the result of a bug with overloads.

Stub file overload_test.pyi:

from typing import overload, TypeVar, Any

_T = TypeVar('_T')

@overload
def attr(default: _T = ..., blah: int = ...) -> _T: ...
@overload
def attr(default: Any = ...) -> Any: ...

Test program:

from overload_test import attr

a = attr()
b = attr()

reveal_type(a)
reveal_type(b)

mypy output:

test.py:3: error: Need type annotation for variable
test.py:4: error: Need type annotation for variable
test.py:6: error: Revealed type is 'Any'
test.py:6: error: Cannot determine type of 'a'
test.py:7: error: Revealed type is 'Any'
test.py:7: error: Cannot determine type of 'b'

It may be interesting to note that if I swap the order of the overloads, the "Need type annotation for variable" and "Cannot determine type" errors go away.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions