Skip to content

Type unknown with TypeVar default and overload #7173

@cdce8p

Description

@cdce8p

Describe the bug
Found this while writing tests for mypy, even though the first overload is technically redundant.

Code or Screenshots

from typing import Any
from typing_extensions import TypeVar, overload, assert_type

T1 = TypeVar("T1", default=str)

class A: ...
class B: ...

@overload
def func_1(x: A) -> A: ...
@overload
def func_1(x: B | T1) -> T1: ...
def func_1(x: Any) -> Any: ...
assert_type(func_1(A()), A)
assert_type(func_1(B()), str)  # error
test.py
  test.py:15:13 - error: "assert_type" mismatch: expected "str" but received "Unknown" (reportAssertTypeFailure)

In comparison, this works just fine

def func_2(x: B | T1) -> T1: ...
assert_type(func_2(A()), A)
assert_type(func_2(B()), str)

VS Code extension or command-line
pyright 1.1.349

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions