|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import Dict, Generic, Iterable, TypeVar |
| 3 | +from typing import Any, Dict, Generic, Iterable, TypeVar, Union |
4 | 4 | from typing_extensions import assert_type
|
5 | 5 |
|
6 | 6 | # These do follow `__init__` overloads order:
|
@@ -57,3 +57,93 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
|
57 | 57 |
|
58 | 58 | dict(["foo", "bar", "baz"]) # type: ignore
|
59 | 59 | dict([b"foo", b"bar", b"baz"]) # type: ignore
|
| 60 | + |
| 61 | +# Exploring corner cases of dict.get() |
| 62 | +d_any: dict[str, Any] = {} |
| 63 | +d_str: dict[str, str] = {} |
| 64 | +any_value: Any = None |
| 65 | +str_value = "value" |
| 66 | +int_value = 1 |
| 67 | + |
| 68 | +assert_type(d_any["key"], Any) |
| 69 | +assert_type(d_any.get("key"), Union[Any, None]) |
| 70 | +assert_type(d_any.get("key", None), Any) |
| 71 | +assert_type(d_any.get("key", any_value), Any) |
| 72 | +assert_type(d_any.get("key", str_value), Any) |
| 73 | +assert_type(d_any.get("key", int_value), Any) |
| 74 | + |
| 75 | +assert_type(d_str["key"], str) |
| 76 | +assert_type(d_str.get("key"), Union[str, None]) |
| 77 | +assert_type(d_str.get("key", None), Union[str, None]) |
| 78 | +# Pyright has str instead of Any here |
| 79 | +assert_type(d_str.get("key", any_value), Any) # pyright: ignore[reportAssertTypeFailure] |
| 80 | +assert_type(d_str.get("key", str_value), str) |
| 81 | +assert_type(d_str.get("key", int_value), Union[str, int]) |
| 82 | + |
| 83 | +# Now with context! |
| 84 | +result: str |
| 85 | +result = d_any["key"] |
| 86 | +result = d_any.get("key") # type: ignore[assignment] |
| 87 | +result = d_any.get("key", None) |
| 88 | +result = d_any.get("key", any_value) |
| 89 | +result = d_any.get("key", str_value) |
| 90 | +result = d_any.get("key", int_value) |
| 91 | + |
| 92 | +result = d_str["key"] |
| 93 | +result = d_str.get("key") # type: ignore[assignment] |
| 94 | +result = d_str.get("key", None) # type: ignore[arg-type] |
| 95 | +result = d_str.get("key", any_value) |
| 96 | +result = d_str.get("key", str_value) |
| 97 | +result = d_str.get("key", int_value) # type: ignore[arg-type] |
| 98 | + |
| 99 | + |
| 100 | +# Return values also make things weird |
| 101 | + |
| 102 | +# Pyright doesn't have a version of no-any-return, |
| 103 | +# and mypy doesn't have a type: ignore that pyright will ignore. |
| 104 | +# def test1() -> str: |
| 105 | +# return d_any["key"] # mypy: ignore[no-any-return] |
| 106 | + |
| 107 | + |
| 108 | +def test2() -> str: |
| 109 | + return d_any.get("key") # type: ignore[return-value] |
| 110 | + |
| 111 | + |
| 112 | +# def test3() -> str: |
| 113 | +# return d_any.get("key", None) # mypy: ignore[no-any-return] |
| 114 | +# |
| 115 | +# |
| 116 | +# def test4() -> str: |
| 117 | +# return d_any.get("key", any_value) # mypy: ignore[no-any-return] |
| 118 | +# |
| 119 | +# |
| 120 | +# def test5() -> str: |
| 121 | +# return d_any.get("key", str_value) # mypy: ignore[no-any-return] |
| 122 | +# |
| 123 | +# |
| 124 | +# def test6() -> str: |
| 125 | +# return d_any.get("key", int_value) # mypy: ignore[no-any-return] |
| 126 | + |
| 127 | + |
| 128 | +def test7() -> str: |
| 129 | + return d_str["key"] |
| 130 | + |
| 131 | + |
| 132 | +def test8() -> str: |
| 133 | + return d_str.get("key") # type: ignore[return-value] |
| 134 | + |
| 135 | + |
| 136 | +def test9() -> str: |
| 137 | + return d_str.get("key", None) # type: ignore[arg-type] |
| 138 | + |
| 139 | + |
| 140 | +def test10() -> str: |
| 141 | + return d_str.get("key", any_value) |
| 142 | + |
| 143 | + |
| 144 | +def test11() -> str: |
| 145 | + return d_str.get("key", str_value) |
| 146 | + |
| 147 | + |
| 148 | +def test12() -> str: |
| 149 | + return d_str.get("key", int_value) # type: ignore[arg-type] |
0 commit comments