8000 isinstance does not impact the type detected · Issue #1702 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

isinstance does not impact the type detected #1702

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
ttsiodras opened this issue Jun 12, 2016 · 2 comments
Closed

isinstance does not impact the type detected #1702

ttsiodras opened this issue Jun 12, 2016 · 2 comments

Comments

@ttsiodras
Copy link

I believe the example below depicts a bug in mypy's handling of isinstance. According to the documentation, isinstance informs the type of the variable inside the if block - but the following example shows that this is not the case:

$ pip freeze | grep mypy
mypy-lang==0.4.2

$ cat -n bug.py
 1  class A:
 2      pass
 3
 4  class B(A):
 5      def __init__(self):
 6          self._members = [1]
 7
 8  class C(A):
 9      def __init__(self):
10          self._members = [2]
11
12  class D(A):
13      pass
14
15  def foo(a: A) -> bool:
16      if isinstance(a, B) or isinstance(a, C):
17          for m in a._members:
18              print(m)
19
20  foo(B())
21  foo(C())

$ mypy bug.py
bug.py: note: In function "foo":
bug.py:17: error: "A" has no attribute "_members"

In line 17, the a variable is of type B or C - so it does have access to _members.

@ttsiodras
Copy link
Author

Further testing (thanks, Maxime!) shows that replacing...

if isinstance(a, B) or isinstance(a, C)

with

if isinstance(a, (B, C))

works properly. But the other form should work too, I think.

@JukkaL
Copy link
Collaborator
JukkaL commented Jun 13, 2016

Yes, the first example should be okay. There is an existing issue for or + isinstance: #942, so I'm closing this.

@JukkaL JukkaL closed this as completed Jun 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0