Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing type store for overloads #16803

Merged
merged 1 commit into from Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/checkexpr.py
Expand Up @@ -2825,6 +2825,7 @@ def infer_overload_return_type(
# Return early if possible; otherwise record info, so we can
# check for ambiguity due to 'Any' below.
if not args_contain_any:
self.chk.store_types(m)
return ret_type, infer_type
p_infer_type = get_proper_type(infer_type)
if isinstance(p_infer_type, CallableType):
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-generics.test
Expand Up @@ -1481,6 +1481,30 @@ if int():
b = f(b)
[builtins fixtures/list.pyi]

[case testGenericDictWithOverload]
from typing import Dict, Generic, TypeVar, Any, overload
T = TypeVar("T")

class Key(Generic[T]): ...
class CustomDict(dict):
@overload # type: ignore[override]
def __setitem__(self, key: Key[T], value: T) -> None: ...
@overload
def __setitem__(self, key: str, value: Any) -> None: ...
def __setitem__(self, key, value):
return super().__setitem__(key, value)

def a1(d: Dict[str, Any]) -> None:
if (var := d.get("arg")) is None:
var = d["arg"] = {}
reveal_type(var) # N: Revealed type is "builtins.dict[Any, Any]"

def a2(d: CustomDict) -> None:
if (var := d.get("arg")) is None:
var = d["arg"] = {}
reveal_type(var) # N: Revealed type is "builtins.dict[Any, Any]"
[builtins fixtures/dict.pyi]


-- Type variable scoping
-- ---------------------
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/typexport-basic.test
Expand Up @@ -1236,6 +1236,27 @@ LambdaExpr(10) : def (x: builtins.int) -> builtins.int
LambdaExpr(12) : def (y: builtins.str) -> builtins.str
LambdaExpr(13) : def (x: builtins.str) -> builtins.str

[case testExportOverloadArgTypeDict]
## DictExpr
from typing import TypeVar, Generic, Any, overload, Dict
T = TypeVar("T")
class Key(Generic[T]): ...
@overload
def f(x: Key[T], y: T) -> T: ...
@overload
def f(x: int, y: Any) -> Any: ...
def f(x, y): ...
d: Dict = {}
d.get(
"", {})
f(
2, {})
[builtins fixtures/dict.pyi]
[out]
DictExpr(10) : builtins.dict[Any, Any]
DictExpr(12) : builtins.dict[Any, Any]
DictExpr(14) : builtins.dict[Any, Any]

-- TODO
--
-- test expressions
Expand Down