Skip to content

Unions break with isinstance() #1135

Closed
Closed
@bdarnell

Description

@bdarnell

Mypy incorrectly reports two errors in this (python 2) snippet.

$ nl -ba /tmp/foo.py
     1  import typing
     2  
     3  def foo(value):
     4      # type: (typing.Union[unicode, bytes]) -> None
     5      if isinstance(value, unicode):
     6          return
     7      if not isinstance(value, bytes):
     8          raise TypeError("Expected bytes or unicode; got %r" % type(value))
$ PYTHONPATH=.  scripts/mypy --py2 /tmp/foo.py
/tmp/foo.py: note: In function "foo":
/tmp/foo.py:7: error: Argument 1 to "isinstance" has incompatible type "Union[object, object]"; expected "object"
/tmp/foo.py:8: error: No overload variant of "type" matches argument types [Union[<ERROR>, <ERROR>, <ERROR>, <ERROR>]]

If lines 5 and 6 are removed, the errors disappear. If line 7 is replaced with if True:, the error on line 8 remains. Replacing unicode with str and running mypy in py3 mode also makes the errors disappear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions