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
Closed
@ttsiodras

Description

@ttsiodras

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0