mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Inconsistent behavior with Sequence/Iterable/TypedDict/Union

Open jeremyn opened this issue 3 years ago • 0 comments

Bug Report

I'm getting inconsistent behavior with Sequence/Iterable/TypedDict/Union.

To Reproduce

Run mypy on:

from typing import Iterable, Sequence, TypedDict, Union

class DictType(TypedDict):
    a: str

# No error below with this Sequence line
#UnionType = Union[DictType, Sequence[DictType]]
# Has error below with this Iterable line
UnionType = Union[DictType, Iterable[DictType]]

def func(arg: UnionType) -> None:
    if isinstance(arg, dict):
        # Error here only if using the Iterable line above:
        # error: List item 0 has incompatible type "Union[DictType, Dict[Any, Any]]"; expected "DictType"
        arg = [arg]

Expected Behavior

I expect no errors with either the Sequence or Iterable lines uncommented, or at least both should error.

Actual Behavior

I get the indicated "incompatible type" error only with the Iterable line but not with the Sequence line.

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.9.5
  • Operating system and version: Ubuntu 20.04

jeremyn avatar Aug 09 '22 13:08 jeremyn