Skip to content
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

the possibility that isinstance(some_var, (A, B)) causes mypy to forget about a some_var's type #10835

Closed
finite-state-machine opened this issue Jul 17, 2021 · 4 comments
Labels
bug mypy got something wrong

Comments

@finite-state-machine
Copy link

Bug Report

Certain patterns of if isinstance(...) cause mypy to forget that a variable has a particular generic type.

To Reproduce

Run the following Python code through mypy with --strict:

from typing import TypeVar

# context:
T = TypeVar('T')
class A: pass
class B: pass


def function1(obj: T) -> T:  # expected behaviour:
    if isinstance(obj, A):
        pass
    elif isinstance(obj, B):
        pass
    return obj  # no problem here

def function2(obj: T) -> T:  # unexpected behaviour:
    if isinstance(obj, (A, B)):
        pass
    return obj  # mypy has forgotten that obj is of type T

The issue also reproduces with this alternate definition of function2:

def function2(obj: T) -> T:  # unexpected behaviour:
    if isinstance(obj, A) or isinstance(obj, B):
        pass
    return obj  # mypy has forgotten that obj is of type T

Expected Behavior

mypy should issue no errors.

Actual Behavior

<last line of function2>: error: Returning Any from function declared to return "T" 
[no-any-return]
        return obj  # mypy has forgotten that 'obj' is of type 'T'
        ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.910
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): no additional flags are necessary to reproduce the issue
  • Python version used: CPython 3.8.3 (from pyenv)
  • Operating system and version: macOS 10.14
@finite-state-machine finite-state-machine added the bug mypy got something wrong label Jul 17, 2021
@erictraut
Copy link

This appears to be the same issue as #10817.

@finite-state-machine
Copy link
Author

It could very well be the same root cause; given my lack of knowledge of mypy internals, I can't say. If there is a distinction, it's this: in #10817, it appears that the type of the variable is being positively inferred, overriding the already-known generic type. In this issue, no inference is possible, but the type somehow changes from the generic T to Any.

@finite-state-machine
Copy link
Author

finite-state-machine commented Jul 17, 2021

I also note that in this issue, testing against one class (at a time) is not sufficient to trigger the problem; testing against two classes is required. (See function1, which behaves as expected.)

@finite-state-machine
Copy link
Author

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