|
| 1 | +[case testMypyImmu] |
| 2 | +# cmd: mypy test.py |
| 3 | +[file test.py] |
| 4 | +from immutables import Map |
| 5 | +from typing import Dict, Union, Any, cast |
| 6 | + |
| 7 | +def init() -> None: |
| 8 | + def thing(m: Map[str, Union[str, int]]) -> None: |
| 9 | + ... |
| 10 | + |
| 11 | + thing(Map(foo=1)) |
| 12 | + thing(Map(foo='bar', baz=1)) |
| 13 | + thing(Map([('foo', 'bar'), ('bar', 1)])) |
| 14 | + thing(Map(Map(foo=1), bar='foo')) |
| 15 | + m = Map({1: 2}) |
| 16 | + thing(m) # E: Argument 1 to "thing" has incompatible type "Map[int, int]"; expected "Map[str, Union[str, int]]" |
| 17 | + |
| 18 | +def assignments() -> None: |
| 19 | + m_int__str = Map[int, str]() |
| 20 | + m_str__str = Map[str, str]() |
| 21 | + m_int_str__str = Map[Union[int, str], str]() |
| 22 | + m_str__int_str = Map[str, Union[int, str]]() |
| 23 | + |
| 24 | + m_int__str = m_str__str # E: Incompatible types in assignment (expression has type "Map[str, str]", variable has type "Map[int, str]") |
| 25 | + m_int__str = m_int_str__str # E: Incompatible types in assignment (expression has type "Map[Union[int, str], str]", variable has type "Map[int, str]") |
| 26 | + m_int__str = m_str__int_str # E: Incompatible types in assignment (expression has type "Map[str, Union[int, str]]", variable has type "Map[int, str]") |
| 27 | + |
| 28 | + m_str__str = m_int__str # E: Incompatible types in assignment (expression has type "Map[int, str]", variable has type "Map[str, str]") |
| 29 | + m_str__str = m_int_str__str # E: Incompatible types in assignment (expression has type "Map[Union[int, str], str]", variable has type "Map[str, str]") |
| 30 | + m_str__str = m_str__int_str # E: Incompatible types in assignment (expression has type "Map[str, Union[int, str]]", variable has type "Map[str, str]") |
| 31 | + |
| 32 | + m_int_str__str = m_int__str # E: Incompatible types in assignment (expression has type "Map[int, str]", variable has type "Map[Union[int, str], str]") |
| 33 | + m_int_str__str = m_str__str # E: Incompatible types in assignment (expression has type "Map[str, str]", variable has type "Map[Union[int, str], str]") |
| 34 | + m_int_str__str = m_str__int_str # E: Incompatible types in assignment (expression has type "Map[str, Union[int, str]]", variable has type "Map[Union[int, str], str]") |
| 35 | + |
| 36 | + m_str__int_str = m_int__str # E: Incompatible types in assignment (expression has type "Map[int, str]", variable has type "Map[str, Union[int, str]]") |
| 37 | + m_str__int_str = m_int_str__str # E: Incompatible types in assignment (expression has type "Map[Union[int, str], str]", variable has type "Map[str, Union[int, str]]") |
| 38 | + m_str__int_str = m_str__str |
| 39 | + |
| 40 | +def update() -> None: |
| 41 | + m_int__str: Map[int, str] = Map() |
| 42 | + m_str__str: Map[str, str] = Map() |
| 43 | + m_int_str__str: Map[Union[int, str], str] = Map() |
| 44 | + m_str__int_str: Map[str, Union[int, str]] = Map() |
| 45 | + |
| 46 | + m_int__str.update({1: '2'}) |
| 47 | + m_int__str.update({1: '2'}, three='4') # E: Unexpected keyword argument "three" for "update" of "Map" |
| 48 | + m_int__str.update({1: 2}) # E: Argument 1 to "update" of "Map" has incompatible type "Dict[int, int]"; expected "Union[IterableItems[int, str], Iterable[Tuple[int, str]]]" |
| 49 | + |
| 50 | + m_str__str.update({'1': '2'}) |
| 51 | + m_str__str.update({'1': '2'}, three='4') |
| 52 | + m_str__str.update({'1': 2}) # E: Argument 1 to "update" of "Map" has incompatible type "Dict[str, int]"; expected "Union[IterableItems[str, str], Iterable[Tuple[str, str]]]" |
| 53 | + |
| 54 | + m_int_str__str.update(cast(Dict[Union[int, str], str], {1: '2', '3': '4'})) |
| 55 | + m_int_str__str.update({1: '2'}, three='4') |
| 56 | + m_int_str__str.update({'1': 2}) # E: Argument 1 to "update" of "Map" has incompatible type "Dict[str, int]"; expected "Union[IterableItems[Union[int, str], str], Iterable[Tuple[Union[int, str], str]]]" |
| 57 | + |
| 58 | + m_str__int_str.update({'1': 2, '2': 3}) |
| 59 | + m_str__int_str.update({'1': 2, '2': 3}, four='5') |
| 60 | + m_str__int_str.update({1: 2}) # E: Argument 1 to "update" of "Map" has incompatible type "Dict[int, int]"; expected "Union[IterableItems[str, Union[int, str]], Iterable[Tuple[str, Union[int, str]]]]" |
| 61 | + |
| 62 | +def mutate() -> None: |
| 63 | + m = Map[str, str]() |
| 64 | + |
| 65 | + with m.mutate() as mm: |
| 66 | + mm[0] = '1' # E: Invalid index type "int" for "MapMutation[str, str]"; expected type "str" |
| 67 | + mm['1'] = 0 # E: Incompatible types in assignment (expression has type "int", target has type "str") |
| 68 | + mm['1'] = '2' |
| 69 | + del mm['1'] |
| 70 | + mm.set('3', '4') |
| 71 | + m2 = mm.finish() |
| 72 | + |
| 73 | + reveal_type(m2) # N: Revealed type is "immutables._map.Map[builtins.str*, builtins.str*]" |
0 commit comments