Skip to content

Mypy's --warn-unreachable option erroneously considers some isinstance calls to be always False with mypy-zope 0.9.0 #91

@DMRobertson

Description

@DMRobertson

With mypy 1.0.0 and mypy-zope 0.9.0, the typechecker seems to think that that isinstance(x, T) is always false when T implements some interface Iand x: Optional[I]. For example, the source

from typing import Optional, reveal_type
from zope.interface import implementer, Interface


class IFoo(Interface):
    ...


@implementer(IFoo)
class MyFoo:
    ...


def make_foo() -> Optional[IFoo]:
    return MyFoo()


x = make_foo()
reveal_type(x)
assert isinstance(x, MyFoo)
print("hello")

yields

$ mypy temp.py
temp.py:19: note: Revealed type is "Union[temp.IFoo, None]"
temp.py:21: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

but at runtime, we see that the print statement is reachable:

$ python temp.py
Runtime type is 'MyFoo'
hello

Would guess that #90 is related to this somehow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions