Set dunder operators too permissive?
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
Similar to #1840
With #3181 I made KeysView consistent with Set but I really think the operators are confusingly permissive.
Let's look at them
# difference
{'1'} - {2}
reveal_type({'1'} - {2}) # => Set[str]
This should fail because this is always a null operation and thus
a programmer error.
# intersection
{'1'} & {2} # => Set[str]
reveal_type({'1'} & {2}) # => Set[str]
This really should fail because it is unlikely meaningful; the
return type is always an empty set thus it is also not a Set[str]
# sym difference
{'1'} ^ {2}
reveal_type({'1'} ^ {2}) # => Set[Union[str, int]]
Although the assumed union type is correct, Set[A] & Set[B] => Set[A|B] just doesn't make sense. The operator can only meaningful be applied to same type sets. What we do here is just a form of plain concat or union.
# union
{'1'} | {2} # MAYBE OK
reveal_type({'1'} | {2}) # => Set[Union[str, int]]
If we look at Sets as just containers without some
properties, we could accept it to construct mixed
type Sets using 'union'. I think it is okay for Python,
the name union makes it meaningful, but I maybe never did
that in practice.
Since it adds to the type it is also likely that
the following part of the program catches mistakes.
(But lists don't allow that [1] + ['2'] although they're really
arbitrary containers. Except my PR #3183.)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从这个 issue 的示例开始,并比较 #1840、#3181 和 #3183 中的相关讨论。确定哪些 Set 和 KeysView 运算符应拒绝混合元素类型,包括是否继续允许 union。当项目为每个运算符都制定了达成一致的规则,且相关的 typing 行为反映了该决定时,这项工作就完成了。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100