Skip to content

issubclass(...) causes a TypeVar to become a Union, leading to false positives #11163

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
finite-state-machine opened this issue Sep 21, 2021 · 0 comments · Fixed by #19183
Closed
Labels
bug mypy got something wrong topic-type-variables

Comments

@finite-state-machine
Copy link

finite-state-machine commented Sep 21, 2021

Bug Report

The presence of issubclass(...) in the code sample that follows causes arg to take on type Union[T, Type[object]]. This change in type leads to false positives, as demonstrated below.

To Reproduce

Run mypy on the following code: mypy-play.net

from typing import TypeVar

T = TypeVar('T')

def function(arg: T) -> T:
    if isinstance(arg, type) and issubclass(arg, Exception):
        raise RuntimeError
    return arg  # error: Incompatible return value type (got "Union[T, Type[object]]", expected "T")  [return-value]

Expected Behavior

No errors should be reported. Testing the type of a variable should not change the type of the variable as seen outside the controlled code blocks (if/else).

Actual Behavior

The type of arg is changed, leading to a false positive as noted in the code sample above.

Your Environment

  • Mypy version used: 0.910 [edit: verified with 1.9.0]
  • Mypy command-line flags: (none necessary)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.8.3 from pyenv [edit: verified with 3.10]
  • Operating system and version: macOS 10.14 with homebrew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants