Skip to content

isinstance and TypeVar #13492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johnburnett opened this issue Aug 24, 2022 · 1 comment
Closed

isinstance and TypeVar #13492

johnburnett opened this issue Aug 24, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@johnburnett
Copy link

Bug Report

Run mypy against the following:

from typing import TypeVar

T = TypeVar("T")

def _checkType(arg: T, typ: type) -> T:
    if isinstance(arg, typ):
        return arg
    raise TypeError(f"Argument is not a {typ}.")

reveal_type(_checkType(42, int))

Expected Behavior

Type checks to pass

Actual Behavior

The following error is printed: error: Incompatible return value type (got "object", expected "T")

Additionally, these two variants work around the issue (and pass without error):

def _checkType_cast(arg: T, typ: type) -> T:
    if isinstance(arg, typ):
        return cast(T, arg)
    raise TypeError(f"Argument is not a {typ}.")

def _checkType_backup(arg: T, typ: type) -> T:
    backup = arg
    if isinstance(arg, typ):
        return backup
    raise TypeError(f"Argument is not a {typ}.")

Your Environment

  • Mypy version used: 0.971
  • Python version used: 3.9.7
@johnburnett johnburnett added the bug mypy got something wrong label Aug 24, 2022
@ilinum
Copy link
Collaborator

ilinum commented Sep 4, 2022

Duplicate of #13462.

@ilinum ilinum closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants