a way to refer to result type of an overloaded function
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
I am looking for a way to refer to result type of an overloaded function.
To illustrate:
import typing as tp
T = tp.TypeVar("T")
@tp.overload
def gf1(i: int, j: tp.Any) -> str:
...
@tp.overload
def gf1(i: tp.Iterable[T], j: bool) -> tp.Dict[T, T]:
...
def gf1(i: tp.Union[int, tp.Iterable[T]], j: tp.Any) -> tp.Union[str, tp.Dict[T, T]]:
pass
gf1.resultType = lambda *a: None
class C(tp.Generic[T]):
def f1(self) -> gf1.resultType(T, bool):
pass
def f2(self) -> gf1.resultType(tp.List[T], bool):
pass
if not tp.TYPE_CHECKING:
def reveal_type(x):
pass
# outputs: builtins.str
reveal_type(gf1(1, True))
# wanted: builtins.str
reveal_type(C[int]().f1())
# outputs: builtins.dict[builtins.int*, builtins.int*]
reveal_type(gf1([1], True))
# wanted: builtins.dict[builtins.int*, builtins.int*]
reveal_type(C[int]().f2())
The f.resultOf syntax is, of course, tentative.
Issue #623 seems to suggest that this may be achievable with a clever use of of Protocol but I just cannot twist my head around that.
Upon request I can elaborate on my specific use case for this but I believe it would suffice to say that in real code gf1.resultType would be used multiple times. Specific argument types for gf1 were chosen arbitrarily for the sake of example.
Not sure whether #548 is anyhow related.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从这个 issue 中的重载函数示例开始,并查看 issues #623 和 #548 中的相关讨论。没有确定任何 repository 文件或测试;只有在就引用重载函数的结果类型达成一个约定且可实现的范围后,才能视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100