Allow to use function objects as singletons
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
I was trying to create some stubs for lxml library, but I encountered a function that depending on a function object it gets (as in specific function object - checked with identity check) only finds objects of specific type.
Here's a very simplified mock-up of it so that you can get some idea on what I mean (the typing for arg argument in the find function is incorrect and that's what I want to be able to type hint in some way):
from typing import Union
class _Element: ...
def Element() -> _Element: ...
class _Comment: ...
def Comment() -> _Comment: ...
def find(arg: Union[Element, Comment]) -> Union[_Element, _Comment]:
if arg is Element:
# only find _Element objects
elif arg is Element:
# only find _Comment objects
# this function should only accept `Element` and `Comment` function object
find(arg=Element)
So basically those functions serve as a singletons. Unfortunately typing.Union only allows Enums to work as singletons.
I am not entirely sure if this should be supported, usually the code could just be adjusted to use Enums, but in this case it will be used for type stubs so the code can't be edited. And to be honest, I am not sure if the code really needs editing in that case as if I didn't need typing for that library, I would say it's actually quite intuitive to use these factory functions as arguments in this case.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue #668 中的 factory-function singleton 示例以及与 Enum singleton 的比较开始。由于 issue 中没有指定实现文件或测试,因此首先确定 typing 在哪里表示 singleton 值,以及函数对象目前是如何处理的。完成标准是确认这些函数对象是否可以在类型提示中被接受并进行区分表示,并覆盖 Element/Comment 风格的情况。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100