Skip to content

Commit 6f51ded

Browse files
committed
Add additional try based on Guido's 2017 suggestion
Ref: python/mypy#3737 (comment)
1 parent 1dcc06e commit 6f51ded

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

main.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# requires-python = ">=3.13"
33
# ///
44

5-
from typing import Any, TypeVar, reveal_type
5+
from typing import Any, TypeVar, reveal_type, overload
66

77
type Record = dict[str, Any]
88

@@ -43,3 +43,23 @@ def from_record_2(record: Record, element_cls: type[T] | None = None) -> T:
4343
reveal_type(from_record_2(REC)) # reveals Element, since this is default
4444
reveal_type(from_record_2(REC, element_cls=Element)) # reveals Element
4545
reveal_type(from_record_2(REC, element_cls=DerivedElement)) # reveals DerivedElement
46+
47+
48+
# on a third try, I use guido's suggestion in https://github.com/python/mypy/issues/3737#issuecomment-316263133.
49+
# this doesn't work either
50+
51+
@overload
52+
def from_record_3(record: Record) -> Element: ...
53+
54+
55+
@overload
56+
def from_record_3(record: Record, element_cls: type[T]) -> T: ...
57+
58+
59+
def from_record_3(record: Record, element_cls):
60+
return element_cls(record)
61+
62+
63+
reveal_type(from_record_3(REC)) # reveals Element, since this is default
64+
reveal_type(from_record_3(REC, element_cls=Element)) # reveals Element
65+
reveal_type(from_record_3(REC, element_cls=DerivedElement)) # reveals DerivedElement

0 commit comments

Comments
 (0)