Skip to content

Incorrect errors with TypeVar + Union/overload #1241

@drewhaven

Description

@drewhaven

I'm trying to stub a function that takes different types for a base argument and then a bunch of AnyStrs. It seems that neither Union nor overload works correctly for stubbing it:

from typing import overload, AnyStr, Union

def f(x: Union[AnyStr, int], *a: AnyStr) -> None: ...

@overload
def g(x: AnyStr, *a: AnyStr) -> None: ...
@overload
def g(x: int, *a: AnyStr) -> None: ...

f('foo')
f('foo', 'bar')
f('foo', b'bar')  # correctly fails: Type argument 1 of "f" has incompatible value "object"
f(1)  # erroneously fails: error: Type argument 1 of "f" has incompatible value "int"
f(1, 'foo')  # erroneously fails: error: Type argument 1 of "f" has incompatible value "object"
f(1, 'foo', b'bar')  # correctly fails: error: Type argument 1 of "f" has incompatible value "object"

g('foo')
g('foo', 'bar')
g('foo', b'bar')  # correctly fails: error: Type argument 1 of "g" has incompatible value "object"
g(1)
g(1, 'foo')  # erroneously fails: error: Type argument 1 of "g" has incompatible value "object"
g(1, 'foo', b'bar')  # correctly fails: Type argument 1 of "g" has incompatible value "object"

Metadata

Metadata

Assignees

Labels

bugmypy got something wrong

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions