Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Type of list items not inferred when using isinstance inside all #7853

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

Open
RobbeSneyders opened this issue Nov 2, 2019 · 2 comments
Open
Labels
feature priority-1-normal topic-type-narrowing Conditional type narrowing / binder

Comments

@RobbeSneyders
Copy link

RobbeSneyders commented Nov 2, 2019

The type of items in a list is not inferred when using an isinstance check in an all call.

from typing import List

class A:
    pass

class B(A):
    pass

def test(arg: List[A]) -> None:
    if all([isinstance(a, B) for a in arg]):
        test2(arg)

def test2(arg: List[B]) -> None:
    pass
  • What is the actual behavior/output?

error: Argument 1 to "test_2" has incompatible type "List[A]"; expected "List[B]"
Found 1 error in 1 file (checked 1 source file)

  • What is the behavior/output you expect?

I expect mypy to infer the type of the list items based on the if check.

  • What are the versions of mypy and Python you are using?

mypy: v0.740
python: 3.6.7

@JelleZijlstra
Copy link
Member

This is a feature request; mypy currently only understands a very small number of special type-narrowing functions (like isinstance()). I don't think there are plans to add more, but contributions would likely be accepted.

@ilevkivskyi
Copy link
Member

Closely related to #4573

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature priority-1-normal topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants